GsonAgent/src/main/java/cn/citycraft/GsonAgent/GsonAgent.java

136 lines
4.1 KiB
Java

package cn.citycraft.GsonAgent;
import java.io.Reader;
import java.io.Writer;
import cn.citycraft.GsonAgent.api.Gson;
import cn.citycraft.GsonAgent.api.JsonArray;
import cn.citycraft.GsonAgent.api.JsonNull;
import cn.citycraft.GsonAgent.api.JsonObject;
import cn.citycraft.GsonAgent.api.JsonParser;
import cn.citycraft.GsonAgent.api.JsonPrimitive;
import cn.citycraft.GsonAgent.api.TypeAdapter;
import cn.citycraft.GsonAgent.api.stream.JsonReader;
import cn.citycraft.GsonAgent.api.stream.JsonWriter;
import cn.citycraft.GsonAgent.api.utils.Utils;
/**
* Minecraft Gson代理类(用于兼容1.7-1.8Gson类库)
* 复制修改于曲尘的Gson
*
* @since 2016年1月5日 下午12:51:30
* @author 喵♂呜,曲尘
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public class GsonAgent {
public static boolean newVersion = false;
private static String internalPackageName;
private static Class TypeAdapter;
static {
internalPackageName = "cn.citycraft.PluginHelper.gsonagent.";
try {
Thread.currentThread().getContextClassLoader().loadClass("com.google.gson.Gson");
newVersion = true;
internalPackageName += "normal.";
} catch (final Exception | Error e) {
internalPackageName += "nms.";
}
try {
TypeAdapter = getInternalClass("bind.TypeAdapter");
} catch (final Exception e) {
e.printStackTrace();
}
}
/**
* 初始化GsonAgent
*
* @return 根据新老版本获得不同的实例
*/
public static Gson newGson() {
if (newVersion) {
return new cn.citycraft.GsonAgent.normal.GsonHandle();
}
return new cn.citycraft.GsonAgent.nms.GsonHandle();
}
public static JsonArray newJsonArray() {
if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonArrayHandle();
}
return new cn.citycraft.GsonAgent.nms.JsonArrayHandle();
}
public static JsonNull newJsonNull() {
if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonNullHandle();
}
return new cn.citycraft.GsonAgent.nms.JsonNullHandle();
}
public static JsonObject newJsonObject() {
if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonObjectHandle();
}
return new cn.citycraft.GsonAgent.nms.JsonObjectHandle();
}
public static JsonParser newJsonParser() {
if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonParserHandle();
}
return new cn.citycraft.GsonAgent.normal.JsonParserHandle();
}
public static JsonPrimitive newJsonPrimitive(final Boolean value) {
if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonPrimitiveHandle(value);
}
return new cn.citycraft.GsonAgent.nms.JsonPrimitiveHandle(value);
}
public static JsonPrimitive newJsonPrimitive(final Character value) {
if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonPrimitiveHandle(value);
}
return new cn.citycraft.GsonAgent.nms.JsonPrimitiveHandle(value);
}
public static JsonPrimitive newJsonPrimitive(final Number value) {
if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonPrimitiveHandle(value);
}
return new cn.citycraft.GsonAgent.nms.JsonPrimitiveHandle(value);
}
public static JsonPrimitive newJsonPrimitive(final String value) {
if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonPrimitiveHandle(value);
}
return new cn.citycraft.GsonAgent.nms.JsonPrimitiveHandle(value);
}
public static JsonReader newJsonReader(final Reader in) {
if (newVersion) {
return new cn.citycraft.GsonAgent.normal.stream.JsonReaderHandle(in);
}
return new cn.citycraft.GsonAgent.nms.stream.JsonReaderHandle(in);
}
public static JsonWriter newJsonWriter(final Writer out) {
if (newVersion) {
return new cn.citycraft.GsonAgent.normal.stream.JsonWriterHandle(out);
}
return new cn.citycraft.GsonAgent.nms.stream.JsonWriterHandle(out);
}
public static TypeAdapter newTypeAdapter() {
return (TypeAdapter) Utils.newInstance(TypeAdapter);
}
private static Class<?> getInternalClass(final String className) throws ClassNotFoundException {
return Class.forName(internalPackageName + className + "Handle");
}
}