public void testAnnotationIntrospectorCopyin() {
    ObjectMapper m = new ObjectMapper();
    m.setAnnotationIntrospector(new MyAnnotationIntrospector());
    assertEquals(
        MyAnnotationIntrospector.class,
        m.getDeserializationConfig().getAnnotationIntrospector().getClass());
    ObjectMapper m2 = m.copy();

    assertEquals(
        MyAnnotationIntrospector.class,
        m2.getDeserializationConfig().getAnnotationIntrospector().getClass());
    assertEquals(
        MyAnnotationIntrospector.class,
        m2.getSerializationConfig().getAnnotationIntrospector().getClass());
  }
  // [Issue#28]: ObjectMapper.copy()
  public void testCopy() throws Exception {
    ObjectMapper m = new ObjectMapper();
    assertTrue(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
    m.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
    InjectableValues inj = new InjectableValues.Std();
    m.setInjectableValues(inj);
    assertFalse(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
    m.enable(JsonParser.Feature.ALLOW_COMMENTS);
    assertTrue(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));

    // // First: verify that handling of features is decoupled:

    ObjectMapper m2 = m.copy();
    assertFalse(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
    m2.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    assertTrue(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
    assertSame(inj, m2.getInjectableValues());

    // but should NOT change the original
    assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));

    // nor vice versa:
    assertFalse(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
    assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
    m.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
    assertTrue(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
    assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));

    // // Also, underlying JsonFactory instances should be distinct

    assertNotSame(m.getFactory(), m2.getFactory());

    // [Issue#122]: Need to ensure mix-ins are not shared
    assertEquals(0, m.getSerializationConfig().mixInCount());
    assertEquals(0, m2.getSerializationConfig().mixInCount());
    assertEquals(0, m.getDeserializationConfig().mixInCount());
    assertEquals(0, m2.getDeserializationConfig().mixInCount());

    m.addMixIn(String.class, Integer.class);
    assertEquals(1, m.getSerializationConfig().mixInCount());
    assertEquals(0, m2.getSerializationConfig().mixInCount());
    assertEquals(1, m.getDeserializationConfig().mixInCount());
    assertEquals(0, m2.getDeserializationConfig().mixInCount());

    // [Issue#913]: Ensure JsonFactory Features copied
    assertTrue(m2.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
  }
 // [#438]: Support @JsonProperty.index
 public void testPropertyIndex() throws Exception {
   BeanDescription beanDesc =
       mapper.getDeserializationConfig().introspect(mapper.constructType(PropDescBean.class));
   _verifyProperty(beanDesc, false, true);
   beanDesc = mapper.getSerializationConfig().introspect(mapper.constructType(PropDescBean.class));
   _verifyProperty(beanDesc, false, true);
 }
 public void testJackson744() throws Exception {
   BeanDescription beanDesc =
       mapper.getDeserializationConfig().introspect(mapper.constructType(Issue744Bean.class));
   assertNotNull(beanDesc);
   AnnotatedMethod setter = beanDesc.findAnySetter();
   assertNotNull(setter);
 }
 // [#269]: Support new @JsonPropertyDescription
 public void testPropertyDesc() throws Exception {
   // start via deser
   BeanDescription beanDesc =
       mapper.getDeserializationConfig().introspect(mapper.constructType(PropDescBean.class));
   _verifyProperty(beanDesc, true, false);
   // and then via ser:
   beanDesc = mapper.getSerializationConfig().introspect(mapper.constructType(PropDescBean.class));
   _verifyProperty(beanDesc, true, false);
 }
 // for [JACKSON-701]
 public void testInnerClassWithAnnotationsInCreator() throws Exception {
   BasicBeanDescription beanDesc;
   // first with serialization
   beanDesc = mapper.getSerializationConfig().introspect(mapper.constructType(Issue701Bean.class));
   assertNotNull(beanDesc);
   // then with deserialization
   beanDesc =
       mapper.getDeserializationConfig().introspect(mapper.constructType(Issue701Bean.class));
   assertNotNull(beanDesc);
 }
 protected POJOPropertiesCollector collector(
     ObjectMapper m0, Class<?> cls, boolean forSerialization) {
   BasicClassIntrospector bci = new BasicClassIntrospector();
   // no real difference between serialization, deserialization, at least here
   if (forSerialization) {
     return bci.collectProperties(
         m0.getSerializationConfig(), m0.constructType(cls), null, true, "set");
   }
   return bci.collectProperties(
       m0.getDeserializationConfig(), m0.constructType(cls), null, false, "set");
 }
  // Test to ensure that we can check property ordering defaults...
  public void testConfigForPropertySorting() throws Exception {
    ObjectMapper m = new ObjectMapper();

    // sort-alphabetically is disabled by default:
    assertFalse(m.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY));
    SerializationConfig sc = m.getSerializationConfig();
    assertFalse(sc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY));
    assertFalse(sc.shouldSortPropertiesAlphabetically());
    DeserializationConfig dc = m.getDeserializationConfig();
    assertFalse(dc.shouldSortPropertiesAlphabetically());

    // but when enabled, should be visible:
    m.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
    sc = m.getSerializationConfig();
    assertTrue(sc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY));
    assertTrue(sc.shouldSortPropertiesAlphabetically());
    dc = m.getDeserializationConfig();
    // and not just via SerializationConfig, but also via DeserializationConfig
    assertTrue(dc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY));
    assertTrue(dc.shouldSortPropertiesAlphabetically());
  }
  @Test
  public void testBooleanSetters() {
    this.factory.setAutoDetectFields(false);
    this.factory.setAutoDetectGettersSetters(false);
    this.factory.setFailOnEmptyBeans(false);
    this.factory.setIndentOutput(true);
    this.factory.afterPropertiesSet();

    ObjectMapper objectMapper = this.factory.getObject();

    assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
    assertFalse(
        objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
    assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
    assertFalse(
        objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_SETTERS));
    assertFalse(
        objectMapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
    assertTrue(objectMapper.getSerializationConfig().isEnabled(SerializationFeature.INDENT_OUTPUT));
    assertTrue(
        objectMapper.getSerializationConfig().getSerializationInclusion()
            == JsonInclude.Include.ALWAYS);
  }
Пример #10
0
 public JacksonJaxbJsonProvider buildJacksonJaxbJsonProvider() {
   ObjectMapper mapper = new ObjectMapper();
   // mapper.enable(SerializationFeature.INDENT_OUTPUT);
   AnnotationIntrospector pair =
       AnnotationIntrospector.pair(
           new PrioritizeJSONPropertyJaxbAnnotationIntrospector(),
           new JacksonAnnotationIntrospector());
   mapper.getDeserializationConfig().with(pair);
   mapper.getSerializationConfig().with(pair);
   mapper.setAnnotationIntrospectors(pair, pair);
   // create JsonProvider to provide custom ObjectMapper
   ResteasyJackson2Provider provider = new ResteasyJackson2Provider();
   provider.setMapper(mapper);
   return provider;
 }
  @Override
  public String nameForDeserialization(final BeanPropertyDefinition beanProperty) {

    DeserializationConfig deserializationConfig = objectMapper.getDeserializationConfig();

    Optional<PropertyNamingStrategy> namingStrategy =
        Optional.fromNullable(deserializationConfig.getPropertyNamingStrategy());
    String newName =
        namingStrategy
            .transform(overTheWireName(beanProperty, deserializationConfig))
            .or(beanProperty.getName());

    LOG.debug("Name '{}' renamed to '{}'", beanProperty.getName(), newName);

    return newName;
  }
 public void testCustomPoolResolver() throws Exception {
   Map<Object, WithCustomResolution> pool = new HashMap<Object, WithCustomResolution>();
   pool.put(1, new WithCustomResolution(1, 1));
   pool.put(2, new WithCustomResolution(2, 2));
   pool.put(3, new WithCustomResolution(3, 3));
   pool.put(4, new WithCustomResolution(4, 4));
   pool.put(5, new WithCustomResolution(5, 5));
   ContextAttributes attrs =
       mapper.getDeserializationConfig().getAttributes().withSharedAttribute(POOL_KEY, pool);
   String content = "{\"data\":[1,2,3,4,5]}";
   CustomResolutionWrapper wrapper =
       mapper.reader(CustomResolutionWrapper.class).with(attrs).readValue(content);
   assertFalse(wrapper.data.isEmpty());
   for (WithCustomResolution ob : wrapper.data) {
     assertSame(pool.get(ob.id), ob);
   }
 }
  @Test
  public void testBasicParse() throws IOException {
    ObjectMapper mapper = new XmlMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    AnnotationIntrospector introspector = new JacksonXmlAnnotationIntrospector();
    mapper.getDeserializationConfig().withAppendedAnnotationIntrospector(introspector);

    CheckstyleWarning expected =
        expectedWarning(EXPECTED_PATH, 27, "First sentence should end with a period.", "warning");

    try (InputStreamReader inputStreamReader =
        new InputStreamReader(
            CheckstyleWarningGeneratorTest.class.getResourceAsStream("/checkstyle-result.xml"))) {
      CheckStyleReport report = mapper.readValue(inputStreamReader, CheckStyleReport.class);
      Set<CheckstyleWarning> warnings = checkstyleWarningGenerator.generateWarnings(commit, report);
      assertThat(warnings, contains(expected));
    }
  }
  public static ObjectMapper setupInjectablesInObjectMapper(ObjectMapper objectMapper) {
    objectMapper.registerModule(
        new SimpleModule("testModule").registerSubtypes(LocalLoadSpec.class));

    final GuiceAnnotationIntrospector guiceIntrospector = new GuiceAnnotationIntrospector();
    objectMapper.setAnnotationIntrospectors(
        new AnnotationIntrospectorPair(
            guiceIntrospector, objectMapper.getSerializationConfig().getAnnotationIntrospector()),
        new AnnotationIntrospectorPair(
            guiceIntrospector,
            objectMapper.getDeserializationConfig().getAnnotationIntrospector()));
    objectMapper.setInjectableValues(
        new GuiceInjectableValues(
            GuiceInjectors.makeStartupInjectorWithModules(
                ImmutableList.of(
                    new Module() {
                      @Override
                      public void configure(Binder binder) {
                        binder.bind(LocalDataSegmentPuller.class);
                      }
                    }))));
    return objectMapper;
  }
  @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);
  }