fuint-application/application-web/src/main/java/com/fuint/application/web/backend/util/JSONUtil.java

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

package com.fuint.application.web.backend.util;


import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.StringWriter;
import java.text.SimpleDateFormat;

/**
 * JsonUtil
 * 提供Json和对象之间的转换。
 * Site https://www.fuint.cn
 */
public class JSONUtil {

    /**
     * 把对象转换成Json字符串。
     * @param obj 需要转换的对象。
     * @return 转换好的字符串。如果出错会抛出RuntimeException
     */
    public static String toJSonString(Object obj){
        ObjectMapper mapper = new ObjectMapper();
        StringWriter out = new StringWriter();

        try {
            JsonGenerator gen = new JsonFactory().createJsonGenerator(out);
            gen.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            mapper.writeValue(gen, obj);
            gen.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return out.toString();
    }

    /**
     * 把Json字符串转换成对象
     * @param <T> 所要转换的对象类型
     * @param json Json字符串
     * @param clazz 转换对象有类型
     * @return 转换好的对象,如果出错会抛出RuntimeException
     */
    public static <T> T toObject(String json,Class<T> clazz){
        ObjectMapper mapper = new ObjectMapper();
        T t = null;
        try {
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            t = mapper.readValue(json, clazz);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return t;
    }

    /**
     * 把对象转换成Json字符串。
     * @param obj 需要转换的对象。
     * @return 转换好的字符串。如果出错会抛出RuntimeException
后面还有 36 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。