JsonUtils.java 644 B

123456789101112131415161718192021222324
  1. package nju.seec.helper.util;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. import lombok.experimental.UtilityClass;
  5. import nju.seec.helper.util.serializer.LocalDateTimeSerializer;
  6. import java.time.LocalDateTime;
  7. /**
  8. * @author cst
  9. */
  10. @UtilityClass
  11. public class JsonUtils {
  12. private static final Gson GSON = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).create();
  13. public String toJson(Object o) {
  14. return GSON.toJson(o);
  15. }
  16. public <T> T fromJson(String json, Class<T> classOfT) {
  17. return GSON.fromJson(json, classOfT);
  18. }
  19. }