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

com.gccloud.starter.common.json.JSON Maven / Gradle / Ivy

package com.gccloud.starter.common.json;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
import com.gccloud.starter.common.entity.ExtendObj;
import com.gccloud.starter.common.entity.SysUserEntity;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

/**
 * json工具类
 */
@Slf4j
public class JSON {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    static {
        OBJECT_MAPPER.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        OBJECT_MAPPER.registerModule(new JsonOrgModule());

        // 解决 ExtendObj 作为属性无法解析
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(ExtendObj.class, ExtendObjSerializer.INSTANCE);
        simpleModule.addDeserializer(ExtendObj.class, ExtendObjDeserializer.INSTANCE);
        OBJECT_MAPPER.registerModule(simpleModule);

        OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        OBJECT_MAPPER.setLocale(Locale.CHINA);
    }

    private JSON() {
    }

    /**
     * 获取jackson操作对象,当该类不满足需求时,可调用原生api进行使用
     *
     * @return
     */
    public static ObjectMapper getInstance() {
        return OBJECT_MAPPER;
    }

    @SneakyThrows
    public static  T parseObject(String text, Class clazz) {
        return OBJECT_MAPPER.readValue(text, clazz);
    }

    @SneakyThrows
    public static  T parseObject(JSONObject jsonObject, Class clazz) {
        return OBJECT_MAPPER.readValue(jsonObject.toString(), clazz);
    }

    @SneakyThrows
    public static  T parseObject(String text, Type type) {
        JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructType(type);
        return OBJECT_MAPPER.readValue(text, javaType);
    }

    @SneakyThrows
    public static JSONObject parseObject(String text) {
        return OBJECT_MAPPER.readValue(text, JSONObject.class);
    }

    @SneakyThrows
    public static JSONArray parseArray(String text) {
        return OBJECT_MAPPER.readValue(text, JSONArray.class);
    }

    @SneakyThrows
    public static  List parseArray(String text, Class clazz) {
        CollectionType listType = OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz);
        return OBJECT_MAPPER.readValue(text, listType);
    }

    @SneakyThrows
    public static String toJSONString(Object object) {
        return OBJECT_MAPPER.writeValueAsString(object);
    }

    public static void main(String[] args) {
        SysUserEntity usr = new SysUserEntity();
        System.out.println(JSON.toJSONString(usr));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy