검색결과 리스트
글
LANGUAGE/Java & Groovy
2016. 2. 7. 21:32
///// Java (자바)
///// 환경
Java
Groovy
Gradle
IntelliJ
///// Dependency (디펜덴씨, 의존)
compile 'org.codehaus.groovy:groovy-all:2.3.11'
compile 'org.codehaus.jackson:jackson-core-asl:1.9.13'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
///// Example (Serialize & Deserialize)
// Set Map
Map<String, String> obj = new HashMap<String, String>()
obj.hi = 1
obj.Str = 'hihi'
obj.Str1 = ''
obj.Str2 = null
obj[''] = 'ads'
////////// We Need This Object To Use JSON
ObjectMapper mapper = new ObjectMapper()
// Serialize
String json = mapper.writeValueAsString(obj)
println json
// Serialize Remove Null Object
mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);
json = mapper.writeValueAsString(obj)
println json
// Serialize PrettyPrinter
json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj)
println json
// Deserialize
println mapper.readValue(json, Map.class)
// Deserialize
println mapper.readValue(json, new TypeReference<Map>(){})
///// Result (결과)
{"":"ads","Str":"hihi","Str1":"","Str2":null,"hi":1}
{"":"ads","Str":"hihi","Str1":"","hi":1}
{
"" : "ads",
"Str" : "hihi",
"Str1" : "",
"hi" : 1
}
[:ads, Str:hihi, Str1:, hi:1]
[:ads, Str:hihi, Str1:, hi:1]
///// 참고
How to prevent null values inside a Map and null fields inside a bean from getting serialized through Jackson:
How Do I Write a Jackson JSON Serializer & Deserializer?:
'LANGUAGE > Java & Groovy ' 카테고리의 다른 글
[Groovy] 그루비 편리한 기능 (0) | 2016.02.26 |
---|---|
[Java] 날짜 비교 (0) | 2016.02.26 |
[SPRING] RestTemplate Timeout (0) | 2016.01.26 |
[Groovy] 클로저(Closure) 만들기 (0) | 2016.01.24 |
[Groovy] each 클로저 break하기 (0) | 2016.01.24 |