com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair Java Examples

The following examples show how to use com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ObjectMapperModule.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
public ObjectMapper get()
{
    ObjectMapper mapper = objectMapper;
    if (mapper == null) {
        final GuiceAnnotationIntrospector guiceIntrospector = new GuiceAnnotationIntrospector();
        AnnotationIntrospector defaultAI = new JacksonAnnotationIntrospector();
        MapperBuilder<?,?> builder = JsonMapper.builder()
                .injectableValues(new GuiceInjectableValues(injector))
                .annotationIntrospector(new AnnotationIntrospectorPair(guiceIntrospector, defaultAI))
                .addModules(modulesToAdd);
        for (Provider<? extends Module> provider : providedModules) {
            builder = builder.addModule(provider.get());
        }
        mapper = builder.build();

      /*
  } else {
        // 05-Feb-2017, tatu: _Should_ be fine, considering instances are now (3.0) truly immutable.
      //    But if this turns out to be problematic, may need to consider addition of `copy()`
      //    back in databind
      mapper = mapper.copy();
      */
  }
  return mapper;
}
 
Example #2
Source File: TestJaxbAutoDetect.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
public void testJaxbAnnotatedObject() throws Exception
{
    AnnotationIntrospector primary = new JaxbAnnotationIntrospector();
    AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
    AnnotationIntrospector pair = new AnnotationIntrospectorPair(primary, secondary);
    ObjectMapper mapper = objectMapperBuilder()
            .annotationIntrospector(pair)
            .build();

    JaxbAnnotatedObject original = new JaxbAnnotatedObject("123");
    
    String json = mapper.writeValueAsString(original);
    assertFalse("numberString field in JSON", json.contains("numberString")); // kinda hack-y :)
    JaxbAnnotatedObject result = mapper.readValue(json, JaxbAnnotatedObject.class);
    assertEquals(new BigDecimal("123"), result.number);
}
 
Example #3
Source File: TestUnwrapping.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
public void testXmlElementAndXmlElementRefs() throws Exception
    {
        Bean<A> bean = new Bean<A>();
        bean.r = new A(12);
        bean.name = "test";
        AnnotationIntrospector pair = new AnnotationIntrospectorPair(
                new JacksonAnnotationIntrospector(),
                new JaxbAnnotationIntrospector());
        ObjectMapper mapper = objectMapperBuilder()
                .annotationIntrospector(pair)
                .build();
            
//            mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector());
            // mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector());

        String json = mapper.writeValueAsString(bean);
        // !!! TODO: verify
        assertNotNull(json);
    }
 
Example #4
Source File: TestIntrospectorPair.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
public void testSimpleIgnore() throws Exception
{
    // first: only Jackson introspector (default)
    ObjectMapper mapper = new ObjectMapper();
    Map<String,Object> result = writeAndMap(mapper, new IgnoreBean());
    assertEquals(2, result.size());
    assertEquals("abc", result.get("text"));
    assertEquals(Boolean.TRUE, result.get("any"));

    // Then JAXB only
    mapper = objectMapperBuilder()
            .annotationIntrospector(_jaxbAI)
            .build();

    // jackson one should have priority
    result = writeAndMap(mapper, new IgnoreBean());
    assertEquals(2, result.size());
    assertEquals(Integer.valueOf(13), result.get("number"));
    assertEquals(Boolean.TRUE, result.get("any"));

    // then both, Jackson first
    mapper = objectMapperBuilder()
            .annotationIntrospector(new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI))
            .build();

    result = writeAndMap(mapper, new IgnoreBean());
    assertEquals(1, result.size());
    assertEquals(Boolean.TRUE, result.get("any"));

    // then both, JAXB first
    mapper = objectMapperBuilder()
            .annotationIntrospector(new AnnotationIntrospectorPair(_jaxbAI, _jacksonAI))
            .build();

    result = writeAndMap(mapper, new IgnoreBean());
    assertEquals(1, result.size());
    assertEquals(Boolean.TRUE, result.get("any"));
}
 
