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

cn.ocoop.framework.common.json.ObjectMapperCustomized Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
package cn.ocoop.framework.common.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

public class ObjectMapperCustomized extends com.fasterxml.jackson.databind.ObjectMapper {
    {
        DefaultSerializerProvider.Impl sp = new DefaultSerializerProvider.Impl();
        this.setSerializerProvider(sp);
        this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        this.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);

        SimpleModule module = new SimpleModule();

        module.addDeserializer(String.class, new StdDeserializer(String.class) {

            @Override
            public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
                String result = StringDeserializer.instance.deserialize(p, ctxt);
                if (StringUtils.isBlank(result)) return null;
                return result;
            }
        });

        registerModule(module);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy