package com.sjkj.base_lib.net.utils;

import com.google.gson.Gson;

import java.lang.reflect.Type;

/**
 * Class news as Serializer/Deserializer for entities.
 */
public class JsonSerializer {

    private final Gson gson = new Gson();

    public JsonSerializer() {
    }

    /**
     * Serialize an object to Json.
     *
     * @param obj {@link Object} to serialize.
     */
    public String serialize(Object obj) {
        String jsonString = gson.toJson(obj, obj.getClass());
        return jsonString;
    }

    /**
     * Deserialize a json representation of an object.
     *
     * @param jsonString A json string to deserialize.
     * @return {@link T}
     */
//  public News deserialize(String jsonString) {
//    News news = gson.fromJson(jsonString, News.class);
//    return news;
//  }
    public <T> T deserialize(String jsonString, Class<T> tClass) {
        T news = gson.fromJson(jsonString, tClass);
        return news;
    }

    public <T> T deserialize(String jsonString, Type type) {

        T news = gson.fromJson(jsonString, type);
        return news;
    }
}
