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

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#setDefaultTyping() . 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: JacksonUtil.java    From archie with Apache License 2.0 6 votes vote down vote up
/**
 * Configure an existing object mapper to work with Archie RM and AOM Objects.
 * Indentation is enabled. Feel free to disable again in your own code.
 * @param objectMapper
 */
public static void configureObjectMapper(ObjectMapper objectMapper) {
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    objectMapper.enable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
    objectMapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
    objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    objectMapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    //objectMapper.

    objectMapper.registerModule(new JavaTimeModule());


    TypeResolverBuilder typeResolverBuilder = new ArchieTypeResolverBuilder()
            .init(JsonTypeInfo.Id.NAME, new OpenEHRTypeNaming())
            .typeProperty("@type")
            .typeIdVisibility(true)
            .inclusion(JsonTypeInfo.As.PROPERTY);

    objectMapper.setDefaultTyping(typeResolverBuilder);
}
 
Example 2
Source File: NsObjectTransducer.java    From components with Apache License 2.0 6 votes vote down vote up
/**
 * Creates instance of transducer using given NetSuite client.
 *
 * @param clientService client to be used
 */
protected NsObjectTransducer(NetSuiteClientService<?> clientService) {
    this.clientService = clientService;

    try {
        datatypeFactory = DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException e) {
        throw new ComponentException(e);
    }

    objectMapper = new ObjectMapper();

    // Customize typing of JSON objects.
    objectMapper.setDefaultTyping(new NsTypeResolverBuilder(clientService.getBasicMetaData()));

    // Register JAXB annotation module to perform mapping of data model objects to/from JSON.
    JaxbAnnotationModule jaxbAnnotationModule = new JaxbAnnotationModule();
    objectMapper.registerModule(jaxbAnnotationModule);

    setMetaDataSource(clientService.getMetaDataSource());
}
 
Example 3
Source File: JsonJacksonCodec.java    From redisson with Apache License 2.0 5 votes vote down vote up
protected void initTypeInclusion(ObjectMapper mapObjectMapper) {
    TypeResolverBuilder<?> mapTyper = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL) {
        public boolean useForType(JavaType t) {
            switch (_appliesFor) {
            case NON_CONCRETE_AND_ARRAYS:
                while (t.isArrayType()) {
                    t = t.getContentType();
                }
                // fall through
            case OBJECT_AND_NON_CONCRETE:
                return (t.getRawClass() == Object.class) || !t.isConcrete();
            case NON_FINAL:
                while (t.isArrayType()) {
                    t = t.getContentType();
                }
                // to fix problem with wrong long to int conversion
                if (t.getRawClass() == Long.class) {
                    return true;
                }
                if (t.getRawClass() == XMLGregorianCalendar.class) {
                    return false;
                }
                return !t.isFinal(); // includes Object.class
            default:
                // case JAVA_LANG_OBJECT:
                return t.getRawClass() == Object.class;
            }
        }
    };
    mapTyper.init(JsonTypeInfo.Id.CLASS, null);
    mapTyper.inclusion(JsonTypeInfo.As.PROPERTY);
    mapObjectMapper.setDefaultTyping(mapTyper);
}
 
Example 4
Source File: MarshallerService.java    From dawnsci with Eclipse Public License 1.0 4 votes vote down vote up
private ObjectMapper createRegisteredClassMapper() throws InstantiationException, IllegalAccessException, ClassRegistryDuplicateIdException, CoreException {
	ObjectMapper mapper = createJacksonMapper();
	mapper.setDefaultTyping(createRegisteredTypeIdResolver());
	return mapper;
}