Created
November 16, 2011 12:06
-
-
Save kanemu/1369933 to your computer and use it in GitHub Desktop.
[groovy][jackson]jacksonで読み込むJSONにシングルクォートの使用を許可する。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.codehaus.jackson.map.ObjectMapper | |
import org.codehaus.jackson.JsonParser.Feature | |
@Grab(group='org.codehaus.jackson', module='jackson-mapper-lgpl', version='1.6.0') | |
def mapper = new ObjectMapper() | |
//読み込み | |
String jsonText = """{ | |
"名前" : { "first" : "一朗", "last" : "鈴木" }, | |
"打率" : ".315", | |
"打点" : "43", | |
"本塁打" : "6" | |
}""" | |
def map = mapper.readValue(jsonText, Map) | |
assert map == ["名前":["first":"一朗", "last":"鈴木"], "打率":".315", "打点":"43", "本塁打":"6"] | |
assert map.getClass().toString() == "class java.util.LinkedHashMap" | |
//読み込み | |
jsonText = """{ | |
'名前' : { 'first' : '一朗', 'last' : '鈴木' }, | |
'打率' : '.315', | |
'打点' : '43', | |
'本塁打' : '6' | |
}""" | |
mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true); | |
def map2 = mapper.readValue(jsonText, Map) | |
assert map2 == ["名前":["first":"一朗", "last":"鈴木"], "打率":".315", "打点":"43", "本塁打":"6"] | |
assert map2.getClass().toString() == "class java.util.LinkedHashMap" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment