UpdateSerializer extends JsonSerializer<Update<?>> { @Override public void serialize(Update<?> update, JsonGenerator jsonGen, SerializerProvider serializerProvider) throws IOException { ObjectMapper mapper = (ObjectMapper) jsonGen.getCodec(); Map<String,Object> partial = mapper.convertValue(update.getPartial(), new TypeReference<Map<String,Object>>() { }); jsonGen.writeStartObject(); for (String field : update.getSetFields()) { jsonGen.writeObjectField(field, partial.get(field)); } for (String field : update.getRemoveFields()) { jsonGen.writeNullField(field); } jsonGen.writeEndObject(); }  @Override void serialize(Update<?> update, JsonGenerator jsonGen, SerializerProvider serializerProvider);  }
@Test public void serialize() throws Exception { Update<TestModel> update = Update.of(TestModel.builder() .email("test@example.com") .birthday(LocalDate.of(2017, 3, 1)) .name("Test") .build(), ImmutableSet.of("email", "birthday"), ImmutableSet.of("country")); ObjectMapper mapper = new ObjectMapper() .registerModule(new JavaTimeModule()); String json = mapper.writeValueAsString(update); assertEquals("{\"email\":\"test@example.com\",\"birthday\":\"2017-03-01\",\"country\":null}", json); }