malys
3/4/2014 - 7:54 AM

Jackson

Jackson

/*
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);