コード例 #1
0
 @Test
 public void testSettersWithNullValues() {
   // Should not crash:
   factory.setSerializers((JsonSerializer<?>[]) null);
   factory.setSerializersByType(null);
   factory.setDeserializersByType(null);
   factory.setFeaturesToEnable((Object[]) null);
   factory.setFeaturesToDisable((Object[]) null);
 }
コード例 #2
0
  @Test
  public void testCompleteSetup() {
    NopAnnotationIntrospector annotationIntrospector = NopAnnotationIntrospector.instance;
    ObjectMapper objectMapper = new ObjectMapper();

    assertTrue(this.factory.isSingleton());
    assertEquals(ObjectMapper.class, this.factory.getObjectType());

    Map<Class<?>, JsonDeserializer<?>> deserializers = new HashMap<Class<?>, JsonDeserializer<?>>();
    deserializers.put(Date.class, new DateDeserializer());

    factory.setObjectMapper(objectMapper);

    JsonSerializer serializer1 = new ClassSerializer();
    JsonSerializer serializer2 = new NumberSerializer();

    factory.setSerializers(serializer1);
    factory.setSerializersByType(
        Collections.<Class<?>, JsonSerializer<?>>singletonMap(Boolean.class, serializer2));
    factory.setDeserializersByType(deserializers);
    factory.setAnnotationIntrospector(annotationIntrospector);

    this.factory.setFeaturesToEnable(
        SerializationFeature.FAIL_ON_EMPTY_BEANS,
        DeserializationFeature.UNWRAP_ROOT_VALUE,
        JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER,
        JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);

    this.factory.setFeaturesToDisable(
        MapperFeature.AUTO_DETECT_GETTERS,
        MapperFeature.AUTO_DETECT_FIELDS,
        JsonParser.Feature.AUTO_CLOSE_SOURCE,
        JsonGenerator.Feature.QUOTE_FIELD_NAMES);

    assertFalse(getSerializerFactoryConfig(objectMapper).hasSerializers());
    assertFalse(getDeserializerFactoryConfig(objectMapper).hasDeserializers());

    this.factory.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    this.factory.afterPropertiesSet();

    assertTrue(objectMapper == this.factory.getObject());
    assertTrue(getSerializerFactoryConfig(objectMapper).hasSerializers());
    assertTrue(getDeserializerFactoryConfig(objectMapper).hasDeserializers());

    Serializers serializers =
        getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
    assertTrue(
        serializers.findSerializer(null, SimpleType.construct(Class.class), null) == serializer1);
    assertTrue(
        serializers.findSerializer(null, SimpleType.construct(Boolean.class), null) == serializer2);
    assertNull(serializers.findSerializer(null, SimpleType.construct(Number.class), null));

    assertTrue(
        annotationIntrospector
            == objectMapper.getSerializationConfig().getAnnotationIntrospector());
    assertTrue(
        annotationIntrospector
            == objectMapper.getDeserializationConfig().getAnnotationIntrospector());

    assertTrue(
        objectMapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
    assertTrue(
        objectMapper
            .getDeserializationConfig()
            .isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
    assertTrue(
        objectMapper
            .getFactory()
            .isEnabled(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER));
    assertTrue(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS));

    assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
    assertFalse(
        objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
    assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
    assertFalse(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES));
    assertTrue(
        objectMapper.getSerializationConfig().getSerializationInclusion()
            == JsonInclude.Include.NON_NULL);
  }