[+] Abstract mapper

matching
Azalea 2024-12-26 21:49:51 -05:00
parent 18554ec439
commit b421b4476b
3 changed files with 57 additions and 63 deletions

View File

@ -1,61 +0,0 @@
package icu.samnyan.aqua.sega.util.jackson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
/**
* @author samnyan (privateamusement@protonmail.com)
*/
@Component
public class BasicMapper {
private final ObjectMapper mapper;
public BasicMapper() {
mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(SerializationFeature.WRITE_ENUMS_USING_INDEX, true);
SimpleModule module = new SimpleModule();
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.0")));
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.0")));
mapper.registerModule(module);
}
public String write(Object o) throws JsonProcessingException {
return mapper.writeValueAsString(o);
}
public <T> T read(String jsonStr, Class<T> toClass) throws JsonProcessingException {
return mapper.readValue(jsonStr, toClass);
}
public <T> T read(String jsonStr, TypeReference<T> toValueTypeRef) throws JsonProcessingException {
return mapper.readValue(jsonStr, toValueTypeRef);
}
public <T> T convert(Object map, Class<T> toClass) {
return mapper.convertValue(map, toClass);
}
public <T> T convert(Object map, TypeReference<T> toValueTypeRef) {
return mapper.convertValue(map, toValueTypeRef);
}
public LinkedHashMap<String, Object> toMap(Object object) {
return mapper.convertValue(object, new TypeReference<>() {
});
}
}

View File

@ -0,0 +1,55 @@
package icu.samnyan.aqua.sega.util.jackson
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.springframework.stereotype.Component
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
interface IMapper {
fun write(o: Any?): String
}
@Component
class BasicMapper: IMapper {
companion object {
val BASIC_MAPPER = jacksonObjectMapper().apply {
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
configure(SerializationFeature.WRITE_ENUMS_USING_INDEX, true)
findAndRegisterModules()
registerModule(SimpleModule().apply {
addSerializer(
LocalDateTime::class.java,
LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.0"))
)
addDeserializer(
LocalDateTime::class.java,
LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.0"))
)
})
}
}
override fun write(o: Any?) =
BASIC_MAPPER.writeValueAsString(o)
fun <T> read(jsonStr: String?, toClass: Class<T>?) =
BASIC_MAPPER.readValue(jsonStr, toClass)
fun <T> read(jsonStr: String?, toValueTypeRef: TypeReference<T>?) =
BASIC_MAPPER.readValue(jsonStr, toValueTypeRef)
fun <T> convert(map: Any?, toClass: Class<T>?) =
BASIC_MAPPER.convertValue(map, toClass)
fun <T> convert(map: Any?, toValueTypeRef: TypeReference<T>?) =
BASIC_MAPPER.convertValue(map, toValueTypeRef)
fun toMap(obj: Any?): LinkedHashMap<String, Any?> =
BASIC_MAPPER.convertValue(obj, object : TypeReference<LinkedHashMap<String, Any?>>() {})
}

View File

@ -17,8 +17,8 @@ import java.time.format.DateTimeFormatter
@Component
class StringMapper {
fun write(o: Any?) = STRING_MAPPER.writeValueAsString(o)
class StringMapper: IMapper {
override fun write(o: Any?) = STRING_MAPPER.writeValueAsString(o)
fun <T> convert(map: Any?, toClass: Class<T>?) = STRING_MAPPER.convertValue(map, toClass)
final inline fun <reified T> convert(map: Any?) = convert(map, T::class.java)
fun toMap(obj: Any?) = STRING_MAPPER.convertValue(obj, object : TypeReference<LinkedHashMap<String, Any>>() {})