All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.serialization.JSON.DefaultNCacheMapper Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package com.alachisoft.ncache.serialization.JSON;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;

/**
 * This class handles parsing of JSON. It generates the same JSON as being generated in .NET
 */
class DefaultNCacheMapper extends ObjectMapper {
    private static final String TYPE_PROPERTY = "$type"; // this is the same attribute name as it occurs in .NET

    public DefaultNCacheMapper(boolean isTypeEnabled) {
        super();
        // if an attribute is missing during deserialization, no exception will be thrown and the attribute will be left null
        this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        // adds type to serialization
        if(isTypeEnabled) {
            // this custom type resolver is being written to match JSON between Java and .NET
            StdTypeResolverBuilder typer = new NCacheTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_FINAL)
                    .init(JsonTypeInfo.Id.CUSTOM, null)
                    .inclusion(JsonTypeInfo.As.PROPERTY)
                    .typeProperty(TYPE_PROPERTY);
            this.setDefaultTyping(typer);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy