当前位置:   article > 正文

com.google.gson.Gson的基本json代码工具类

com.google.gson

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.stream.JsonReader;

public class JsonUtils {

    private static Gson gson = new GsonBuilder().serializeNulls().create();
    private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);
    
    private static MflexConfigBean mflexConfigBean;
    
    private JsonUtils() {
    }

    /**
     * 将对象转换为Json字符串
     * 
     * @param <T>
     *            转换对象的类
     * @param 要转换的对象
     * @return 转换后的字符串
     */
    public static <T> String toJsonString(T json) {
        return gson.toJson(json);
    }
    
    /**
     * 将对象转换为Json字符串数组
     * 
     * @param <T>
     *            转换对象的类
     * @param 要转换的对象
     * @return 转换后的字符串
     */
    public static <T> String toJsonArray(T json) {
        return gson.toJsonTree(json).getAsJsonArray().toString();
    }

    public static <T> T fromJson(String jsonString, Class<T> classOfT) {
        return gson.fromJson(jsonString, classOfT);
    }

    public static <T> List<T> listFromJson(String jsonString, Class<T> classOfT) {
        List<T> list = new LinkedList<>();
        JsonArray jsonArray = parseString(jsonString).getAsJsonArray();
        for (JsonElement jsonElement : jsonArray) {
            list.add(gson.fromJson(jsonElement, classOfT));
        }
        return list;
    }

    private static JsonElement parseString(String jsonString) {
        JsonParser parser = new JsonParser();
        JsonReader reader = new JsonReader(new StringReader(jsonString));
        reader.setLenient(true);
        return parser.parse(reader);
    }
    
    public static MflexConfigBean getConfigBean() throws IOException {
        if (mflexConfigBean == null ) {
            String root = System.getProperty("user.dir");
            String readFileToString = FileUtils.readFileToString(new File(root, "application-mflex.json"), StandardCharsets.UTF_8);
            
            try {
                mflexConfigBean = gson.fromJson(readFileToString, MflexConfigBean.class);
            } catch (JsonSyntaxException e) {
                logger.error("json字符串转对象失败,被转换的json字符串为:{}, 失败信息:", readFileToString, e);
            }
        }
        
        return mflexConfigBean;
    }
    
    public static void main(String[] args) throws IOException {
//    	System.out.println(getConfigBean().toString());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/834175
推荐阅读
相关标签
  

闽ICP备14008679号