Jackson通用工具类

    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.11.1'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.1'
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

@Slf4j
public class Json {

    private static ObjectMapper MAPPER;

    static {
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(ZonedDateTime.class, new JsonSerializer<zoneddatetime>() {

            @Override
            public void serialize(ZonedDateTime value, JsonGenerator gen, SerializerProvider serializers)
                    throws IOException {
                gen.writeNumber(value.toInstant().toEpochMilli());
            }
        });
        javaTimeModule.addDeserializer(ZonedDateTime.class, new JsonDeserializer<zoneddatetime>() {
            @Override
            public ZonedDateTime deserialize(JsonParser p, DeserializationContext ctxt)
                    throws IOException, JsonProcessingException {
                return ZonedDateTime.ofInstant(Instant.ofEpochMilli(p.readValueAs(Long.class)), ZoneId.systemDefault());
            }
        });
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        objectMapper.registerModule(javaTimeModule);
        MAPPER = objectMapper;
    }

    public static ObjectMapper getMapper() {
        return MAPPER;
    }

    @SneakyThrows
    public static String toJson(Object object) {
        return MAPPER.writeValueAsString(object);
    }

    @SneakyThrows
    public static <t> T toObject(String json, Class<t> clazz) {
        return MAPPER.readValue(json, clazz);
    }

    public static <t> T toObject(Object object, Class<t> clazz) {
        return MAPPER.convertValue(object, clazz);
    }

    @SneakyThrows
    public static <t> T toObject(String json, TypeReference<t> type) {
        return MAPPER.readValue(json, type);
    }

    public static <t> T toObject(Object object, TypeReference<t> type) {
        return MAPPER.convertValue(object, type);
    }

}
</t></t></t></t></t></t></t></t></zoneddatetime></zoneddatetime>

Original: https://www.cnblogs.com/ylty/p/13673445.html
Author: 风小雅
Title: Jackson通用工具类

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/575152/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球