Created
November 5, 2011 06:39
-
-
Save toby55kij/1341193 to your computer and use it in GitHub Desktop.
groovy.json.JsonOutputのサンプル
This file contains 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 groovy.json.JsonBuilder | |
import groovy.json.JsonOutput | |
//JsonOutputのサンプル | |
//サンプルデータ | |
def data = { | |
people { | |
person { | |
firstName 'Yasuharu' | |
lastName 'Hayami' | |
// Named arguments are valid values for objects too | |
address( | |
city: 'Niigata', | |
country: 'Japan', | |
zip: 9501115 | |
) | |
married false | |
// a list of values | |
conferences 'JGGUG', 'JJUG' | |
} | |
} | |
} | |
//JsonOutput.toJson()の例 | |
def resultJsonOutput1 = JsonOutput.toJson data | |
println resultJsonOutput1 | |
//JsonBuilderで書くとこうなる | |
def builder = new JsonBuilder() | |
builder data | |
def resultJsonBuilder1 = builder.toString() | |
println resultJsonBuilder1 | |
//結果は一緒 | |
assert resultJsonOutput1 == resultJsonBuilder1 | |
//JsonOutput.prettyPrint()の例 | |
def resultJsonOutput2 = JsonOutput.prettyPrint resultJsonOutput1 | |
println resultJsonOutput2 | |
//JsonBuilderで書くとこうなる | |
def resultJsonBuilder2 = builder.toPrettyString() | |
println resultJsonBuilder2 | |
//結果は一緒 | |
assert resultJsonOutput2 == resultJsonBuilder2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment