122 lines
4.2 KiB
Java
122 lines
4.2 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.stream.JsonReader;
|
|
import cn.citycraft.GsonAgent.api.stream.JsonWriter;
|
|
|
|
/**
|
|
* Minecraft Gson代理类(用于兼容1.7-1.8Gson类库)
|
|
* 复制修改于尘曲的Gson
|
|
*
|
|
* @since 2016年1月5日 下午12:51:30
|
|
* @author 喵♂呜,尘曲
|
|
*/
|
|
public class GsonAgent {
|
|
public static boolean newVersion = false;
|
|
|
|
static {
|
|
try {
|
|
Thread.currentThread().getContextClassLoader().loadClass("com.google.gson.Gson");
|
|
newVersion = true;
|
|
} catch (final Exception | Error e) {
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 初始化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);
|
|
}
|
|
// FIXME ###会造成java.lang.StackOverflowError###
|
|
// public static <T> TypeAdapter newTypeAdapter(final TypeAdapter<T> typeAdapter) {
|
|
// if (newVersion) {
|
|
// return new cn.citycraft.GsonAgent.normal.bind.TypeAdapterHandle((com.google.gson.TypeAdapter) typeAdapter);
|
|
// }
|
|
// return new cn.citycraft.GsonAgent.nms.bind.TypeAdapterHandle((net.minecraft.util.com.google.gson.TypeAdapter) typeAdapter);
|
|
// }
|
|
}
|