93 lines
3.3 KiB
Java
93 lines
3.3 KiB
Java
package com.myoa.engineering.crawl.ppomppu.support.util;
|
|
|
|
import com.fasterxml.jackson.core.JsonGenerator;
|
|
import com.fasterxml.jackson.core.JsonParser.Feature;
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
|
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
import java.io.IOException;
|
|
import org.springframework.cache.support.NullValue;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
/**
|
|
* ObjectMapperFactory
|
|
*
|
|
* @author Shin Woo-jin (woozu.shin@kakaoent.com)
|
|
* @since 2021-08-31
|
|
*/
|
|
public final class ObjectMapperFactory {
|
|
|
|
private ObjectMapperFactory() {
|
|
}
|
|
|
|
private static final ObjectMapper defaultMapper;
|
|
private static final ObjectMapper httpMapper;
|
|
|
|
static {
|
|
defaultMapper = initDefaultMapper();
|
|
httpMapper = initHttpMapper();
|
|
}
|
|
|
|
public static ObjectMapper defaultMapper() {
|
|
return defaultMapper;
|
|
}
|
|
|
|
public static ObjectMapper httpMapper() {
|
|
return httpMapper;
|
|
}
|
|
|
|
private static ObjectMapper initDefaultMapper() {
|
|
final ObjectMapper objectMapper = new ObjectMapper();
|
|
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
|
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
|
|
objectMapper.disable(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS);
|
|
objectMapper.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
|
|
objectMapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
|
|
objectMapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
|
|
|
objectMapper.registerModule(new JavaTimeModule());
|
|
return objectMapper;
|
|
}
|
|
|
|
private static ObjectMapper initHttpMapper() {
|
|
final ObjectMapper objectMapper = initDefaultMapper();
|
|
objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true);
|
|
return objectMapper;
|
|
}
|
|
|
|
|
|
/**
|
|
* Copy from {@link GenericJackson2JsonRedisSerializer.NullValueSerializer}.
|
|
*/
|
|
public static class NullValueSerializer extends StdSerializer<NullValue> {
|
|
|
|
private static final long serialVersionUID = 6776419544239897328L;
|
|
private final String classIdentifier;
|
|
|
|
/**
|
|
* @param classIdentifier can be {@literal null} and will be defaulted to {@code @class}.
|
|
*/
|
|
NullValueSerializer(String classIdentifier) {
|
|
super(NullValue.class);
|
|
this.classIdentifier = StringUtils.hasText(classIdentifier) ? classIdentifier : "@class";
|
|
}
|
|
|
|
/*
|
|
* (non-Javadoc)
|
|
* @see com.fasterxml.jackson.databind.ser.std.StdSerializer#serialize(java.lang.Object, com
|
|
* .fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider)
|
|
*/
|
|
@Override
|
|
public void serialize(NullValue value, JsonGenerator jgen, SerializerProvider provider)
|
|
throws IOException {
|
|
|
|
jgen.writeStartObject();
|
|
jgen.writeStringField(classIdentifier, NullValue.class.getName());
|
|
jgen.writeEndObject();
|
|
}
|
|
}
|
|
}
|