Example #5
Source File: TestIntrospectorPair.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
public void testSimpleFieldIgnore() throws Exception
{
    ObjectMapper mapper;

    // first: only Jackson introspector (default)
    mapper = new ObjectMapper();
    Map<String,Object> result = writeAndMap(mapper, new IgnoreFieldBean());
    assertEquals(2, result.size());
    assertEquals("123", result.get("text"));
    assertEquals(Boolean.TRUE, result.get("any"));

    // Then JAXB only
    mapper = objectMapperBuilder()
            .annotationIntrospector(_jaxbAI)
            .build();

    // jackson one should have priority
    result = writeAndMap(mapper, new IgnoreFieldBean());
    assertEquals(2, result.size());
    assertEquals(Integer.valueOf(7), result.get("number"));
    assertEquals(Boolean.TRUE, result.get("any"));

    // then both, Jackson first
    mapper = objectMapperBuilder()
            .annotationIntrospector(new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI))
            .build();

    result = writeAndMap(mapper, new IgnoreFieldBean());
    assertEquals(1, result.size());
    assertEquals(Boolean.TRUE, result.get("any"));

    // then both, JAXB first
    mapper = objectMapperBuilder()
            .annotationIntrospector(new AnnotationIntrospectorPair(_jaxbAI, _jacksonAI))
            .build();

    result = writeAndMap(mapper, new IgnoreFieldBean());
    assertEquals(1, result.size());
    assertEquals(Boolean.TRUE, result.get("any"));
}
 
Example #6
Source File: TestIntrospectorPair.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
/**
 * Test that will just use Jackson annotations, but did trigger [JACKSON-495] due to a bug
 * in JAXB annotation introspector.
 */
public void testIssue495() throws Exception
{
    ObjectMapper mapper = objectMapperBuilder()
            .annotationIntrospector(new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI))
            .build();
    CreatorBean bean = mapper.readValue("{\"name\":\"foo\"}", CreatorBean.class);
    assertNotNull(bean);
}
 
Example #7
Source File: BaseJaxbTest.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
protected MapperBuilder<?,?> getJaxbAndJacksonMapperBuilder()
{
    return JsonMapper.builder()
            .annotationIntrospector(new AnnotationIntrospectorPair(
                    new JaxbAnnotationIntrospector(),
                    new JacksonAnnotationIntrospector()));
}
 
Example #8
Source File: JsonUtils.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a configured mapper supporting JAXB.
 * @see #createObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)
 */
public static ObjectMapper createJaxbMapper() {
    ObjectMapper om = createObjectMapper(createObjectMapper());
    JaxbAnnotationIntrospector jaxbIntr = new JaxbAnnotationIntrospector(om.getTypeFactory());
    JacksonAnnotationIntrospector jsonIntr = new JacksonAnnotationIntrospector();
    om.setAnnotationIntrospector(new AnnotationIntrospectorPair(jsonIntr, jaxbIntr));
    return om;
}
 
Example #9
Source File: BaseSettings.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public BaseSettings withInsertedAnnotationIntrospector(AnnotationIntrospector ai) {
    return withAnnotationIntrospector(AnnotationIntrospectorPair.create(ai, _annotationIntrospector));
}
 
Example #10
Source File: BaseSettings.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public BaseSettings withAppendedAnnotationIntrospector(AnnotationIntrospector ai) {
    return withAnnotationIntrospector(AnnotationIntrospectorPair.create(_annotationIntrospector, ai));
}
 
Example #11
Source File: BaseJaxbTest.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
protected MapperBuilder<?,?> getJacksonAndJaxbMapperBuilder()
{
    return JsonMapper.builder()
            .annotationIntrospector(new AnnotationIntrospectorPair(new JacksonAnnotationIntrospector(),
                    new JaxbAnnotationIntrospector()));
}