All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.stalary.easydoc.util.JsonUtils Maven / Gradle / Ivy

/**
 * @(#)JsonUtils.java, 2021-07-16.
 *
 * Copyright 2021 Youdao, Inc. All rights reserved.
 * YOUDAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.stalary.easydoc.util;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

/**
 * JsonUtils
 *
 * @author lirongqian
 * @since 2021/07/16
 */
@Slf4j
public class JsonUtils {

    private static ObjectMapper objectMapper;

    static {
        init();
    }

    public static void init() {
        objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    }

    public static String toString(Object obj) {
        if (obj == null) {
            return null;
        }
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            log.error("JSON 序列化错误", e);
        }
        return null;
    }

    public static  T parse(String json, TypeReference typeReference) {
        if (json == null) {
            return null;
        }
        try {
            return objectMapper.readValue(json, typeReference);
        } catch (Exception e) {
            log.error("JSON 解析错误", e);
        }
        return null;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy