1
0
mirror of https://e.coding.net/circlecloud/GsonAgent.git synced 2024-11-24 02:08:48 +00:00

首次提交...

This commit is contained in:
502647092 2016-01-06 11:13:42 +08:00
commit 2dc3901c06
51 changed files with 3662 additions and 0 deletions

25
.classpath Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

38
.gitignore vendored Normal file
View File

@ -0,0 +1,38 @@
# Eclipse stuff
/.settings
# netbeans
/nbproject
# we use maven!
/build.xml
# maven
/target
/repo
# vim
.*.sw[a-p]
# various other potential build files
/build
/bin
/dist
/manifest.mf
/world
# Mac filesystem dust
*.DS_Store
# intellij
*.iml
*.ipr
*.iws
.idea/
# Project Stuff
/src/main/resources/Soulbound
# Atlassian Stuff
/atlassian-ide-plugin.xml

23
.project Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>GsonAgent</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

BIN
lib/craftbukkit-1.7.10.jar Normal file

Binary file not shown.

56
pom.xml Normal file
View File

@ -0,0 +1,56 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.citycraft</groupId>
<artifactId>GsonAgent</artifactId>
<version>1.0</version>
<name>GsonAgent</name>
<build>
<finalName>${project.name}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>citycraft-repo</id>
<url>${jenkins.url}/plugin/repository/everything/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<type>jar</type>
<version>1.8.8-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.7.10</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/craftbukkit-1.7.10.jar</systemPath>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jenkins.url>http://ci.citycraft.cn:8080</jenkins.url>
</properties>
</project>

View File

@ -0,0 +1,135 @@
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");
}
}

View File

@ -0,0 +1,76 @@
package cn.citycraft.GsonAgent.api;
import java.io.Reader;
import java.lang.reflect.Type;
import cn.citycraft.GsonAgent.api.exception.JsonIOException;
import cn.citycraft.GsonAgent.api.exception.JsonSyntaxException;
import cn.citycraft.GsonAgent.api.reflect.TypeToken;
import cn.citycraft.GsonAgent.api.stream.JsonReader;
import cn.citycraft.GsonAgent.api.stream.JsonWriter;
/**
* Gson代理API
* 复制修改于曲尘的Gson
*
* @since 2016年1月5日 下午12:48:43
* @author
*/
public interface Gson {
public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException;
public <T> T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException;
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException;
public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException, JsonIOException;
public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException;
/**
* @param result
* Json字符串
* @param clazz
* 模型类
* @return
*/
public <T> T fromJson(final String result, Class<T> clazz) throws JsonSyntaxException;
public <T> T fromJson(String json, Type typeOfT) throws JsonSyntaxException;
public <T> TypeAdapter<T> getAdapter(Class<T> type);
public <T> TypeAdapter<T> getAdapter(TypeToken<T> type);
public <T> TypeAdapter<T> getDelegateAdapter(TypeAdapterFactory skipPast, TypeToken<T> type);
public String toJson(JsonElement jsonElement);
public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOException;
public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOException;
/**
* 对象序列化成Json
*
* @param src
* 对象
* @return 序列化后的对象
*/
public String toJson(Object src);
public void toJson(Object src, Appendable writer) throws JsonIOException;
public String toJson(Object src, Type typeOfSrc);
public void toJson(Object src, Type typeOfSrc, Appendable writer) throws JsonIOException;
public void toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOException;
public JsonElement toJsonTree(Object src);
public JsonElement toJsonTree(Object src, Type typeOfSrc);
@Override
public String toString();
}

View File

@ -0,0 +1,69 @@
package cn.citycraft.GsonAgent.api;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator;
/**
* JsonArray代理API
*
* @since 2016年1月5日 下午1:13:27
* @author
*/
public abstract interface JsonArray extends JsonElement, Iterable<JsonElement> {
public void add(JsonElement element);
public void addAll(JsonArray array);
@Override
public JsonArray deepCopy();
@Override
public boolean equals(Object o);
public JsonElement get(int i);
@Override
public BigDecimal getAsBigDecimal();
@Override
public BigInteger getAsBigInteger();
@Override
public boolean getAsBoolean();
@Override
public byte getAsByte();
@Override
public char getAsCharacter();
@Override
public double getAsDouble();
@Override
public float getAsFloat();
@Override
public int getAsInt();
@Override
public long getAsLong();
@Override
public Number getAsNumber();
@Override
public short getAsShort();
@Override
public String getAsString();
@Override
public int hashCode();
@Override
public Iterator<JsonElement> iterator();
public int size();
}

View File

@ -0,0 +1,56 @@
package cn.citycraft.GsonAgent.api;
import java.math.BigDecimal;
import java.math.BigInteger;
/**
*
* @since 2016年1月5日 下午1:12:35
* @author
*/
public abstract interface JsonElement {
public JsonElement deepCopy();
public BigDecimal getAsBigDecimal();
public BigInteger getAsBigInteger();
public boolean getAsBoolean();
public byte getAsByte();
public char getAsCharacter();
public double getAsDouble();
public float getAsFloat();
public int getAsInt();
public JsonArray getAsJsonArray();
public JsonNull getAsJsonNull();
public JsonObject getAsJsonObject();
public JsonPrimitive getAsJsonPrimitive();
public long getAsLong();
public Number getAsNumber();
public short getAsShort();
public String getAsString();
public boolean isJsonArray();
public boolean isJsonNull();
public boolean isJsonObject();
public boolean isJsonPrimitive();
@Override
public String toString();
}

View File

@ -0,0 +1,17 @@
package cn.citycraft.GsonAgent.api;
/**
*
* @since 2016年1月5日 下午1:13:43
* @author
*/
public abstract interface JsonNull extends JsonElement {
@Override
public JsonNull deepCopy();
@Override
public boolean equals(Object other);
@Override
public int hashCode();
}

View File

@ -0,0 +1,44 @@
package cn.citycraft.GsonAgent.api;
import java.util.Map;
import java.util.Set;
/**
*
* @since 2016年1月5日 下午1:13:20
* @author
*/
public abstract interface JsonObject extends JsonElement {
public void add(String property, JsonElement value);
public void addProperty(String property, Boolean value);
public void addProperty(String property, Character value);
public void addProperty(String property, Number value);
public void addProperty(String property, String value);
@Override
public JsonObject deepCopy();
public Set<Map.Entry<String, JsonElement>> entrySet();
@Override
public boolean equals(Object o);
public JsonElement get(String memberName);
public JsonArray getAsJsonArray(String memberName);
public JsonObject getAsJsonObject(String memberName);
public JsonPrimitive getAsJsonPrimitive(String memberName);
public boolean has(String memberName);
@Override
public int hashCode();
public JsonElement remove(String property);
}

View File

@ -0,0 +1,19 @@
package cn.citycraft.GsonAgent.api;
import java.io.Reader;
import cn.citycraft.GsonAgent.api.exception.JsonIOException;
import cn.citycraft.GsonAgent.api.exception.JsonSyntaxException;
import cn.citycraft.GsonAgent.api.stream.JsonReader;
import cn.citycraft.GsonAgent.api.utils.HandleInterface;
@SuppressWarnings("rawtypes")
public abstract interface JsonParser extends HandleInterface {
public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException;
public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException;
public JsonElement parse(String json) throws JsonSyntaxException;
}

View File

@ -0,0 +1,64 @@
package cn.citycraft.GsonAgent.api;
import java.math.BigDecimal;
import java.math.BigInteger;
/**
*
* @since 2016年1月5日 下午1:13:35
* @author
*/
public abstract interface JsonPrimitive extends JsonElement {
@Override
public JsonPrimitive deepCopy();
@Override
public boolean equals(Object obj);
@Override
public BigDecimal getAsBigDecimal();
@Override
public BigInteger getAsBigInteger();
@Override
public boolean getAsBoolean();
@Override
public byte getAsByte();
@Override
public char getAsCharacter();
@Override
public double getAsDouble();
@Override
public float getAsFloat();
@Override
public int getAsInt();
@Override
public long getAsLong();
@Override
public Number getAsNumber();
@Override
public short getAsShort();
@Override
public String getAsString();
@Override
public int hashCode();
public boolean isBoolean();
public boolean isNumber();
public boolean isString();
void setValue(Object primitive);
}

View File

@ -0,0 +1,31 @@
package cn.citycraft.GsonAgent.api;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import cn.citycraft.GsonAgent.api.stream.JsonReader;
import cn.citycraft.GsonAgent.api.stream.JsonWriter;
import cn.citycraft.GsonAgent.api.utils.HandleInterface;
@SuppressWarnings("rawtypes")
public abstract interface TypeAdapter<T> extends HandleInterface {
public T fromJson(Reader in) throws IOException;
public T fromJson(String json) throws IOException;
public T fromJsonTree(JsonElement jsonTree);
public TypeAdapter<T> nullSafe();
public abstract T read(JsonReader paramJsonReader) throws IOException;
public String toJson(T value) throws IOException;
public void toJson(Writer out, T value) throws IOException;
public JsonElement toJsonTree(T value);
public void write(JsonWriter paramJsonWriter, T paramT) throws IOException;
}

View File

@ -0,0 +1,8 @@
package cn.citycraft.GsonAgent.api;
import cn.citycraft.GsonAgent.api.reflect.TypeToken;
public abstract interface TypeAdapterFactory {
public abstract <T> TypeAdapter<T> create(Gson paramGson, TypeToken<T> paramTypeToken);
}

View File

@ -0,0 +1,33 @@
package cn.citycraft.GsonAgent.api.bind;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import cn.citycraft.GsonAgent.api.JsonElement;
import cn.citycraft.GsonAgent.api.stream.JsonReader;
import cn.citycraft.GsonAgent.api.stream.JsonWriter;
import cn.citycraft.GsonAgent.api.utils.HandleInterface;
@SuppressWarnings("rawtypes")
public abstract interface TypeAdapter<T> extends HandleInterface {
public T fromJson(Reader in) throws IOException;
public T fromJson(String json) throws IOException;
public T fromJsonTree(JsonElement jsonTree);
public TypeAdapter<T> nullSafe();
public abstract T read(JsonReader paramJsonReader) throws IOException;
public String toJson(T value) throws IOException;
public void toJson(Writer out, T value) throws IOException;
public JsonElement toJsonTree(T value);
public void write(JsonWriter paramJsonWriter, T paramT) throws IOException;
}

View File

@ -0,0 +1,8 @@
package cn.citycraft.GsonAgent.api.bind;
import cn.citycraft.GsonAgent.api.Gson;
import cn.citycraft.GsonAgent.api.reflect.TypeToken;
public abstract interface TypeAdapterFactory {
public abstract <T> TypeAdapter<T> create(Gson paramGson, TypeToken<T> paramTypeToken);
}

View File

