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

cn.t.util.common.JsonUtil Maven / Gradle / Ivy

There is a newer version: 1.0.16.RELEASE
Show newest version
package cn.t.util.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class JsonUtil {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    static {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    }

    public static  T deserialize(String data, Class clazz) throws IOException {
        return objectMapper.readValue(data, clazz);
    }

    public static  T deserialize(String data, TypeReference tTypeReference) throws IOException {
        return objectMapper.readValue(data, tTypeReference);
    }

    public static String serialize(Object obj) throws JsonProcessingException {
        return objectMapper.writeValueAsString(obj);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy