zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }
    
  • 相关阅读:
    BZOJ-2743: [HEOI2012]采花(树状数组 or TLE莫队)
    BZOJ-1122: [POI2008]账本BBB (单调栈神题)
    2017年10月18日23:54:18
    [校内自测 NOIP模拟题] chenzeyu97要请客(单调栈)
    BZOJ-1057: [ZJOI2007]棋盘制作(单调栈)
    [校内自测] 奶牛编号 (递推+智商)
    [校内自测] Incr (LIS+智商)
    BZOJ1486 [HNOI2009]最小圈
    BZOJ2400 Spoj 839 Optimal Marks
    BZOJ2595 [Wc2008]游览计划
  • 原文地址:https://www.cnblogs.com/ylty/p/13673445.html
Copyright © 2011-2022 走看看