ExplicitlySetFilter extends SimpleBeanPropertyFilter { @Override public void serializeAsField( Object pojo, JsonGenerator jgen, SerializerProvider provider, PropertyWriter writer) throws Exception { if (include(writer)) { Field field = getMatchingDeclaredField(pojo.getClass(), writer.getName()); boolean accessible = field.isAccessible(); try { field.setAccessible(true); Object fieldValue = field.get(pojo); if (fieldValue != null) { writer.serializeAsField(pojo, jgen, provider); } else { Field explicitField = pojo.getClass().getDeclaredField(FIELD_NAME); boolean explicitAccessible = explicitField.isAccessible(); try { explicitField.setAccessible(true); Set<String> explicitlySet = (Set<String>) explicitField.get(pojo); if (explicitlySet.contains(writer.getName())) { writer.serializeAsField(pojo, jgen, provider); } } finally { explicitField.setAccessible(explicitAccessible); } } } finally { field.setAccessible(accessible); } } else if (!jgen.canOmitFields()) { writer.serializeAsOmittedField(pojo, jgen, provider); } } private  ExplicitlySetFilter(); @Override void serializeAsField(
            Object pojo, JsonGenerator jgen, SerializerProvider provider, PropertyWriter writer); static final ExplicitlySetFilter INSTANCE; static final String NAME; static final String FIELD_NAME; }
@Test public void testFieldInSuperclass() throws Exception { Subclass pojo = Subclass.builder().baseVal(1).subVal("two").build(); JsonGenerator jgen = mock(JsonGenerator.class); SerializerProvider provider = mock(SerializerProvider.class); PropertyWriter writer = mock(PropertyWriter.class); when(writer.getName()).thenReturn("baseVal"); ExplicitlySetFilter.INSTANCE.serializeAsField(pojo, jgen, provider, writer); verify(writer, atLeast(1)).getName(); verify(writer).serializeAsField(pojo, jgen, provider); verifyNoMoreInteractions(writer); }
@Test public void testFieldInSubclass() throws Exception { Subclass pojo = Subclass.builder().baseVal(1).subVal("two").build(); JsonGenerator jgen = mock(JsonGenerator.class); SerializerProvider provider = mock(SerializerProvider.class); PropertyWriter writer = mock(PropertyWriter.class); when(writer.getName()).thenReturn("subVal"); ExplicitlySetFilter.INSTANCE.serializeAsField(pojo, jgen, provider, writer); verify(writer, atLeast(1)).getName(); verify(writer).serializeAsField(pojo, jgen, provider); verifyNoMoreInteractions(writer); }
@Test public void testExplicitNullFieldInSuperclass() throws Exception { Subclass pojo = Subclass.builder().baseVal(null).subVal("two").build(); JsonGenerator jgen = mock(JsonGenerator.class); SerializerProvider provider = mock(SerializerProvider.class); PropertyWriter writer = mock(PropertyWriter.class); when(writer.getName()).thenReturn("baseVal"); ExplicitlySetFilter.INSTANCE.serializeAsField(pojo, jgen, provider, writer); verify(writer, atLeast(1)).getName(); verify(writer).serializeAsField(pojo, jgen, provider); verifyNoMoreInteractions(writer); }
@Test public void testExplicitNullInSubclass() throws Exception { Subclass pojo = Subclass.builder().baseVal(1).subVal(null).build(); JsonGenerator jgen = mock(JsonGenerator.class); SerializerProvider provider = mock(SerializerProvider.class); PropertyWriter writer = mock(PropertyWriter.class); when(writer.getName()).thenReturn("subVal"); ExplicitlySetFilter.INSTANCE.serializeAsField(pojo, jgen, provider, writer); verify(writer, atLeast(1)).getName(); verify(writer).serializeAsField(pojo, jgen, provider); verifyNoMoreInteractions(writer); }
@Test public void testNullFieldInSuperclass() throws Exception { Subclass pojo = Subclass.builder().subVal("two").build(); JsonGenerator jgen = mock(JsonGenerator.class); SerializerProvider provider = mock(SerializerProvider.class); PropertyWriter writer = mock(PropertyWriter.class); when(writer.getName()).thenReturn("baseVal"); ExplicitlySetFilter.INSTANCE.serializeAsField(pojo, jgen, provider, writer); verify(writer, atLeast(1)).getName(); verifyNoMoreInteractions(writer); }
@Test public void testNullInSubclass() throws Exception { Subclass pojo = Subclass.builder().baseVal(1).build(); JsonGenerator jgen = mock(JsonGenerator.class); SerializerProvider provider = mock(SerializerProvider.class); PropertyWriter writer = mock(PropertyWriter.class); when(writer.getName()).thenReturn("subVal"); ExplicitlySetFilter.INSTANCE.serializeAsField(pojo, jgen, provider, writer); verify(writer, atLeast(1)).getName(); verifyNoMoreInteractions(writer); }