SAVIK

JacksonMappers.java

Jan 9th, 2022
1,187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1.  
  2. import com.fasterxml.jackson.core.JsonGenerator;
  3. import com.fasterxml.jackson.core.JsonProcessingException;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.fasterxml.jackson.databind.SerializerProvider;
  6. import com.fasterxml.jackson.databind.annotation.JsonSerialize;
  7. import com.fasterxml.jackson.databind.ser.std.StdSerializer;
  8. import com.google.common.collect.ImmutableList;
  9. import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
  10.  
  11. import java.io.IOException;
  12. import java.util.UUID;
  13.  
  14. public class JacksonMappers {
  15.  
  16.     private static final ObjectMapper mapper = new ObjectMapper();
  17.  
  18.  
  19.     public static void main(String[] args) throws JsonProcessingException {
  20.         ImmutableList<UUID> ids = ImmutableList.of(UUID.randomUUID(), UUID.randomUUID());
  21.         MyObject object = new MyObject("Some arbitrary value.", ids);
  22.         MyJacksonDto dto = new MyJacksonDto(object);
  23.         String json = mapper.writeValueAsString(dto);
  24.         System.out.println(json);
  25.     }
  26.  
  27.  
  28.     public static final class MySerializer extends StdSerializer<MyJacksonDto> {
  29.  
  30.         MySerializer() {
  31.             super(MyJacksonDto.class);
  32.         }
  33.  
  34.         @Override
  35.         public void serialize(MyJacksonDto value,
  36.                               JsonGenerator gen,
  37.                               SerializerProvider provider)
  38.                 throws IOException {
  39.             String flatJson = mapper.writeValueAsString(value.object);
  40.             gen.writeStartObject();
  41.             gen.writeObjectField("object", value.object);
  42.             gen.writeStringField("flatJson", flatJson);
  43.             gen.writeEndObject();
  44.         }
  45.     }
  46.  
  47.  
  48.     @JsonSerialize(using = MySerializer.class)
  49.     public static final class MyJacksonDto {
  50.  
  51.  
  52.         private final MyObject object;
  53.         private @MonotonicNonNull String flatJson;
  54.  
  55.         public MyJacksonDto(MyObject object) {
  56.             this.object = object;
  57.         }
  58.  
  59.         public String getFlatJson() {
  60.             return flatJson;
  61.         }
  62.  
  63.         public MyObject getObject() {
  64.             return object;
  65.         }
  66.     }
  67.  
  68.     public static final class MyObject {
  69.  
  70.         private final String value;
  71.         private final ImmutableList<UUID> ids;
  72.  
  73.         public MyObject(String value, ImmutableList<UUID> ids) {
  74.             this.value = value;
  75.             this.ids = ids;
  76.         }
  77.  
  78.         public ImmutableList<UUID> getIds() {
  79.             return ids;
  80.         }
  81.  
  82.         public String getValue() {
  83.             return value;
  84.         }
  85.     }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment