JsonUtils.java 5.83 KB
package com.diligrp.tax.central.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import tools.jackson.core.JacksonException;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.cfg.DateTimeFeature;
import tools.jackson.databind.cfg.EnumFeature;
import tools.jackson.databind.ext.javatime.deser.LocalDateDeserializer;
import tools.jackson.databind.ext.javatime.deser.LocalDateTimeDeserializer;
import tools.jackson.databind.ext.javatime.deser.LocalTimeDeserializer;
import tools.jackson.databind.ext.javatime.ser.LocalDateSerializer;
import tools.jackson.databind.ext.javatime.ser.LocalDateTimeSerializer;
import tools.jackson.databind.ext.javatime.ser.LocalTimeSerializer;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.module.SimpleModule;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

public class JsonUtils {
    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static final String DATE_FORMAT = "yyyy-MM-dd";

    private static final String TIME_FORMAT = "HH:mm:ss";


    private static final JsonMapper MAPPER = initJacksonMapper();

    private static JsonMapper initJacksonMapper() {
        var dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT);
        var dateFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
        var timeFormatter = DateTimeFormatter.ofPattern(TIME_FORMAT);
        LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(dateTimeFormatter);
        LocalDateSerializer localDateSerializer = new LocalDateSerializer(dateFormatter);
        LocalTimeSerializer localTimeSerializer = new LocalTimeSerializer(timeFormatter);

        LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer(dateTimeFormatter);
        LocalDateDeserializer localDateDeserializer = new LocalDateDeserializer(dateFormatter);
        LocalTimeDeserializer localTimeDeserializer = new LocalTimeDeserializer(timeFormatter);

        SimpleModule module = new SimpleModule();
        module.addSerializer(LocalDateTime.class, localDateTimeSerializer);
        module.addSerializer(LocalDate.class, localDateSerializer);
        module.addSerializer(LocalTime.class, localTimeSerializer);
        module.addDeserializer(LocalDateTime.class, localDateTimeDeserializer);
        module.addDeserializer(LocalDate.class, localDateDeserializer);
        module.addDeserializer(LocalTime.class, localTimeDeserializer);
        return JsonMapper.builder()
                .changeDefaultPropertyInclusion(incl -> incl.withValueInclusion(JsonInclude.Include.NON_NULL))
                .changeDefaultPropertyInclusion(incl -> incl.withContentInclusion(JsonInclude.Include.NON_NULL))
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                .disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
                .disable(EnumFeature.FAIL_ON_NUMBERS_FOR_ENUMS)
                .disable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
                .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
                .enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
                .enable(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)
                .defaultDateFormat(new SimpleDateFormat(DATE_TIME_FORMAT))
                .defaultTimeZone(TimeZone.getTimeZone(ZoneOffset.of("+8")))
                .addModule(module)
                .build();

    }

    public static <T> T fromJsonString(String json, Class<T> type) {
        try {
            return MAPPER.readValue(json, type);
        } catch (JacksonException ex) {
            throw new IllegalArgumentException("Deserialize json exception", ex);
        }
    }

    public static <T> T fromJsonString(String json, tools.jackson.core.type.TypeReference<T> jsonTypeReference) {
        try {
            return MAPPER.readValue(json, jsonTypeReference);
        } catch (JacksonException ex) {
            throw new IllegalArgumentException("Deserialize json array exception", ex);
        }
    }

    public static <T> T fromJsonString(String json, tools.jackson.databind.JavaType javaType) {
        try {
            return MAPPER.readValue(json, javaType);
        } catch (JacksonException ex) {
            throw new IllegalArgumentException("Deserialize json array exception", ex);
        }
    }

    public static <T> String toJsonString(T object) {
        try {
            return MAPPER.writeValueAsString(object);
        } catch (JacksonException ex) {
            throw new IllegalArgumentException("Serialize json exception", ex);
        }
    }

    public static <T> T convertValue(Object fromValue, Class<T> toValueType) {
        try {
            return MAPPER.convertValue(fromValue, toValueType);
        } catch (JacksonException ex) {
            throw new IllegalArgumentException("Convert value exception", ex);
        }
    }

    public static <T> T convertValue(Object fromValue, tools.jackson.core.type.TypeReference<T> toValueTypeRef) {
        try {
            return MAPPER.convertValue(fromValue, toValueTypeRef);
        } catch (JacksonException ex) {
            throw new IllegalArgumentException("Convert value exception", ex);
        }
    }

    public static <T> T convertValue(Object fromValue, tools.jackson.databind.JavaType toValueType) {
        try {
            return MAPPER.convertValue(fromValue, toValueType);
        } catch (JacksonException ex) {
            throw new IllegalArgumentException("Convert value exception", ex);
        }
    }

    public static <T> T deepCopy(T t, Class<T> cls) {
        return JsonUtils.fromJsonString(JsonUtils.toJsonString(t), cls);
    }

}