@ -0,0 +1,18 @@
package cn.citycraft.GsonAgent.api.exception;
public final class JsonIOException extends JsonParseException {
private static final long serialVersionUID = 1L;
public JsonIOException(String msg) {
super(msg);
}
public JsonIOException(String msg, Throwable cause) {
super(msg, cause);
}
public JsonIOException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,19 @@
package cn.citycraft.GsonAgent.api.exception;
public class JsonParseException extends RuntimeException {
static final long serialVersionUID = -4086729973971783390L;
public JsonParseException(String msg) {
super(msg);
}
public JsonParseException(String msg, Throwable cause) {
super(msg, cause);
}
public JsonParseException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,18 @@
package cn.citycraft.GsonAgent.api.exception;
public final class JsonSyntaxException extends JsonParseException {
private static final long serialVersionUID = 1L;
public JsonSyntaxException(String msg) {
super(msg);
}
public JsonSyntaxException(String msg, Throwable cause) {
super(msg, cause);
}
public JsonSyntaxException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,80 @@
package cn.citycraft.GsonAgent.api.reflect;
import java.lang.reflect.Type;
import cn.citycraft.GsonAgent.GsonAgent;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class TypeToken<T> {
private final TypeTokenAbstract<T> internal;
public TypeToken() {
if (GsonAgent.newVersion) {
this.internal = new cn.citycraft.GsonAgent.normal.reflect.TypeTokenHandle<T>();
} else {
this.internal = new cn.citycraft.GsonAgent.nms.reflect.TypeTokenHandle<T>();
}
}
protected TypeToken(final TypeTokenAbstract<T> internal) {
this.internal = internal;
}
public static <T> TypeToken<T> get(final Class<T> type) {
if (GsonAgent.newVersion) {
return new TypeToken(cn.citycraft.GsonAgent.normal.reflect.TypeTokenHandle.get(type));
}
return new TypeToken(cn.citycraft.GsonAgent.nms.reflect.TypeTokenHandle.get(type));
}
public static TypeToken<?> get(final Type type) {
if (GsonAgent.newVersion) {
return new TypeToken(cn.citycraft.GsonAgent.normal.reflect.TypeTokenHandle.get(type));
}
return new TypeToken(cn.citycraft.GsonAgent.nms.reflect.TypeTokenHandle.get(type));
}
@Override
public boolean equals(final Object o) {
return this.internal.equals(o);
}
public Object getHandle() {
return this.internal.getHandle();
}
public Class<? super T> getRawType() {
return this.internal.getRawType();
}
public Type getType() {
return this.internal.getType();
}
@Override
public int hashCode() {
return this.internal.hashCode();
}
@Deprecated
public boolean isAssignableFrom(final Class<?> cls) {
return this.internal.isAssignableFrom(cls);
}
@Deprecated
public boolean isAssignableFrom(final Type from) {
return this.internal.isAssignableFrom(from);
}
@Deprecated
public boolean isAssignableFrom(final TypeToken<?> token) {
return this.internal.isAssignableFrom(token);
}
@Override
public String toString() {
return this.internal.toString();
}
}

View File

@ -0,0 +1,32 @@
package cn.citycraft.GsonAgent.api.reflect;
import java.lang.reflect.Type;
import cn.citycraft.GsonAgent.api.utils.HandleInterface;
@SuppressWarnings("rawtypes")
public abstract interface TypeTokenAbstract<T> extends HandleInterface {
@Override
public boolean equals(Object o);
public Class<? super T> getRawType();
public Type getType();
@Override
public int hashCode();
@Deprecated
public boolean isAssignableFrom(Class<?> cls);
@Deprecated
public boolean isAssignableFrom(Type from);
@Deprecated
public boolean isAssignableFrom(TypeToken<?> token);
@Override
public String toString();
}

View File

@ -0,0 +1,44 @@
package cn.citycraft.GsonAgent.api.stream;
import java.io.IOException;
public abstract interface JsonReader {
public void beginArray() throws IOException;
public void beginObject() throws IOException;
public void close() throws IOException;
public void endArray() throws IOException;
public void endObject() throws IOException;
public boolean hasNext() throws IOException;
public boolean isLenient();
public boolean nextBoolean() throws IOException;
public double nextDouble() throws IOException;
public int nextInt() throws IOException;
public long nextLong() throws IOException;
public String nextName() throws IOException;
public void nextNull() throws IOException;
public String nextString() throws IOException;
public JsonToken peek() throws IOException;
public void setLenient(boolean lenient);
public void skipValue() throws IOException;
@Override
public String toString();
}

View File

@ -0,0 +1,24 @@
package cn.citycraft.GsonAgent.api.stream;
public enum JsonToken {
BEGIN_ARRAY,
END_ARRAY,
BEGIN_OBJECT,
END_OBJECT,
NAME,
STRING,
NUMBER,
BOOLEAN,
NULL,
END_DOCUMENT;
public static JsonToken getByName(String name) {
for (JsonToken next : JsonToken.values()) {
if (next.name().equals(name)) {
return next;
}
}
return null;
}
}

View File

@ -0,0 +1,55 @@
package cn.citycraft.GsonAgent.api.stream;
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
public abstract interface JsonWriter extends Closeable, Flushable {
public JsonWriter beginArray() throws IOException;
public JsonWriter beginObject() throws IOException;
@Override
public void close() throws IOException;
public JsonWriter endArray() throws IOException;
public JsonWriter endObject() throws IOException;
@Override
public void flush() throws IOException;
public boolean getSerializeNulls();
public boolean isHtmlSafe();
public boolean isLenient();
public JsonWriter name(String name) throws IOException;
public JsonWriter nameWithoutQuotes(String name) throws IOException;
public JsonWriter nullValue() throws IOException;
public void setHtmlSafe(boolean htmlSafe);
public void setIndent(String indent);
public void setLenient(boolean lenient);
public void setSerializeNulls(boolean serializeNulls);
public JsonWriter value(boolean value) throws IOException;
public JsonWriter value(double value) throws IOException;
public JsonWriter value(long value) throws IOException;
public JsonWriter value(Number value) throws IOException;
public JsonWriter value(String value) throws IOException;
void string(String value) throws IOException;
}

View File

@ -0,0 +1,7 @@
package cn.citycraft.GsonAgent.api.utils;
public interface HandleInterface<T> {
public T getHandle();
}

View File

@ -0,0 +1,54 @@
/**
*
*/
package cn.citycraft.GsonAgent.api.utils;
import java.lang.reflect.Constructor;
/**
* Gson代理工具类(来自7老板)
*
* @since 2016年1月5日 下午1:33:57
* @author 曲尘,
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public class Utils {
public static <T> T deepCopyObject(final T obj) {
try {
return (T) obj.getClass().getDeclaredMethod("deepCopy").invoke(obj);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
public static <T> T newInstance(final Class<T> clzz) {
try {
final Constructor<T> constructor = clzz.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
public static <T> T newInstance(final Class<T> clzz, final Class[] types, final Object[] args) {
if (args.length == 0 || types.length == 0) {
return newInstance(clzz);
}
try {
final Constructor<T> constructor = clzz.getDeclaredConstructor(types);
constructor.setAccessible(true);
return constructor.newInstance(args);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
public static <T> T newInstance(final Class<T> clzz, final Object... args) {
final Class[] argClasses = new Class[args.length];
for (int i = 0; i < argClasses.length; i++) {
argClasses[i] = args[i].getClass();
}
return newInstance(clzz, argClasses, args);
}
}

View File

@ -0,0 +1,135 @@
/**
*
*/
package cn.citycraft.GsonAgent.nms;
import java.io.Reader;
import java.lang.reflect.Type;
import cn.citycraft.GsonAgent.api.Gson;
import cn.citycraft.GsonAgent.api.exception.JsonIOException;
import cn.citycraft.GsonAgent.api.exception.JsonSyntaxException;
import cn.citycraft.GsonAgent.nms.bind.TypeAdapterFactoryHandle;
import cn.citycraft.GsonAgent.nms.bind.TypeAdapterHandle;
import cn.citycraft.GsonAgent.nms.stream.JsonReaderHandle;
import cn.citycraft.GsonAgent.nms.stream.JsonWriterHandle;
/**
* 老版本Gson代理类
*
* @since 2016年1月5日 下午12:57:20
* @author
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public class GsonHandle implements Gson {
private final net.minecraft.util.com.google.gson.Gson handle;
public GsonHandle() {
handle = new net.minecraft.util.com.google.gson.Gson();
}
@Override
public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Class<T> classOfT) throws JsonSyntaxException {
return getHandle().fromJson(((JsonElementHandle) json).getHandle(), classOfT);
}
@Override
public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Type typeOfT) throws JsonSyntaxException {
return getHandle().fromJson(((JsonElementHandle) json).getHandle(), typeOfT);
}
@Override
public <T> T fromJson(final cn.citycraft.GsonAgent.api.stream.JsonReader reader, final Type typeOfT) throws JsonIOException, JsonSyntaxException {
return getHandle().fromJson(((JsonReaderHandle) reader).getHandle(), typeOfT);
}
@Override
public <T> T fromJson(final Reader json, final Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
return getHandle().fromJson(json, classOfT);
}
@Override
public <T> T fromJson(final Reader json, final Type typeOfT) throws JsonIOException, JsonSyntaxException {
return getHandle().fromJson(json, typeOfT);
}
@Override
public <T> T fromJson(final String json, final Class<T> classOfT) throws JsonSyntaxException {
return getHandle().fromJson(json, classOfT);
}
@Override
public <T> T fromJson(final String json, final Type typeOfT) throws JsonSyntaxException {
return getHandle().fromJson(json, typeOfT);
}
@Override
public <T> TypeAdapterHandle<T> getAdapter(final Class<T> type) {
return new TypeAdapterHandle(getHandle().getAdapter(type));
}
@Override
public <T> TypeAdapterHandle<T> getAdapter(final cn.citycraft.GsonAgent.api.reflect.TypeToken<T> type) {
return new TypeAdapterHandle(getHandle().getAdapter((net.minecraft.util.com.google.gson.reflect.TypeToken<T>) type.getHandle()));
}
@Override
public <T> TypeAdapterHandle<T> getDelegateAdapter(final cn.citycraft.GsonAgent.api.TypeAdapterFactory skipPast, final cn.citycraft.GsonAgent.api.reflect.TypeToken<T> type) {
return new TypeAdapterHandle(getHandle().getDelegateAdapter(((TypeAdapterFactoryHandle) skipPast).getHandle(), (net.minecraft.util.com.google.gson.reflect.TypeToken<T>) type.getHandle()));
}
public net.minecraft.util.com.google.gson.Gson getHandle() {
return handle;
}
@Override
public String toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement) {
return getHandle().toJson(((JsonElementHandle) jsonElement).getHandle());
}
@Override
public void toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement, final Appendable writer) throws JsonIOException {
getHandle().toJson(((JsonElementHandle) jsonElement).getHandle(), writer);
}
@Override
public void toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement, final cn.citycraft.GsonAgent.api.stream.JsonWriter writer) throws JsonIOException {
getHandle().toJson(((JsonElementHandle) jsonElement).getHandle(), ((JsonWriterHandle) writer).getHandle());
}
@Override
public String toJson(final Object src) {
return getHandle().toJson(src);
}
@Override
public void toJson(final Object src, final Appendable writer) throws JsonIOException {
getHandle().toJson(src, writer);
}
@Override
public String toJson(final Object src, final Type typeOfSrc) {
return getHandle().toJson(src, typeOfSrc);
}
@Override
public void toJson(final Object src, final Type typeOfSrc, final Appendable writer) throws JsonIOException {
getHandle().toJson(src, typeOfSrc, writer);
}
@Override
public void toJson(final Object src, final Type typeOfSrc, final cn.citycraft.GsonAgent.api.stream.JsonWriter writer) throws JsonIOException {
getHandle().toJson(src, typeOfSrc, ((JsonWriterHandle) writer).getHandle());
}
@Override
public JsonElementHandle toJsonTree(final Object src) {
return new JsonElementHandle(getHandle().toJsonTree(src));
}
@Override
public JsonElementHandle toJsonTree(final Object src, final Type typeOfSrc) {
return new JsonElementHandle(getHandle().toJsonTree(src, typeOfSrc));
}
}

View File

@ -0,0 +1,159 @@
package cn.citycraft.GsonAgent.nms;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import cn.citycraft.GsonAgent.api.JsonArray;
import cn.citycraft.GsonAgent.api.utils.Utils;
/**
*
* @since 2016年1月5日 下午1:49:21
* @author
*/
public class JsonArrayHandle extends JsonElementHandle implements JsonArray {
private static Field elementsField;
static {
try {
elementsField = net.minecraft.util.com.google.gson.JsonArray.class.getDeclaredField("elements");
elementsField.setAccessible(true);
} catch (final Exception e) {
e.printStackTrace();
}
}
private final net.minecraft.util.com.google.gson.JsonArray handle;
private List<net.minecraft.util.com.google.gson.JsonElement> elements;
public JsonArrayHandle() {
this(new net.minecraft.util.com.google.gson.JsonArray());
}
@SuppressWarnings("unchecked")
protected JsonArrayHandle(final net.minecraft.util.com.google.gson.JsonArray handle) {
this.handle = handle;
try {
elements = (List<net.minecraft.util.com.google.gson.JsonElement>) elementsField.get(this.handle);
} catch (final Exception e) {
e.printStackTrace();
}
}
@Override
public void add(final cn.citycraft.GsonAgent.api.JsonElement element) {
handle.add(((JsonElementHandle) element).getHandle());
}
@Override
public void addAll(final cn.citycraft.GsonAgent.api.JsonArray array) {
handle.addAll(((JsonArrayHandle) array).handle);
}
@Override
public cn.citycraft.GsonAgent.api.JsonArray deepCopy() {
return new JsonArrayHandle(Utils.deepCopyObject(this.handle));
}
@Override
public boolean equals(final Object o) {
return handle.equals(o);
}
@Override
public cn.citycraft.GsonAgent.api.JsonElement get(final int i) {
return new JsonElementHandle(handle.get(i));
}
@Override
public BigDecimal getAsBigDecimal() {
return handle.getAsBigDecimal();
}
@Override
public BigInteger getAsBigInteger() {
return handle.getAsBigInteger();
}
@Override
public boolean getAsBoolean() {
return handle.getAsBoolean();
}
@Override
public byte getAsByte() {
return handle.getAsByte();
}
@Override
public char getAsCharacter() {
return handle.getAsCharacter();
}
@Override
public double getAsDouble() {
return handle.getAsDouble();
}
@Override
public float getAsFloat() {
return handle.getAsFloat();
}
@Override
public int getAsInt() {
return handle.getAsInt();
}
@Override
public long getAsLong() {
return handle.getAsLong();
}
@Override
public Number getAsNumber() {
return handle.getAsNumber();
}
@Override
public short getAsShort() {
return handle.getAsShort();
}
@Override
public String getAsString() {
return handle.getAsString();
}
@Override
public int hashCode() {
return handle.hashCode();
}
@Override
public Iterator<cn.citycraft.GsonAgent.api.JsonElement> iterator() {
try {
final List<cn.citycraft.GsonAgent.api.JsonElement> result = new ArrayList<>();
final Iterator<net.minecraft.util.com.google.gson.JsonElement> iterator = this.elements.iterator();
while (iterator.hasNext()) {
result.add(new JsonElementHandle(iterator.next()));
}
return result.iterator();
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public int size() {
return handle.size();
}
}

View File

@ -0,0 +1,143 @@
/**
*
*/
package cn.citycraft.GsonAgent.nms;
import java.math.BigDecimal;
import java.math.BigInteger;
import cn.citycraft.GsonAgent.api.JsonElement;
import cn.citycraft.GsonAgent.api.utils.Utils;
/**
* JsonElementHandler处理类
*
* @since 2016年1月5日 下午1:36:14
* @author
*/
public class JsonElementHandle implements JsonElement {
private net.minecraft.util.com.google.gson.JsonElement handle;
public JsonElementHandle() {
}
public JsonElementHandle(final net.minecraft.util.com.google.gson.JsonElement handle) {
this.handle = handle;
}
@Override
public cn.citycraft.GsonAgent.api.JsonElement deepCopy() {
return new JsonElementHandle(Utils.deepCopyObject(this.getHandle()));
}
@Override
public BigDecimal getAsBigDecimal() {
return getHandle().getAsBigDecimal();
}
@Override
public BigInteger getAsBigInteger() {
return getHandle().getAsBigInteger();
}
@Override
public boolean getAsBoolean() {
return getHandle().getAsBoolean();
}
@Override
public byte getAsByte() {
return getHandle().getAsByte();
}
@Override
public char getAsCharacter() {
return getHandle().getAsCharacter();
}
@Override
public double getAsDouble() {
return getHandle().getAsDouble();
}
@Override
public float getAsFloat() {
return getHandle().getAsFloat();
}
@Override
public int getAsInt() {
return getHandle().getAsInt();
}
@Override
public JsonArrayHandle getAsJsonArray() {
return new JsonArrayHandle(getHandle().getAsJsonArray());
}
@Override
public JsonNullHandle getAsJsonNull() {
return new JsonNullHandle(getHandle().getAsJsonNull());
}
@Override
public JsonObjectHandle getAsJsonObject() {
return new JsonObjectHandle(getHandle().getAsJsonObject());
}
@Override
public JsonPrimitiveHandle getAsJsonPrimitive() {
return new JsonPrimitiveHandle(getHandle().getAsJsonPrimitive());
}
@Override
public long getAsLong() {
return getHandle().getAsLong();
}
@Override
public Number getAsNumber() {
return getHandle().getAsNumber();
}
@Override
public short getAsShort() {
return getHandle().getAsShort();
}
@Override
public String getAsString() {
return getHandle().getAsString();
}
public net.minecraft.util.com.google.gson.JsonElement getHandle() {
return handle;
}
@Override
public boolean isJsonArray() {
return getHandle().isJsonArray();
}
@Override
public boolean isJsonNull() {
return getHandle().isJsonNull();
}
@Override
public boolean isJsonObject() {
return getHandle().isJsonObject();
}
@Override
public boolean isJsonPrimitive() {
return getHandle().isJsonPrimitive();
}
@Override
public String toString() {
return getHandle().toString();
}
}

View File

@ -0,0 +1,36 @@
package cn.citycraft.GsonAgent.nms;
import cn.citycraft.GsonAgent.api.JsonNull;
/**
*
* @since 2016年1月5日 下午1:49:34
* @author
*/
public class JsonNullHandle extends JsonElementHandle implements JsonNull {
private final net.minecraft.util.com.google.gson.JsonNull handle;
public JsonNullHandle() {
this(net.minecraft.util.com.google.gson.JsonNull.INSTANCE);
}
protected JsonNullHandle(final net.minecraft.util.com.google.gson.JsonNull handle) {
this.handle = handle;
}
@Override
public cn.citycraft.GsonAgent.api.JsonNull deepCopy() {
return new JsonNullHandle();
}
@Override
public boolean equals(final Object other) {
return handle.equals(other);
}
@Override
public int hashCode() {
return handle.hashCode();
}
}

View File

@ -0,0 +1,131 @@
package cn.citycraft.GsonAgent.nms;
import java.lang.reflect.Field;
import java.util.AbstractMap;
import java.util.Map;
import java.util.Set;
import cn.citycraft.GsonAgent.api.JsonObject;
import cn.citycraft.GsonAgent.api.utils.Utils;
import net.minecraft.util.com.google.gson.internal.LinkedTreeMap;
/**
*
* @since 2016年1月5日 下午1:49:41
* @author
*/
public class JsonObjectHandle extends JsonElementHandle implements JsonObject {
private static Field membersField;
static {
try {
membersField = net.minecraft.util.com.google.gson.JsonObject.class.getDeclaredField("members");
membersField.setAccessible(true);
} catch (final Exception e) {
e.printStackTrace();
}
}
private final net.minecraft.util.com.google.gson.JsonObject handle;
private AbstractMap<String, net.minecraft.util.com.google.gson.JsonElement> members;
public JsonObjectHandle() {
this(new net.minecraft.util.com.google.gson.JsonObject());
}
@SuppressWarnings("unchecked")
protected JsonObjectHandle(final net.minecraft.util.com.google.gson.JsonObject handle) {
this.handle = handle;
try {
members = (AbstractMap<String, net.minecraft.util.com.google.gson.JsonElement>) membersField.get(this.handle);
} catch (final Exception e) {
e.printStackTrace();
}
}
@Override
public void add(final String property, final cn.citycraft.GsonAgent.api.JsonElement value) {
handle.add(property, ((JsonElementHandle) value).getHandle());
}
@Override
public void addProperty(final String property, final Boolean value) {
handle.addProperty(property, value);
}
@Override
public void addProperty(final String property, final Character value) {
handle.addProperty(property, value);
}
@Override
public void addProperty(final String property, final Number value) {
handle.addProperty(property, value);
}
@Override
public void addProperty(final String property, final String value) {
handle.addProperty(property, value);
}
@Override
public cn.citycraft.GsonAgent.api.JsonObject deepCopy() {
return new JsonObjectHandle(Utils.deepCopyObject(this.handle));
}
@Override
public Set<Map.Entry<String, cn.citycraft.GsonAgent.api.JsonElement>> entrySet() {
try {
final Map<String, cn.citycraft.GsonAgent.api.JsonElement> result = new LinkedTreeMap<>();
for (final Map.Entry<String, net.minecraft.util.com.google.gson.JsonElement> entry : members.entrySet()) {
result.put(entry.getKey(), new JsonElementHandle(entry.getValue()));
}
return result.entrySet();
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public boolean equals(final Object o) {
return handle.equals(o);
}
@Override
public JsonElementHandle get(final String memberName) {
return new JsonElementHandle(handle.get(memberName));
}
@Override
public JsonArrayHandle getAsJsonArray(final String memberName) {
return new JsonArrayHandle(handle.getAsJsonArray(memberName));
}
@Override
public JsonObjectHandle getAsJsonObject(final String memberName) {
return new JsonObjectHandle(handle.getAsJsonObject(memberName));
}
@Override
public JsonPrimitiveHandle getAsJsonPrimitive(final String memberName) {
return new JsonPrimitiveHandle(handle.getAsJsonPrimitive(memberName));
}
@Override
public boolean has(final String memberName) {
return handle.has(memberName);
}
@Override
public int hashCode() {
return handle.hashCode();
}
@Override
public JsonElementHandle remove(final String property) {
return new JsonElementHandle(handle.remove(property));
}
}

View File

@ -0,0 +1,41 @@
package cn.citycraft.GsonAgent.nms;
import java.io.Reader;
import cn.citycraft.GsonAgent.api.exception.JsonIOException;
import cn.citycraft.GsonAgent.api.exception.JsonSyntaxException;
import cn.citycraft.GsonAgent.nms.stream.JsonReaderHandle;
public class JsonParserHandle implements cn.citycraft.GsonAgent.api.JsonParser {
public net.minecraft.util.com.google.gson.JsonParser handle;
public JsonParserHandle() {
handle = new net.minecraft.util.com.google.gson.JsonParser();
}
protected JsonParserHandle(final net.minecraft.util.com.google.gson.JsonParser handle) {
this.handle = handle;
}
@Override
public net.minecraft.util.com.google.gson.JsonParser getHandle() {
return this.handle;
}
@Override
public JsonElementHandle parse(final cn.citycraft.GsonAgent.api.stream.JsonReader json) throws JsonIOException, JsonSyntaxException {
return new JsonElementHandle(getHandle().parse(((JsonReaderHandle) json).getHandle()));
}
@Override
public JsonElementHandle parse(final Reader json) throws JsonIOException, JsonSyntaxException {
return new JsonElementHandle(getHandle().parse(json));
}
@Override
public JsonElementHandle parse(final String json) throws JsonSyntaxException {
return new JsonElementHandle(getHandle().parse(json));
}
}

View File

@ -0,0 +1,154 @@
/**
*
*/
package cn.citycraft.GsonAgent.nms;
import java.math.BigDecimal;
import java.math.BigInteger;
import cn.citycraft.GsonAgent.api.JsonPrimitive;
import cn.citycraft.GsonAgent.api.utils.Utils;
/**
*
* @since 2016年1月5日 下午1:49:49
* @author
*/
public class JsonPrimitiveHandle extends JsonElementHandle implements JsonPrimitive {
private final net.minecraft.util.com.google.gson.JsonPrimitive handle;
public JsonPrimitiveHandle(final Boolean bool) {
this(new net.minecraft.util.com.google.gson.JsonPrimitive(bool));
setValue(bool);
}
public JsonPrimitiveHandle(final Character c) {
this(new net.minecraft.util.com.google.gson.JsonPrimitive(c));
setValue(c);
}
public JsonPrimitiveHandle(final Number number) {
this(new net.minecraft.util.com.google.gson.JsonPrimitive(number));
setValue(number);
}
public JsonPrimitiveHandle(final String string) {
this(new net.minecraft.util.com.google.gson.JsonPrimitive(string));
setValue(string);
}
protected JsonPrimitiveHandle(final net.minecraft.util.com.google.gson.JsonPrimitive handle) {
this.handle = handle;
}
@Override
public cn.citycraft.GsonAgent.api.JsonPrimitive deepCopy() {
return new JsonPrimitiveHandle(Utils.deepCopyObject(this.handle));
}
@Override
public boolean equals(final Object obj) {
return handle.equals(obj);
}
@Override
public BigDecimal getAsBigDecimal() {
return handle.getAsBigDecimal();
}
@Override
public BigInteger getAsBigInteger() {
return handle.getAsBigInteger();
}
@Override
public boolean getAsBoolean() {
return handle.getAsBoolean();
}
@Override
public byte getAsByte() {
return handle.getAsByte();
}
@Override
public char getAsCharacter() {
return handle.getAsCharacter();
}
@Override
public double getAsDouble() {
return handle.getAsDouble();
}
@Override
public float getAsFloat() {
return handle.getAsFloat();
}
@Override
public int getAsInt() {
return handle.getAsInt();
}
@Override
public JsonArrayHandle getAsJsonArray() {
return new JsonArrayHandle(handle.getAsJsonArray());
}
@Override
public JsonObjectHandle getAsJsonObject() {
return new JsonObjectHandle(handle.getAsJsonObject());
}
@Override
public long getAsLong() {
return handle.getAsLong();
}
@Override
public Number getAsNumber() {
return handle.getAsNumber();
}
@Override
public short getAsShort() {
return handle.getAsShort();
}
@Override
public String getAsString() {
return handle.getAsString();
}
@Override
public int hashCode() {
return handle.hashCode();
}
@Override
public boolean isBoolean() {
return handle.isBoolean();
}
@Override
public boolean isNumber() {
return handle.isNumber();
}
@Override
public boolean isString() {
return handle.isString();
}
@Override
public final void setValue(final Object primitive) {
try {
handle.getClass().getDeclaredMethod("setValue", Object.class).invoke(handle, primitive);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,32 @@
package cn.citycraft.GsonAgent.nms.bind;
import cn.citycraft.GsonAgent.nms.GsonHandle;
import net.minecraft.util.com.google.gson.Gson;
import net.minecraft.util.com.google.gson.TypeAdapter;
import net.minecraft.util.com.google.gson.TypeAdapterFactory;
import net.minecraft.util.com.google.gson.reflect.TypeToken;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class TypeAdapterFactoryHandle implements cn.citycraft.GsonAgent.api.TypeAdapterFactory, net.minecraft.util.com.google.gson.TypeAdapterFactory {
private final TypeAdapterFactory handle;
protected TypeAdapterFactoryHandle(final TypeAdapterFactory handle) {
this.handle = handle;
}
@Override
public <T> TypeAdapterHandle<T> create(final cn.citycraft.GsonAgent.api.Gson paramGson, final cn.citycraft.GsonAgent.api.reflect.TypeToken<T> paramTypeToken) {
return new TypeAdapterHandle(this.create(((GsonHandle) paramGson).getHandle(), (TypeToken<T>) paramTypeToken.getHandle()));
}
@Override
public <T> TypeAdapter<T> create(final Gson paramGson, final TypeToken<T> paramTypeToken) {
return this.getHandle().create(paramGson, paramTypeToken);
}
public TypeAdapterFactory getHandle() {
return handle;
}
}

View File

@ -0,0 +1,77 @@
package cn.citycraft.GsonAgent.nms.bind;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import cn.citycraft.GsonAgent.nms.JsonElementHandle;
import net.minecraft.util.com.google.gson.TypeAdapter;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class TypeAdapterHandle<T> implements cn.citycraft.GsonAgent.api.TypeAdapter<T> {
private final TypeAdapter<T> handle;
public TypeAdapterHandle(final TypeAdapter handle) {
this.handle = handle;
}
@Override
public T fromJson(final Reader in) throws IOException {
return getHandle().fromJson(in);
}
@Override
public T fromJson(final String json) throws IOException {
return getHandle().fromJson(json);
}
@Override
public T fromJsonTree(final cn.citycraft.GsonAgent.api.JsonElement jsonTree) {
return getHandle().fromJsonTree(((JsonElementHandle) jsonTree).getHandle());
}
@Override
public TypeAdapter<T> getHandle() {
return handle;
}
@Override
public TypeAdapterHandle<T> nullSafe() {
return new TypeAdapterHandle(this.handle);
}
@Override
public T read(final cn.citycraft.GsonAgent.api.stream.JsonReader reader) throws IOException {
if (reader.peek().name().equals("NULL")) {
reader.nextNull();
return null;
}
return this.read(reader);
}
@Override
public String toJson(final T value) throws IOException {
return getHandle().toJson(value);
}
@Override
public void toJson(final Writer out, final T value) throws IOException {
getHandle().toJson(out, value);
}
@Override
public JsonElementHandle toJsonTree(final T value) {
return new JsonElementHandle(getHandle().toJsonTree(value));
}
@Override
public void write(final cn.citycraft.GsonAgent.api.stream.JsonWriter out, final T value) throws IOException {
if (value == null) {
out.nullValue();
} else {
this.write(out, value);
}
}
}

View File

@ -0,0 +1,73 @@
package cn.citycraft.GsonAgent.nms.reflect;
import java.lang.reflect.Type;
import cn.citycraft.GsonAgent.api.reflect.TypeTokenAbstract;
import cn.citycraft.GsonAgent.api.utils.Utils;
import net.minecraft.util.com.google.gson.reflect.TypeToken;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class TypeTokenHandle<T> implements cn.citycraft.GsonAgent.api.reflect.TypeTokenAbstract<T> {
private TypeToken<T> handle;
public TypeTokenHandle() {
try {
handle = new TypeToken<T>() {
};
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
public TypeTokenHandle(final Type type) {
try {
handle = Utils.newInstance(TypeToken.class, Type.class, type);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
protected TypeTokenHandle(final TypeToken<T> handle) {
this.handle = handle;
}
public static <T> TypeTokenAbstract<T> get(final Class<T> type) {
return new TypeTokenHandle(TypeToken.get(type));
}
public static TypeTokenAbstract<?> get(final Type type) {
return new TypeTokenHandle(TypeToken.get(type));
}
@Override
public TypeToken<T> getHandle() {
return this.handle;
}
@Override
public Class<? super T> getRawType() {
return getHandle().getRawType();
}
@Override
public Type getType() {
return getHandle().getType();
}
@Override
public boolean isAssignableFrom(final Class<?> cls) {
return getHandle().isAssignableFrom(cls);
}
@Override
public boolean isAssignableFrom(final cn.citycraft.GsonAgent.api.reflect.TypeToken<?> token) {
return getHandle().isAssignableFrom((TypeToken<?>) token.getHandle());
}
@Override
public boolean isAssignableFrom(final Type from) {
return getHandle().isAssignableFrom(from);
}
}

View File

@ -0,0 +1,115 @@
package cn.citycraft.GsonAgent.nms.stream;
import java.io.IOException;
import java.io.Reader;
import cn.citycraft.GsonAgent.api.stream.JsonToken;
import net.minecraft.util.com.google.gson.stream.JsonReader;
public class JsonReaderHandle implements cn.citycraft.GsonAgent.api.stream.JsonReader {
private final JsonReader handle;
public JsonReaderHandle(final Reader in) {
this(new JsonReader(in));
}
protected JsonReaderHandle(final JsonReader handle) {
this.handle = handle;
}
@Override
public void beginArray() throws IOException {
getHandle().beginArray();
}
@Override
public void beginObject() throws IOException {
getHandle().beginObject();
}
@Override
public void close() throws IOException {
getHandle().close();
}
@Override
public void endArray() throws IOException {
getHandle().endArray();
}
@Override
public void endObject() throws IOException {
getHandle().endObject();
}
public JsonReader getHandle() {
return handle;
}
@Override
public boolean hasNext() throws IOException {
return getHandle().hasNext();
}
@Override
public boolean isLenient() {
return getHandle().isLenient();
}
@Override
public boolean nextBoolean() throws IOException {
return getHandle().nextBoolean();
}
@Override
public double nextDouble() throws IOException {
return getHandle().nextDouble();
}
@Override
public int nextInt() throws IOException {
return getHandle().nextInt();
}
@Override
public long nextLong() throws IOException {
return getHandle().nextLong();
}
@Override
public String nextName() throws IOException {
return getHandle().nextName();
}
@Override
public void nextNull() throws IOException {
getHandle().nextNull();
}
@Override
public String nextString() throws IOException {
return getHandle().nextString();
}
@Override
public JsonToken peek() throws IOException {
return JsonToken.getByName(getHandle().peek().name());
}
@Override
public void setLenient(final boolean lenient) {
getHandle().setLenient(lenient);
}
@Override
public void skipValue() throws IOException {
getHandle().skipValue();
}
@Override
public String toString() {
return getHandle().toString();
}
}

View File

@ -0,0 +1,208 @@
package cn.citycraft.GsonAgent.nms.stream;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Field;
import net.minecraft.util.com.google.gson.stream.JsonWriter;
public class JsonWriterHandle implements cn.citycraft.GsonAgent.api.stream.JsonWriter {
private static String[] REPLACEMENT_CHARS;
private static String[] HTML_SAFE_REPLACEMENT_CHARS;
static {
try {
Field field = JsonWriter.class.getDeclaredField("REPLACEMENT_CHARS");
field.setAccessible(true);
REPLACEMENT_CHARS = (String[]) field.get(null);
field = JsonWriter.class.getDeclaredField("HTML_SAFE_REPLACEMENT_CHARS");
field.setAccessible(true);
HTML_SAFE_REPLACEMENT_CHARS = (String[]) field.get(null);
} catch (final Exception e) {
e.printStackTrace();
}
}
private final JsonWriter handle;
private Writer out;
private boolean withoutQuotes = false;
public JsonWriterHandle(final Writer out) {
this(new JsonWriter(out));
this.out = out;
}
protected JsonWriterHandle(final JsonWriter handle) {
this.handle = handle;
}
@Override
public JsonWriterHandle beginArray() throws IOException {
getHandle().beginArray();
return this;
}
@Override
public JsonWriterHandle beginObject() throws IOException {
getHandle().beginObject();
return this;
}
@Override
public void close() throws IOException {
getHandle().close();
}
@Override
public JsonWriterHandle endArray() throws IOException {
getHandle().endArray();
return this;
}
@Override
public JsonWriterHandle endObject() throws IOException {
getHandle().endObject();
return this;
}
@Override
public void flush() throws IOException {
getHandle().flush();
}
public JsonWriter getHandle() {
return handle;
}
@Override
public boolean getSerializeNulls() {
return getHandle().getSerializeNulls();
}
@Override
public boolean isHtmlSafe() {
return getHandle().isHtmlSafe();
}
@Override
public boolean isLenient() {
return getHandle().isLenient();
}
@Override
public JsonWriterHandle name(final String name) throws IOException {
getHandle().name(name);
withoutQuotes = false;
return this;
}
@Override
public JsonWriterHandle nameWithoutQuotes(final String name) throws IOException {
getHandle().name(name);
withoutQuotes = true;
return this;
}
@Override
public JsonWriterHandle nullValue() throws IOException {
getHandle().nullValue();
return this;
}
@Override
public void setHtmlSafe(final boolean htmlSafe) {
getHandle().setHtmlSafe(htmlSafe);
}
@Override
public void setIndent(final String indent) {
getHandle().setIndent(indent);
}
@Override
public void setLenient(final boolean lenient) {
getHandle().setLenient(lenient);
}
@Override
public void setSerializeNulls(final boolean serializeNulls) {
getHandle().setSerializeNulls(serializeNulls);
}
@Override
public void string(final String value) throws IOException {
final String[] replacements = getHandle().isHtmlSafe() ? HTML_SAFE_REPLACEMENT_CHARS : REPLACEMENT_CHARS;
if (!this.withoutQuotes) {
this.out.write("\"");
}
int last = 0;
final int length = value.length();
for (int i = 0; i < length; i++) {
final char c = value.charAt(i);
String replacement;
if (c < '€') {
replacement = replacements[c];
if (replacement == null) {
continue;
}
} else {
if (c == '') {
replacement = "\\u2028";
} else {
if (c != '') {
continue;
}
replacement = "\\u2029";
}
}
if (last < i) {
this.out.write(value, last, i - last);
}
this.out.write(replacement);
last = i + 1;
}
if (last < length) {
this.out.write(value, last, length - last);
}
if (!this.withoutQuotes) {
this.out.write("\"");
}
withoutQuotes = false;
}
@Override
public JsonWriterHandle value(final boolean value) throws IOException {
getHandle().value(value);
return this;
}
@Override
public JsonWriterHandle value(final double value) throws IOException {
getHandle().value(value);
return this;
}
@Override
public JsonWriterHandle value(final long value) throws IOException {
getHandle().value(value);
return this;
}
@Override
public JsonWriterHandle value(final Number value) throws IOException {
getHandle().value(value);
return this;
}
@Override
public JsonWriterHandle value(final String value) throws IOException {
getHandle().value(value);
return this;
}
}

View File

@ -0,0 +1,128 @@
package cn.citycraft.GsonAgent.normal;
import java.io.Reader;
import java.lang.reflect.Type;
import com.google.gson.JsonSyntaxException;
import cn.citycraft.GsonAgent.api.Gson;
import cn.citycraft.GsonAgent.api.exception.JsonIOException;
import cn.citycraft.GsonAgent.normal.bind.TypeAdapterFactoryHandle;
import cn.citycraft.GsonAgent.normal.bind.TypeAdapterHandle;
import cn.citycraft.GsonAgent.normal.stream.JsonReaderHandle;
import cn.citycraft.GsonAgent.normal.stream.JsonWriterHandle;
/**
* 新版本Gson代理类
*
* @since 2016年1月5日 下午12:57:20
* @author
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public class GsonHandle implements Gson {
final com.google.gson.Gson handle = new com.google.gson.Gson();
@Override
public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Class<T> classOfT) throws JsonSyntaxException {
return getHandle().fromJson(((JsonElementHandle) json).getHandle(), classOfT);
}
@Override
public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Type typeOfT) throws JsonSyntaxException {
return getHandle().fromJson(((JsonElementHandle) json).getHandle(), typeOfT);
}
@Override
public <T> T fromJson(final cn.citycraft.GsonAgent.api.stream.JsonReader reader, final Type typeOfT) throws JsonIOException, JsonSyntaxException {
return getHandle().fromJson(((JsonReaderHandle) reader).getHandle(), typeOfT);
}
@Override
public <T> T fromJson(final Reader json, final Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
return getHandle().fromJson(json, classOfT);
}
@Override
public <T> T fromJson(final Reader json, final Type typeOfT) throws JsonIOException, JsonSyntaxException {
return getHandle().fromJson(json, typeOfT);
}
@Override
public <T> T fromJson(final String json, final Class<T> classOfT) throws JsonSyntaxException {
return getHandle().fromJson(json, classOfT);
}
@Override
public <T> T fromJson(final String json, final Type typeOfT) throws JsonSyntaxException {
return getHandle().fromJson(json, typeOfT);
}
@Override
public <T> TypeAdapterHandle<T> getAdapter(final Class<T> type) {
return new TypeAdapterHandle(getHandle().getAdapter(type));
}
@Override
public <T> TypeAdapterHandle<T> getAdapter(final cn.citycraft.GsonAgent.api.reflect.TypeToken<T> type) {
return new TypeAdapterHandle(getHandle().getAdapter((com.google.gson.reflect.TypeToken<T>) type.getHandle()));
}
@Override
public <T> TypeAdapterHandle<T> getDelegateAdapter(final cn.citycraft.GsonAgent.api.TypeAdapterFactory skipPast, final cn.citycraft.GsonAgent.api.reflect.TypeToken<T> type) {
return new TypeAdapterHandle(getHandle().getDelegateAdapter(((TypeAdapterFactoryHandle) skipPast).getHandle(), (com.google.gson.reflect.TypeToken<T>) type.getHandle()));
}
public com.google.gson.Gson getHandle() {
return handle;
}
@Override
public String toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement) {
return getHandle().toJson(((JsonElementHandle) jsonElement).getHandle());
}
@Override
public void toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement, final Appendable writer) throws JsonIOException {
getHandle().toJson(((JsonElementHandle) jsonElement).getHandle(), writer);
}
@Override
public void toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement, final cn.citycraft.GsonAgent.api.stream.JsonWriter writer) throws JsonIOException {
getHandle().toJson(((JsonElementHandle) jsonElement).getHandle(), ((JsonWriterHandle) writer).getHandle());
}
@Override
public String toJson(final Object src) {
return getHandle().toJson(src);
}
@Override
public void toJson(final Object src, final Appendable writer) throws JsonIOException {
getHandle().toJson(src, writer);
}
@Override
public String toJson(final Object src, final Type typeOfSrc) {
return getHandle().toJson(src, typeOfSrc);
}
@Override
public void toJson(final Object src, final Type typeOfSrc, final Appendable writer) throws JsonIOException {
getHandle().toJson(src, typeOfSrc, writer);
}
@Override
public void toJson(final Object src, final Type typeOfSrc, final cn.citycraft.GsonAgent.api.stream.JsonWriter writer) throws JsonIOException {
getHandle().toJson(src, typeOfSrc, ((JsonWriterHandle) writer).getHandle());
}
@Override
public JsonElementHandle toJsonTree(final Object src) {
return new JsonElementHandle(getHandle().toJsonTree(src));
}
@Override
public JsonElementHandle toJsonTree(final Object src, final Type typeOfSrc) {
return new JsonElementHandle(getHandle().toJsonTree(src, typeOfSrc));
}
}

View File

@ -0,0 +1,159 @@
package cn.citycraft.GsonAgent.normal;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import cn.citycraft.GsonAgent.api.JsonArray;
import cn.citycraft.GsonAgent.api.utils.Utils;
/**
*
* @since 2016年1月5日 下午1:49:21
* @author
*/
public class JsonArrayHandle extends JsonElementHandle implements JsonArray {
private static Field elementsField;
static {
try {
elementsField = com.google.gson.JsonArray.class.getDeclaredField("elements");
elementsField.setAccessible(true);
} catch (final Exception e) {
e.printStackTrace();
}
}
private final com.google.gson.JsonArray handle;
private List<com.google.gson.JsonElement> elements;
public JsonArrayHandle() {
this(new com.google.gson.JsonArray());
}
@SuppressWarnings("unchecked")
protected JsonArrayHandle(final com.google.gson.JsonArray handle) {
this.handle = handle;
try {
elements = (List<com.google.gson.JsonElement>) elementsField.get(this.handle);
} catch (final Exception e) {
e.printStackTrace();
}
}
@Override
public void add(final cn.citycraft.GsonAgent.api.JsonElement element) {
handle.add(((JsonElementHandle) element).getHandle());
}
@Override
public void addAll(final cn.citycraft.GsonAgent.api.JsonArray array) {
handle.addAll(((JsonArrayHandle) array).handle);
}
@Override
public cn.citycraft.GsonAgent.api.JsonArray deepCopy() {
return new JsonArrayHandle(Utils.deepCopyObject(this.handle));
}
@Override
public boolean equals(final Object o) {
return handle.equals(o);
}
@Override
public cn.citycraft.GsonAgent.api.JsonElement get(final int i) {
return new JsonElementHandle(handle.get(i));
}
@Override
public BigDecimal getAsBigDecimal() {
return handle.getAsBigDecimal();
}
@Override
public BigInteger getAsBigInteger() {
return handle.getAsBigInteger();
}
@Override
public boolean getAsBoolean() {
return handle.getAsBoolean();
}
@Override
public byte getAsByte() {
return handle.getAsByte();
}
@Override
public char getAsCharacter() {
return handle.getAsCharacter();
}
@Override
public double getAsDouble() {
return handle.getAsDouble();
}
@Override
public float getAsFloat() {
return handle.getAsFloat();
}
@Override
public int getAsInt() {
return handle.getAsInt();
}
@Override
public long getAsLong() {
return handle.getAsLong();
}
@Override
public Number getAsNumber() {
return handle.getAsNumber();
}
@Override
public short getAsShort() {
return handle.getAsShort();
}
@Override
public String getAsString() {
return handle.getAsString();
}
@Override
public int hashCode() {
return handle.hashCode();
}
@Override
public Iterator<cn.citycraft.GsonAgent.api.JsonElement> iterator() {
try {
final List<cn.citycraft.GsonAgent.api.JsonElement> result = new ArrayList<>();
final Iterator<com.google.gson.JsonElement> iterator = this.elements.iterator();
while (iterator.hasNext()) {
result.add(new JsonElementHandle(iterator.next()));
}
return result.iterator();
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public int size() {
return handle.size();
}
}

View File

@ -0,0 +1,143 @@
/**
*
*/
package cn.citycraft.GsonAgent.normal;
import java.math.BigDecimal;
import java.math.BigInteger;
import cn.citycraft.GsonAgent.api.JsonElement;
import cn.citycraft.GsonAgent.api.utils.Utils;
/**
* JsonElementHandler处理类
*
* @since 2016年1月5日 下午1:36:14
* @author
*/
public class JsonElementHandle implements JsonElement {
private com.google.gson.JsonElement handle;
public JsonElementHandle() {
}
public JsonElementHandle(final com.google.gson.JsonElement handle) {
this.handle = handle;
}
@Override
public cn.citycraft.GsonAgent.api.JsonElement deepCopy() {
return new JsonElementHandle(Utils.deepCopyObject(this.getHandle()));
}
@Override
public BigDecimal getAsBigDecimal() {
return getHandle().getAsBigDecimal();
}
@Override
public BigInteger getAsBigInteger() {
return getHandle().getAsBigInteger();
}
@Override
public boolean getAsBoolean() {
return getHandle().getAsBoolean();
}
@Override
public byte getAsByte() {
return getHandle().getAsByte();
}
@Override
public char getAsCharacter() {
return getHandle().getAsCharacter();
}
@Override
public double getAsDouble() {
return getHandle().getAsDouble();
}
@Override
public float getAsFloat() {
return getHandle().getAsFloat();
}
@Override
public int getAsInt() {
return getHandle().getAsInt();
}
@Override
public JsonArrayHandle getAsJsonArray() {
return new JsonArrayHandle(getHandle().getAsJsonArray());
}
@Override
public JsonNullHandle getAsJsonNull() {
return new JsonNullHandle(getHandle().getAsJsonNull());
}
@Override
public JsonObjectHandle getAsJsonObject() {
return new JsonObjectHandle(getHandle().getAsJsonObject());
}
@Override
public JsonPrimitiveHandle getAsJsonPrimitive() {
return new JsonPrimitiveHandle(getHandle().getAsJsonPrimitive());
}
@Override
public long getAsLong() {
return getHandle().getAsLong();
}
@Override
public Number getAsNumber() {
return getHandle().getAsNumber();
}
@Override
public short getAsShort() {
return getHandle().getAsShort();
}
@Override
public String getAsString() {
return getHandle().getAsString();
}
public com.google.gson.JsonElement getHandle() {
return handle;
}
@Override
public boolean isJsonArray() {
return getHandle().isJsonArray();
}
@Override
public boolean isJsonNull() {
return getHandle().isJsonNull();
}
@Override
public boolean isJsonObject() {
return getHandle().isJsonObject();
}
@Override
public boolean isJsonPrimitive() {
return getHandle().isJsonPrimitive();
}
@Override
public String toString() {
return getHandle().toString();
}
}

View File

@ -0,0 +1,36 @@
package cn.citycraft.GsonAgent.normal;
import cn.citycraft.GsonAgent.api.JsonNull;
/**
*
* @since 2016年1月5日 下午1:49:34
* @author
*/
public class JsonNullHandle extends JsonElementHandle implements JsonNull {
private final com.google.gson.JsonNull handle;
public JsonNullHandle() {
this(com.google.gson.JsonNull.INSTANCE);
}
protected JsonNullHandle(final com.google.gson.JsonNull handle) {
this.handle = handle;
}
@Override
public cn.citycraft.GsonAgent.api.JsonNull deepCopy() {
return new JsonNullHandle();
}
@Override
public boolean equals(final Object other) {
return handle.equals(other);
}
@Override
public int hashCode() {
return handle.hashCode();
}
}

View File

@ -0,0 +1,135 @@
/**
*
*/
package cn.citycraft.GsonAgent.normal;
import java.lang.reflect.Field;
import java.util.AbstractMap;
import java.util.Map;
import java.util.Set;
import com.google.gson.internal.LinkedTreeMap;
import cn.citycraft.GsonAgent.api.JsonObject;
import cn.citycraft.GsonAgent.api.utils.Utils;
/**
*
* @since 2016年1月5日 下午1:49:41
* @author
*/
public class JsonObjectHandle extends JsonElementHandle implements JsonObject {
private static Field membersField;
static {
try {
membersField = com.google.gson.JsonObject.class.getDeclaredField("members");
membersField.setAccessible(true);
} catch (final Exception e) {
e.printStackTrace();
}
}
private final com.google.gson.JsonObject handle;
private AbstractMap<String, com.google.gson.JsonElement> members;
public JsonObjectHandle() {
this(new com.google.gson.JsonObject());
}
@SuppressWarnings("unchecked")
protected JsonObjectHandle(final com.google.gson.JsonObject handle) {
this.handle = handle;
try {
members = (AbstractMap<String, com.google.gson.JsonElement>) membersField.get(this.handle);
} catch (final Exception e) {
e.printStackTrace();
}
}
@Override
public void add(final String property, final cn.citycraft.GsonAgent.api.JsonElement value) {
handle.add(property, ((JsonElementHandle) value).getHandle());
}
@Override
public void addProperty(final String property, final Boolean value) {
handle.addProperty(property, value);
}
@Override
public void addProperty(final String property, final Character value) {
handle.addProperty(property, value);
}
@Override
public void addProperty(final String property, final Number value) {
handle.addProperty(property, value);
}
@Override
public void addProperty(final String property, final String value) {
handle.addProperty(property, value);
}
@Override
public cn.citycraft.GsonAgent.api.JsonObject deepCopy() {
return new JsonObjectHandle(Utils.deepCopyObject(this.handle));
}
@Override
public Set<Map.Entry<String, cn.citycraft.GsonAgent.api.JsonElement>> entrySet() {
try {
final Map<String, cn.citycraft.GsonAgent.api.JsonElement> result = new LinkedTreeMap<>();
for (final Map.Entry<String, com.google.gson.JsonElement> entry : members.entrySet()) {
result.put(entry.getKey(), new JsonElementHandle(entry.getValue()));
}
return result.entrySet();
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public boolean equals(final Object o) {
return handle.equals(o);
}
@Override
public JsonElementHandle get(final String memberName) {
return new JsonElementHandle(handle.get(memberName));
}
@Override
public JsonArrayHandle getAsJsonArray(final String memberName) {
return new JsonArrayHandle(handle.getAsJsonArray(memberName));
}
@Override
public JsonObjectHandle getAsJsonObject(final String memberName) {
return new JsonObjectHandle(handle.getAsJsonObject(memberName));
}
@Override
public JsonPrimitiveHandle getAsJsonPrimitive(final String memberName) {
return new JsonPrimitiveHandle(handle.getAsJsonPrimitive(memberName));
}
@Override
public boolean has(final String memberName) {
return handle.has(memberName);
}
@Override
public int hashCode() {
return handle.hashCode();
}
@Override
public JsonElementHandle remove(final String property) {
return new JsonElementHandle(handle.remove(property));
}
}

View File

@ -0,0 +1,41 @@
package cn.citycraft.GsonAgent.normal;
import java.io.Reader;
import cn.citycraft.GsonAgent.api.exception.JsonIOException;
import cn.citycraft.GsonAgent.api.exception.JsonSyntaxException;
import cn.citycraft.GsonAgent.normal.stream.JsonReaderHandle;
public class JsonParserHandle implements cn.citycraft.GsonAgent.api.JsonParser {
public com.google.gson.JsonParser handle;
public JsonParserHandle() {
handle = new com.google.gson.JsonParser();
}
protected JsonParserHandle(final com.google.gson.JsonParser handle) {
this.handle = handle;
}
@Override
public com.google.gson.JsonParser getHandle() {
return this.handle;
}
@Override
public JsonElementHandle parse(final cn.citycraft.GsonAgent.api.stream.JsonReader json) throws JsonIOException, JsonSyntaxException {
return new JsonElementHandle(getHandle().parse(((JsonReaderHandle) json).getHandle()));
}
@Override
public JsonElementHandle parse(final Reader json) throws JsonIOException, JsonSyntaxException {
return new JsonElementHandle(getHandle().parse(json));
}
@Override
public JsonElementHandle parse(final String json) throws JsonSyntaxException {
return new JsonElementHandle(getHandle().parse(json));
}
}

View File

@ -0,0 +1,154 @@
/**
*
*/
package cn.citycraft.GsonAgent.normal;
import java.math.BigDecimal;
import java.math.BigInteger;
import cn.citycraft.GsonAgent.api.JsonPrimitive;
import cn.citycraft.GsonAgent.api.utils.Utils;
/**
*
* @since 2016年1月5日 下午1:49:49
* @author
*/
public class JsonPrimitiveHandle extends JsonElementHandle implements JsonPrimitive {
private final com.google.gson.JsonPrimitive handle;
public JsonPrimitiveHandle(final Boolean bool) {
this(new com.google.gson.JsonPrimitive(bool));
setValue(bool);
}
public JsonPrimitiveHandle(final Character c) {
this(new com.google.gson.JsonPrimitive(c));
setValue(c);
}
public JsonPrimitiveHandle(final Number number) {
this(new com.google.gson.JsonPrimitive(number));
setValue(number);
}
public JsonPrimitiveHandle(final String string) {
this(new com.google.gson.JsonPrimitive(string));
setValue(string);
}
protected JsonPrimitiveHandle(final com.google.gson.JsonPrimitive handle) {
this.handle = handle;
}
@Override
public cn.citycraft.GsonAgent.api.JsonPrimitive deepCopy() {
return new JsonPrimitiveHandle(Utils.deepCopyObject(this.handle));
}
@Override
public boolean equals(final Object obj) {
return handle.equals(obj);
}
@Override
public BigDecimal getAsBigDecimal() {
return handle.getAsBigDecimal();
}
@Override
public BigInteger getAsBigInteger() {
return handle.getAsBigInteger();
}
@Override
public boolean getAsBoolean() {
return handle.getAsBoolean();
}
@Override
public byte getAsByte() {
return handle.getAsByte();
}
@Override
public char getAsCharacter() {
return handle.getAsCharacter();
}
@Override
public double getAsDouble() {
return handle.getAsDouble();
}
@Override
public float getAsFloat() {
return handle.getAsFloat();
}
@Override
public int getAsInt() {
return handle.getAsInt();
}
@Override
public JsonArrayHandle getAsJsonArray() {
return new JsonArrayHandle(handle.getAsJsonArray());
}
@Override
public JsonObjectHandle getAsJsonObject() {
return new JsonObjectHandle(handle.getAsJsonObject());
}
@Override
public long getAsLong() {
return handle.getAsLong();
}
@Override
public Number getAsNumber() {
return handle.getAsNumber();
}
@Override
public short getAsShort() {
return handle.getAsShort();
}
@Override
public String getAsString() {
return handle.getAsString();
}
@Override
public int hashCode() {
return handle.hashCode();
}
@Override
public boolean isBoolean() {
return handle.isBoolean();
}
@Override
public boolean isNumber() {
return handle.isNumber();
}
@Override
public boolean isString() {
return handle.isString();
}
@Override
public final void setValue(final Object primitive) {
try {
handle.getClass().getDeclaredMethod("setValue", Object.class).invoke(handle, primitive);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,33 @@
package cn.citycraft.GsonAgent.normal.bind;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import cn.citycraft.GsonAgent.normal.GsonHandle;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class TypeAdapterFactoryHandle implements cn.citycraft.GsonAgent.api.TypeAdapterFactory, com.google.gson.TypeAdapterFactory {
private final TypeAdapterFactory handle;
protected TypeAdapterFactoryHandle(final TypeAdapterFactory handle) {
this.handle = handle;
}
@Override
public <T> TypeAdapterHandle<T> create(final cn.citycraft.GsonAgent.api.Gson paramGson, final cn.citycraft.GsonAgent.api.reflect.TypeToken<T> paramTypeToken) {
return new TypeAdapterHandle(this.create(((GsonHandle) paramGson).getHandle(), (TypeToken<T>) paramTypeToken.getHandle()));
}
@Override
public <T> TypeAdapter<T> create(final Gson paramGson, final TypeToken<T> paramTypeToken) {
return this.getHandle().create(paramGson, paramTypeToken);
}
public TypeAdapterFactory getHandle() {
return handle;
}
}

View File

@ -0,0 +1,78 @@
package cn.citycraft.GsonAgent.normal.bind;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import com.google.gson.TypeAdapter;
import cn.citycraft.GsonAgent.normal.JsonElementHandle;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class TypeAdapterHandle<T> implements cn.citycraft.GsonAgent.api.TypeAdapter<T> {
private final TypeAdapter<T> handle;
public TypeAdapterHandle(final TypeAdapter handle) {
this.handle = handle;
}
@Override
public T fromJson(final Reader in) throws IOException {
return getHandle().fromJson(in);
}
@Override
public T fromJson(final String json) throws IOException {
return getHandle().fromJson(json);
}
@Override
public T fromJsonTree(final cn.citycraft.GsonAgent.api.JsonElement jsonTree) {
return getHandle().fromJsonTree(((JsonElementHandle) jsonTree).getHandle());
}
@Override
public TypeAdapter<T> getHandle() {
return handle;
}
@Override
public TypeAdapterHandle<T> nullSafe() {
return new TypeAdapterHandle(this.handle);
}
@Override
public T read(final cn.citycraft.GsonAgent.api.stream.JsonReader reader) throws IOException {
if (reader.peek().name().equals("NULL")) {
reader.nextNull();
return null;
}
return this.read(reader);
}
@Override
public String toJson(final T value) throws IOException {
return getHandle().toJson(value);
}
@Override
public void toJson(final Writer out, final T value) throws IOException {
getHandle().toJson(out, value);
}
@Override
public JsonElementHandle toJsonTree(final T value) {
return new JsonElementHandle(getHandle().toJsonTree(value));
}
@Override
public void write(final cn.citycraft.GsonAgent.api.stream.JsonWriter out, final T value) throws IOException {
if (value == null) {
out.nullValue();
} else {
this.write(out, value);
}
}
}

View File

@ -0,0 +1,74 @@
package cn.citycraft.GsonAgent.normal.reflect;
import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;
import cn.citycraft.GsonAgent.api.reflect.TypeTokenAbstract;
import cn.citycraft.GsonAgent.api.utils.Utils;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class TypeTokenHandle<T> implements cn.citycraft.GsonAgent.api.reflect.TypeTokenAbstract<T> {
private TypeToken<T> handle;
public TypeTokenHandle() {
try {
handle = new TypeToken<T>() {
};
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
public TypeTokenHandle(final Type type) {
try {
handle = Utils.newInstance(TypeToken.class, Type.class, type);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
protected TypeTokenHandle(final TypeToken<T> handle) {
this.handle = handle;
}
public static <T> TypeTokenAbstract<T> get(final Class<T> type) {
return new TypeTokenHandle(TypeToken.get(type));
}
public static TypeTokenAbstract<?> get(final Type type) {
return new TypeTokenHandle(TypeToken.get(type));
}
@Override
public TypeToken<T> getHandle() {
return this.handle;
}
@Override
public Class<? super T> getRawType() {
return getHandle().getRawType();
}
@Override
public Type getType() {
return getHandle().getType();
}
@Override
public boolean isAssignableFrom(final Class<?> cls) {
return getHandle().isAssignableFrom(cls);
}
@Override
public boolean isAssignableFrom(final cn.citycraft.GsonAgent.api.reflect.TypeToken<?> token) {
return getHandle().isAssignableFrom((TypeToken<?>) token.getHandle());
}
@Override
public boolean isAssignableFrom(final Type from) {
return getHandle().isAssignableFrom(from);
}
}

View File

@ -0,0 +1,116 @@
package cn.citycraft.GsonAgent.normal.stream;
import java.io.IOException;
import java.io.Reader;
import com.google.gson.stream.JsonReader;
import cn.citycraft.GsonAgent.api.stream.JsonToken;
public class JsonReaderHandle implements cn.citycraft.GsonAgent.api.stream.JsonReader {
private final JsonReader handle;
public JsonReaderHandle(final Reader in) {
this(new JsonReader(in));
}
protected JsonReaderHandle(final JsonReader handle) {
this.handle = handle;
}
@Override
public void beginArray() throws IOException {
getHandle().beginArray();
}
@Override
public void beginObject() throws IOException {
getHandle().beginObject();
}
@Override
public void close() throws IOException {
getHandle().close();
}
@Override
public void endArray() throws IOException {
getHandle().endArray();
}
@Override
public void endObject() throws IOException {
getHandle().endObject();
}
public JsonReader getHandle() {
return handle;
}
@Override
public boolean hasNext() throws IOException {
return getHandle().hasNext();
}
@Override
public boolean isLenient() {
return getHandle().isLenient();
}
@Override
public boolean nextBoolean() throws IOException {
return getHandle().nextBoolean();
}
@Override
public double nextDouble() throws IOException {
return getHandle().nextDouble();
}
@Override
public int nextInt() throws IOException {
return getHandle().nextInt();
}
@Override
public long nextLong() throws IOException {
return getHandle().nextLong();
}
@Override
public String nextName() throws IOException {
return getHandle().nextName();
}
@Override
public void nextNull() throws IOException {
getHandle().nextNull();
}
@Override
public String nextString() throws IOException {
return getHandle().nextString();
}
@Override
public JsonToken peek() throws IOException {
return JsonToken.getByName(getHandle().peek().name());
}
@Override
public void setLenient(final boolean lenient) {
getHandle().setLenient(lenient);
}
@Override
public void skipValue() throws IOException {
getHandle().skipValue();
}
@Override
public String toString() {
return getHandle().toString();
}
}

View File

@ -0,0 +1,208 @@
package cn.citycraft.GsonAgent.normal.stream;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Field;
import com.google.gson.stream.JsonWriter;
public class JsonWriterHandle implements cn.citycraft.GsonAgent.api.stream.JsonWriter {
private static String[] REPLACEMENT_CHARS;
private static String[] HTML_SAFE_REPLACEMENT_CHARS;
static {
try {
Field field = JsonWriter.class.getDeclaredField("REPLACEMENT_CHARS");
field.setAccessible(true);
REPLACEMENT_CHARS = (String[]) field.get(null);
field = JsonWriter.class.getDeclaredField("HTML_SAFE_REPLACEMENT_CHARS");
field.setAccessible(true);
HTML_SAFE_REPLACEMENT_CHARS = (String[]) field.get(null);
} catch (final Exception e) {
e.printStackTrace();
}
}
private final JsonWriter handle;
private Writer out;
private boolean withoutQuotes = false;
public JsonWriterHandle(final Writer out) {
this(new JsonWriter(out));
this.out = out;
}
protected JsonWriterHandle(final JsonWriter handle) {
this.handle = handle;
}
@Override
public JsonWriterHandle beginArray() throws IOException {
getHandle().beginArray();
return this;
}
@Override
public JsonWriterHandle beginObject() throws IOException {
getHandle().beginObject();
return this;
}
@Override
public void close() throws IOException {
getHandle().close();
}
@Override
public JsonWriterHandle endArray() throws IOException {
getHandle().endArray();
return this;
}
@Override
public JsonWriterHandle endObject() throws IOException {
getHandle().endObject();
return this;
}
@Override
public void flush() throws IOException {
getHandle().flush();
}
public JsonWriter getHandle() {
return handle;
}
@Override
public boolean getSerializeNulls() {
return getHandle().getSerializeNulls();
}
@Override
public boolean isHtmlSafe() {
return getHandle().isHtmlSafe();
}
@Override
public boolean isLenient() {
return getHandle().isLenient();
}
@Override
public JsonWriterHandle name(final String name) throws IOException {
getHandle().name(name);
withoutQuotes = false;
return this;
}
@Override
public JsonWriterHandle nameWithoutQuotes(final String name) throws IOException {
getHandle().name(name);
withoutQuotes = true;
return this;
}
@Override
public JsonWriterHandle nullValue() throws IOException {
getHandle().nullValue();
return this;
}
@Override
public void setHtmlSafe(final boolean htmlSafe) {
getHandle().setHtmlSafe(htmlSafe);
}
@Override
public void setIndent(final String indent) {
getHandle().setIndent(indent);
}
@Override
public void setLenient(final boolean lenient) {
getHandle().setLenient(lenient);
}
@Override
public void setSerializeNulls(final boolean serializeNulls) {
getHandle().setSerializeNulls(serializeNulls);
}
@Override
public void string(final String value) throws IOException {
final String[] replacements = getHandle().isHtmlSafe() ? HTML_SAFE_REPLACEMENT_CHARS : REPLACEMENT_CHARS;
if (!this.withoutQuotes) {
this.out.write("\"");
}
int last = 0;
final int length = value.length();
for (int i = 0; i < length; i++) {
final char c = value.charAt(i);
String replacement;
if (c < '€') {
replacement = replacements[c];
if (replacement == null) {
continue;
}
} else {
if (c == '') {
replacement = "\\u2028";
} else {
if (c != '') {
continue;
}
replacement = "\\u2029";
}
}
if (last < i) {
this.out.write(value, last, i - last);
}
this.out.write(replacement);
last = i + 1;
}
if (last < length) {
this.out.write(value, last, length - last);
}
if (!this.withoutQuotes) {
this.out.write("\"");
}
withoutQuotes = false;
}
@Override
public JsonWriterHandle value(final boolean value) throws IOException {
getHandle().value(value);
return this;
}
@Override
public JsonWriterHandle value(final double value) throws IOException {
getHandle().value(value);
return this;
}
@Override
public JsonWriterHandle value(final long value) throws IOException {
getHandle().value(value);
return this;
}
@Override
public JsonWriterHandle value(final Number value) throws IOException {
getHandle().value(value);
return this;
}
@Override
public JsonWriterHandle value(final String value) throws IOException {
getHandle().value(value);
return this;
}
}