zoukankan      html  css  js  c++  java
  • spring 中json使用

    1.json序列化工具

    public class JsonUtils {
        /**
         * Logger for this class
         */
        private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);
    
        private final static ObjectMapper objectMapper = new ObjectMapper();
    
        static {
            objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
            objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
            objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
            objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
            objectMapper.configure(JsonParser.Feature.INTERN_FIELD_NAMES, true);
            objectMapper.configure(JsonParser.Feature.CANONICALIZE_FIELD_NAMES, true);
            objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        }
    
        private JsonUtils() {
        }
    
        public static String encode(Object obj) {
            try {
                return objectMapper.writeValueAsString(obj);
            } catch (JsonGenerationException e) {
                logger.error("encode(Object)", e); //$NON-NLS-1$
            } catch (JsonMappingException e) {
                logger.error("encode(Object)", e); //$NON-NLS-1$
            } catch (IOException e) {
                logger.error("encode(Object)", e); //$NON-NLS-1$
            }
            return null;
        }
    
        /**
         * 将json string反序列化成对象
         *
         * @param json
         * @param valueType
         * @return
         */
        public static <T> T decode(String json, Class<T> valueType) {
            try {
                return objectMapper.readValue(json, valueType);
            } catch (JsonParseException e) {
                logger.error("decode(String, Class<T>)", e);
            } catch (JsonMappingException e) {
                logger.error("decode(String, Class<T>)", e);
            } catch (IOException e) {
                logger.error("decode(String, Class<T>)", e);
            }
            return null;
        }
    
        /**
         * 将json array反序列化为对象
         *
         * @param json
         * @param jsonTypeReference
         * @return
         */
        @SuppressWarnings("unchecked")
        public static <T> T decode(String json, TypeReference<T> typeReference) {
            try {
                return (T) objectMapper.readValue(json, typeReference);
            } catch (JsonParseException e) {
                logger.error("decode(String, JsonTypeReference<T>)", e);
            } catch (JsonMappingException e) {
                logger.error("decode(String, JsonTypeReference<T>)", e);
            } catch (IOException e) {
                logger.error("decode(String, JsonTypeReference<T>)", e);
            }
            return null;
        }
    
    }

    2.springmvc中自定义JsonConvert

    @ControllerAdvice
    public class MyWebBindingInitializer implements WebBindingInitializer {
    
        @Autowired
        private MappingJackson2HttpMessageConverter jackson2HttpMessageConverter;
    
        @InitBinder
        @Override
        public void initBinder(WebDataBinder webDataBinder) {
            webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dateFormat.setLenient(false);
            webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    
            ObjectMapper objectMapper=jackson2HttpMessageConverter.getObjectMapper();
            objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            SimpleModule simpleModule = new SimpleModule();
            objectMapper.registerModule(simpleModule);
            objectMapper.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
        }
    
    }
  • 相关阅读:
    HDU2586 How far away?(tarjan的LCA)
    You Raise Me Up
    POJ2891 Strange Way to Express Integers(中国剩余定理)
    POJ2142 The Balance(扩展欧几里得)
    HDU 1166模仿大牛写的线段树
    NetWord Dinic
    HDU 1754 线段树裸题
    hdu1394 Minimum Inversion Number
    hdu2795 Billboard
    【完全版】线段树
  • 原文地址:https://www.cnblogs.com/z-test/p/10855238.html
Copyright © 2011-2022 走看看