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

cn.ocoop.framework.common.json.CommonModule 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.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
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.std.StdSerializer;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

public class CommonModule extends SimpleModule {
    private static JsonSerializer longSerializer = new StdSerializer(Long.class) {
        @Override
        public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            if (value == null) {
                gen.writeNull();
                return;
            }

            gen.writeString(String.valueOf(value));
        }
    };

    {
        addSerializer(Long.TYPE, longSerializer)
                .addSerializer(Long.class, longSerializer)
                .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 StringUtils.trim(result);
                    }
                });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy