XIGsonUtils.java 727 B

1234567891011121314151617181920212223242526272829
  1. /*
  2. * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
  3. */
  4. package com.miniframe.tools;
  5. import com.google.gson.Gson;
  6. import com.google.gson.GsonBuilder;
  7. import com.google.gson.JsonParseException;
  8. import java.lang.reflect.Type;
  9. /**
  10. * Json工具类.
  11. */
  12. public class XIGsonUtils {
  13. private static Gson gson = new GsonBuilder().create();
  14. public static String toJson(Object value) {
  15. return gson.toJson(value);
  16. }
  17. public static <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException {
  18. return gson.fromJson(json, classOfT);
  19. }
  20. public static <T> T fromJson(String json, Type typeOfT) throws JsonParseException {
  21. return (T) gson.fromJson(json, typeOfT);
  22. }
  23. }