package Common;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Modifier;
public class JsonObject
{
private static Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.FINAL).create();
protected JsonObject()
{
try
{
this.getClass().getConstructor();
}
catch (Exception e)
{
throw new RuntimeException(this.getClass().getName() + " must have an empty default constructor to extend " + JsonObject.class.getName());
}
}
public String getName()
{
return getName(this.getClass());
}
public String toJsonString()
{
return gson.toJson(this);
}
public static void setDefaultGson(Gson gson)
{
JsonObject.gson = gson;
}
private static String getName(Class c)
{
return c.getSimpleName();
}
public static Gson getDefaultGson()
{
return gson;
}
public static T load(String directory, Class jsonClass) throws IOException
{
return load(new File(directory), jsonClass);
}
public static T load(File directory, Class jsonClass) throws IOException
{
return gson.fromJson(new FileReader(new File(directory, getName(jsonClass))), jsonClass);
}
}