Last active
November 30, 2017 10:20
-
-
Save malys/b85bd76c15d459ea2825 to your computer and use it in GitHub Desktop.
[Jackson Tips] #java
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
/* | |
From https://stackoverflow.com/questions/12046786/jackson-json-custom-serialization-for-certain-fields | |
http://www.mkyong.com/java/jackson-streaming-api-to-read-and-write-json/ | |
*/ | |
//Field Serialization | |
public class Person { | |
public String name; | |
public int age; | |
@JsonSerialize(using = IntToStringSerializer.class, as=String.class) | |
public int favoriteNumber: | |
} | |
public class IntToStringSerializer extends JsonSerializer<Integer> { | |
@Override | |
public void serialize(Integer tmpInt, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { | |
jsonGenerator.writeObject(tmpInt.toString()); | |
} | |
} | |
//Map Deserialization | |
TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>() {}; | |
HashMap<String,Object> o = mapper.readValue(from, typeRef); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment