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

The following examples show how to use com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector. 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: ObjectMapperUtil.java    From endpoints-java with Apache License 2.0 7 votes vote down vote up
/**
 * Creates an Endpoints standard object mapper that allows unquoted field names and unknown
 * properties.
 *
 * Note on unknown properties: When Apiary FE supports a strict mode where properties
 * are checked against the schema, BE can just ignore unknown properties.  This way, FE does
 * not need to filter out everything that the BE doesn't understand.  Before that's done,
 * a property name with a typo in it, for example, will just be ignored by the BE.
 */
public static ObjectMapper createStandardObjectMapper(ApiSerializationConfig config) {
  ObjectMapper objectMapper = new ObjectMapper()
      .configure(JsonParser.Feature.ALLOW_COMMENTS, true)
      .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
      .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
      .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
      .setBase64Variant(Base64Variants.MODIFIED_FOR_URL)
      .setSerializerFactory(
          BeanSerializerFactory.instance.withSerializerModifier(new DeepEmptyCheckingModifier()));
  AnnotationIntrospector pair = EndpointsFlag.JSON_USE_JACKSON_ANNOTATIONS.isEnabled()
      ? AnnotationIntrospector.pair(
          new ApiAnnotationIntrospector(config),
          new JacksonAnnotationIntrospector())
      : new ApiAnnotationIntrospector(config);
  objectMapper.setAnnotationIntrospector(pair);
  return objectMapper;
}
 
Example #2
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 #3
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 #4
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 #5
Source File: JsonUtils.java    From attic-rave with Apache License 2.0 5 votes vote down vote up
private static ObjectMapper getMapper() {
    ObjectMapper jacksonMapper = new ObjectMapper();
    AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
    jacksonMapper.setAnnotationIntrospector(primary);
    jacksonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    jacksonMapper.registerModule(new MrBeanModule());
    return jacksonMapper;
}
 
Example #6
Source File: FirebaseConfiguration.java    From spring-boot-starter-data-firebase with MIT License 5 votes vote down vote up
@Bean
public ObjectMapper firebaseObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
        @Override
        public boolean hasIgnoreMarker(AnnotatedMember m) {
            return _findAnnotation(m, FirebaseId.class) != null;
        }
    });
    return objectMapper;
}
 
Example #7
Source File: Jackson2.java    From oxd with Apache License 2.0 5 votes vote down vote up
public static ObjectMapper jsonMapper() {
    final AnnotationIntrospector jackson = new JacksonAnnotationIntrospector();

    final ObjectMapper mapper = new ObjectMapper();
    final DeserializationConfig deserializationConfig = mapper.getDeserializationConfig().with(jackson);
    final SerializationConfig serializationConfig = mapper.getSerializationConfig().with(jackson);
    if (deserializationConfig != null && serializationConfig != null) {
        // do nothing for now
    }
    return mapper;
}
 
Example #8
Source File: Util.java    From oxAuth with MIT License 5 votes vote down vote up
public static ObjectMapper createJsonMapper() {
    final AnnotationIntrospector jaxb = new JaxbAnnotationIntrospector();
    final AnnotationIntrospector jackson = new JacksonAnnotationIntrospector();

    final AnnotationIntrospector pair = AnnotationIntrospector.pair(jackson, jaxb);

    final ObjectMapper mapper = new ObjectMapper();
    mapper.getDeserializationConfig().with(pair);
    mapper.getSerializationConfig().with(pair);
    return mapper;
}
 
Example #9
Source File: ServerUtil.java    From oxAuth with MIT License 5 votes vote down vote up
public static ObjectMapper createJsonMapper() {
    final AnnotationIntrospector jaxb = new JaxbAnnotationIntrospector();
    final AnnotationIntrospector jackson = new JacksonAnnotationIntrospector();

    final AnnotationIntrospector pair = AnnotationIntrospector.pair(jackson, jaxb);

    final ObjectMapper mapper = new ObjectMapper();
    mapper.getDeserializationConfig().with(pair);
    mapper.getSerializationConfig().with(pair);
    return mapper;
}
 
Example #10
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 #11
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 #12
Source File: ObjectMapperProvider.java    From hermes with Apache License 2.0 5 votes vote down vote up
private static ObjectMapper createDefaultMapper() {
	AnnotationIntrospector jacksonIntrospector = new JacksonAnnotationIntrospector();
	ObjectMapper result = new ObjectMapper();
	result.configure(SerializationFeature.INDENT_OUTPUT, true);
	result.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
	result.getDeserializationConfig().withInsertedAnnotationIntrospector(jacksonIntrospector);
	result.getSerializationConfig().withInsertedAnnotationIntrospector(jacksonIntrospector);
	return result;
}
 
Example #13
Source File: ConjureParser.java    From conjure with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static ObjectMapper createConjureParserObjectMapper() {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory())
            .registerModule(new Jdk8Module())
            .setAnnotationIntrospector(AnnotationIntrospector.pair(
                    new KebabCaseEnforcingAnnotationInspector(), // needs to come first.
                    new JacksonAnnotationIntrospector()));
    mapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
    return mapper;
}
 
Example #14
Source File: ShopifySdkObjectMapper.java    From shopify-sdk with Apache License 2.0 5 votes vote down vote up
public static ObjectMapper buildMapper() {
	final ObjectMapper mapper = new ObjectMapper();
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

	final AnnotationIntrospector pair = AnnotationIntrospector.pair(
			new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()), new JacksonAnnotationIntrospector());
	mapper.setAnnotationIntrospector(pair);

	mapper.enable(MapperFeature.USE_ANNOTATIONS);
	return mapper;
}
 
Example #15
Source File: JacksonContextResolver.java    From datawave with Apache License 2.0 5 votes vote down vote up
public JacksonContextResolver() {
    mapper = new ObjectMapper();
    mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
    mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(new JacksonAnnotationIntrospector(),
                    new JaxbAnnotationIntrospector(mapper.getTypeFactory())));
    mapper.setSerializationInclusion(Include.NON_NULL);
}
 
Example #16
Source File: QueryResponseMessageJsonEncoder.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Override
public void init(EndpointConfig config) {
    mapper = new ObjectMapper();
    mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
    mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(new JacksonAnnotationIntrospector(),
                    new JaxbAnnotationIntrospector(mapper.getTypeFactory())));
    // Don't close the output stream
    mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    // Don't include NULL properties.
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
 
Example #17
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()));
}