Advertisement
NLinker

Jackson deserializer of the inner field

Nov 21st, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1.  
  2.     public static class ModelToString extends JsonSerializer<Model> {
  3.         @Override
  4.         public void serialize(Model value,
  5.                               JsonGenerator gen,
  6.                               SerializerProvider serializers) throws IOException, JsonProcessingException {
  7.             System.out.println("value = " + value);
  8.             gen.writeStartObject();
  9.             gen.writeFieldName("rate1");
  10.             gen.writeNumber(value.rate);
  11.             gen.writeFieldName("count1");
  12.             gen.writeNumber(value.count);
  13.             gen.writeFieldName("content1");
  14.             gen.writeObject(value.content);
  15.             gen.writeEndObject();
  16.         }
  17.     }
  18.  
  19.     public static class ModelFromString<T> extends JsonDeserializer<Model<T>> {
  20.         @Override
  21.         public Model<T> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
  22.             Model<T> m = new Model<>();
  23.             String t1 = p.nextFieldName();
  24.             p.nextToken();
  25.             m.rate = p.getDoubleValue();
  26.             String t3 = p.nextFieldName();
  27.             p.nextToken();
  28.             m.count = p.getIntValue();
  29.             String t5 = p.nextFieldName();
  30.             p.nextToken();
  31.             m.content = ctxt.readValue(p, (Class<T>) Content.class);
  32.             return m;
  33.         }
  34.     }
  35.  
  36.     public static class Content {
  37.         @JsonSerialize(using = LocalDateTimeSerializerToString.class)
  38.         @JsonDeserialize(using = LocalDateTimeDeserializerFromString.class)
  39.         public LocalDateTime time;
  40.  
  41.         public Content() {
  42.         }
  43.     }
  44.  
  45.     public static class Model<T> {
  46.         public Double rate;
  47.         public Integer count;
  48.         public T content;
  49.         public Model() {
  50.         }
  51.     }
  52.  
  53.     public static class My {
  54.         @JsonSerialize(using = ModelToString.class)
  55.         @JsonDeserialize(using = ModelFromString.class)
  56.         Model model;
  57.  
  58.         public Model getModel() {
  59.             return model;
  60.         }
  61.         public void setModel(Model rate) {
  62.             this.model = rate;
  63.         }
  64.     }
  65.  
  66.     public static void main(String[] args) throws Exception {
  67.         ObjectMapper om = new ObjectMapper();
  68.         om.registerModule(new Jdk8Module());
  69.         om.registerModule(new ParameterNamesModule());
  70.         DeserializationConfig newConfig = om.getDeserializationConfig()
  71.             .with();
  72.         om.setConfig(newConfig);
  73.         My my = new My();
  74.         Model<Content> model = new Model<>();
  75.         model.rate = 2.55;
  76.         model.count = 42;
  77.         Content content = new Content();
  78.         content.time = LocalDateTime.now();
  79.         model.content = content;
  80.         my.setModel(model);
  81.         String str = om.writeValueAsString(my);
  82.         System.out.println("str = " + str);
  83.         My my2 = om.readValue(str, My.class);
  84.         System.out.println("my2 = " + my2);
  85.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement