NpmUntypedObjectDeserializerSerializer extends UntypedObjectDeserializerSerializer { @Override @Nullable public Object deserialize(final JsonParser parser, final DeserializationContext context) throws IOException { String fieldName = parser.getCurrentName(); for (NpmFieldMatcher matcher : matchers) { if (matcher.matches(parser) && matcher.allowDeserializationOnMatched()) { return matcher.getDeserializer().deserialize(fieldName, defaultValueDeserialize(parser, context), parser, context, generator); } } return defaultDeserialize(fieldName, parser, context); } NpmUntypedObjectDeserializerSerializer(final JsonGenerator generator,
                                                final List<NpmFieldMatcher> matchers); @Override @Nullable Object deserialize(final JsonParser parser, final DeserializationContext context);  }
@Test public void serializes_During_Deserialization() throws IOException { Object deserializedValue = underTest.deserialize(parser, context); assertThat(deserializedValue, equalTo(FIELD_VALUE)); verify(generator).writeFieldName(eq(FIELD_NAME)); verify(generator).writeObject(eq(FIELD_VALUE)); }
@Test public void deserialize_With_Matchers() throws IOException { when(parser.getCurrentName()).thenReturn(FIELD_NAME); when(fieldMatcher.matches(parser)).thenReturn(true); fieldMatchers.add(fieldMatcher); underTest.deserialize(parser, context); verify(fieldMatcher).matches(parser); verify(fieldMatcher).getDeserializer(); verify(fieldMatcher).allowDeserializationOnMatched(); verify(fieldDeserializer).deserialize(eq(FIELD_NAME), eq(FIELD_VALUE), eq(parser), eq(context), eq(generator)); }
@Test public void deserialize_With_Matchers_Prevented_If_Not_Allowed() throws IOException { when(parser.getCurrentName()).thenReturn(FIELD_NAME); when(fieldMatcher.matches(parser)).thenReturn(true); when(fieldMatcher.allowDeserializationOnMatched()).thenReturn(false); fieldMatchers.add(fieldMatcher); underTest.deserialize(parser, context); verify(fieldMatcher).matches(parser); verify(fieldMatcher).allowDeserializationOnMatched(); verify(fieldMatcher, never()).getDeserializer(); verify(fieldDeserializer, never()).deserialize(eq(FIELD_NAME), eq(FIELD_VALUE), eq(parser), eq(context), eq(generator)); }