Java Code Examples for com.fasterxml.jackson.databind.ObjectMapper#setAnnotationIntrospector()

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#setAnnotationIntrospector() . 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: 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 3
Source File: MoreoverJsonActivitySerializer.java    From streams with Apache License 2.0 5 votes vote down vote up
@Override
public Activity deserialize(String serialized) {
  serialized = serialized.replaceAll("\\[[ ]*\\]", "null");

  LOGGER.debug(serialized);

  ObjectMapper mapper = new ObjectMapper();
  AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory());
  mapper.setAnnotationIntrospector(introspector);
  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE);
  mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, Boolean.FALSE);
  mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
  mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);
  mapper.configure(DeserializationFeature.WRAP_EXCEPTIONS, Boolean.TRUE);

  Article article;
  try {
    ObjectNode node = (ObjectNode)mapper.readTree(serialized);
    node.remove("tags");
    node.remove("locations");
    node.remove("companies");
    node.remove("topics");
    node.remove("media");
    node.remove("outboundUrls");
    ObjectNode jsonNodes = (ObjectNode) node.get("source").get("feed");
    jsonNodes.remove("editorialTopics");
    jsonNodes.remove("tags");
    jsonNodes.remove("autoTopics");
    article = mapper.convertValue(node, Article.class);
  } catch (IOException ex) {
    throw new IllegalArgumentException("Unable to deserialize", ex);
  }
  return MoreoverUtils.convert(article);
}
 
Example 4
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 5
Source File: UnmarshallingTester.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static ObjectMapper getObjectMapper() {
	ObjectMapper om = new ObjectMapper();
	JaxbAnnotationIntrospector jai = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
	om.setAnnotationIntrospector(jai);
	om.enable(SerializationFeature.INDENT_OUTPUT);
	return om;
}
 
Example 6
Source File: CertificateUnmarshallingTest.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static ObjectMapper getObjectMapper() {
	ObjectMapper om = new ObjectMapper();
	JaxbAnnotationIntrospector jai = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
	om.setAnnotationIntrospector(jai);
	om.enable(SerializationFeature.INDENT_OUTPUT);
	return om;
}
 
Example 7
Source File: CertificateProcessExecutorTest.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static ObjectMapper getObjectMapper() {
	ObjectMapper om = new ObjectMapper();
	JaxbAnnotationIntrospector jai = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
	om.setAnnotationIntrospector(jai);
	om.enable(SerializationFeature.INDENT_OUTPUT);
	return om;
}
 
Example 8
Source File: AbstractTestValidationExecutor.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static ObjectMapper getObjectMapper() {
	ObjectMapper om = new ObjectMapper();
	JaxbAnnotationIntrospector jai = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
	om.setAnnotationIntrospector(jai);
	om.enable(SerializationFeature.INDENT_OUTPUT);
	return om;
}
 
Example 9
Source File: IgnoranceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenIntrospection_whenIgnoringProperties_thenCorrect() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setAnnotationIntrospector(new IgnoranceIntrospector());

    String jsonDataString = instantiateAndSerializeObjects(mapper);

    assertThat(jsonDataString, containsString("make"));
    assertThat(jsonDataString, not(containsString("model")));
    assertThat(jsonDataString, not(containsString("seatingCapacity")));
    assertThat(jsonDataString, not(containsString("topSpeed")));
    assertThat(jsonDataString, not(containsString("towingCapacity")));
}
 
Example 10
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 11
Source File: Utility.java    From SNOMED-in-5-minutes with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the json for graph.
 *
 * @param object the object
 * @return the json for graph
 * @throws Exception the exception
 */
public static String getJsonForGraph(Object object) throws Exception {
  ObjectMapper mapper = new ObjectMapper();
  AnnotationIntrospector introspector =
      new JaxbAnnotationIntrospector(mapper.getTypeFactory());
  mapper.setAnnotationIntrospector(introspector);
  return mapper.writeValueAsString(object);
}
 
Example 12
Source File: Utility.java    From SNOMED-in-5-minutes with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the graph for json.
 *
 * @param <T> the generic type
 * @param json the json
 * @param graphClass the graph class
 * @return the graph for json
 * @throws Exception the exception
 */
public static <T> T getGraphForJson(String json, Class<T> graphClass)
  throws Exception {
  InputStream in =
      new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
  ObjectMapper mapper = new ObjectMapper();
  AnnotationIntrospector introspector =
      new JaxbAnnotationIntrospector(mapper.getTypeFactory());
  mapper.setAnnotationIntrospector(introspector);
  return mapper.readValue(in, graphClass);

}
 
Example 13
Source File: ConfigMain.java    From nifi-minifi with Apache License 2.0 5 votes vote down vote up
public static ConfigSchema transformVersionedFlowSnapshotToSchema(InputStream source) throws IOException {
    try {
        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(objectMapper.getTypeFactory()));
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        final VersionedFlowSnapshot versionedFlowSnapshot = objectMapper.readValue(source, VersionedFlowSnapshot.class);
        return transformVersionedFlowSnapshotToSchema(versionedFlowSnapshot);
    } finally {
        source.close();
    }
}
 
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: 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 16
Source File: SolrJacksonAnnotationInspector.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public static ObjectMapper createObjectMapper(){
  ObjectMapper mapper = new ObjectMapper();
  mapper.setAnnotationIntrospector(INSTANCE);
  return mapper;
}
 
Example 17
Source File: AbstractDynamicConfigurationFactoryTest.java    From conf4j with MIT License 4 votes vote down vote up
private ObjectMapper getObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setAnnotationIntrospector(new InternalAwareJacksonAnnotationIntrospector());
    return objectMapper;
}
 
Example 18
Source File: ObjectMapperResolver.java    From nifi with Apache License 2.0 4 votes vote down vote up
public ObjectMapperResolver() throws Exception {
    mapper = new ObjectMapper();
    mapper.setDefaultPropertyInclusion(Value.construct(Include.NON_NULL, Include.ALWAYS));
    mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(mapper.getTypeFactory()));
}
 
Example 19
Source File: JacksonLombokAnnotationIntrospectorTest.java    From jackson-lombok with MIT License 4 votes vote down vote up
@Before
public void setUp() {
    mapperWithExtention = new ObjectMapper();
    mapperWithExtention.setAnnotationIntrospector(new JacksonLombokAnnotationIntrospector());
}
 
Example 20
Source File: Utility.java    From SNOMED-in-5-minutes with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the graph for json. sample usage:
 * 
 * <pre>
 *   List&lt;ConceptJpa&gt; list = ConfigUtility.getGraphForJson(str, new TypeReference&lt;List&lt;ConceptJpa&gt;&gt;{});
 * </pre>
 * @param <T> the
 * @param json the json
 * @param typeRef the type ref
 * @return the graph for json
 * @throws Exception the exception
 */
public static <T> T getGraphForJson(String json, TypeReference<T> typeRef)
  throws Exception {
  InputStream in =
      new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
  ObjectMapper mapper = new ObjectMapper();
  AnnotationIntrospector introspector =
      new JaxbAnnotationIntrospector(mapper.getTypeFactory());
  mapper.setAnnotationIntrospector(introspector);
  return mapper.readValue(in, typeRef);

}