package Gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializer;

import java.time.LocalDateTime;

public class GsonFormatter {
    private static final Gson gson = new GsonBuilder()
            .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer())
            .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeDeserializer())
            .create();

    public static String toJson(Object obj){
        return gson.toJson(obj);
    }

    public static <T> T fromJson(String json, Class<T> clazz){
        try {
            return gson.fromJson(json, clazz);
        } catch (Exception ignored) {
        }
        return null;
    }

    public static JsonObject toJsonObject(Object obj, Class<?> clazz){
        try {
            return gson.toJsonTree(obj, clazz).getAsJsonObject();
        } catch (Exception ignored) {
        }
        return null;
    }
}

