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

格式化代码 修复nameWithoutQuotes方法未生效的问题...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2016-01-12 17:06:47 +08:00
parent 628868dadc
commit 25375227f6
38 changed files with 2692 additions and 2023 deletions

View File

@ -17,60 +17,60 @@ import cn.citycraft.GsonAgent.api.stream.JsonWriter;
* @author * @author
*/ */
public interface Gson { public interface Gson {
public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException; 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(JsonElement json, Type typeOfT) throws JsonSyntaxException;
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, 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, Class<T> classOfT) throws JsonSyntaxException, JsonIOException;
public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException; public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException;
/** /**
* @param result * @param result
* Json字符串 * Json字符串
* @param clazz * @param clazz
* 模型类 * 模型类
* @return * @return
*/ */
public <T> T fromJson(final String result, Class<T> clazz) throws JsonSyntaxException; public <T> T fromJson(final String result, Class<T> clazz) throws JsonSyntaxException;
public <T> T fromJson(String json, Type typeOfT) 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(Class<T> type);
public <T> TypeAdapter<T> getAdapter(TypeToken<T> type); public <T> TypeAdapter<T> getAdapter(TypeToken<T> type);
public <T> TypeAdapter<T> getDelegateAdapter(TypeAdapterFactory skipPast, TypeToken<T> type); public <T> TypeAdapter<T> getDelegateAdapter(TypeAdapterFactory skipPast, TypeToken<T> type);
public String toJson(JsonElement jsonElement); public String toJson(JsonElement jsonElement);
public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOException; public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOException;
public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOException; public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOException;
/** /**
* 对象序列化成Json * 对象序列化成Json
* *
* @param src * @param src
* 对象 * 对象
* @return 序列化后的对象 * @return 序列化后的对象
*/ */
public String toJson(Object src); public String toJson(Object src);
public void toJson(Object src, Appendable writer) throws JsonIOException; public void toJson(Object src, Appendable writer) throws JsonIOException;
public String toJson(Object src, Type typeOfSrc); 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, Appendable writer) throws JsonIOException;
public void toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOException; public void toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOException;
public JsonElement toJsonTree(Object src); public JsonElement toJsonTree(Object src);
public JsonElement toJsonTree(Object src, Type typeOfSrc); public JsonElement toJsonTree(Object src, Type typeOfSrc);
@Override @Override
public String toString(); public String toString();
} }

View File

@ -11,59 +11,59 @@ import java.util.Iterator;
* @author * @author
*/ */
public abstract interface JsonArray extends JsonElement, Iterable<JsonElement> { public abstract interface JsonArray extends JsonElement, Iterable<JsonElement> {
public void add(JsonElement element); public void add(JsonElement element);
public void addAll(JsonArray array); public void addAll(JsonArray array);
@Override @Override
public JsonArray deepCopy(); public JsonArray deepCopy();
@Override @Override
public boolean equals(Object o); public boolean equals(Object o);
public JsonElement get(int i); public JsonElement get(int i);
@Override @Override
public BigDecimal getAsBigDecimal(); public BigDecimal getAsBigDecimal();
@Override @Override
public BigInteger getAsBigInteger(); public BigInteger getAsBigInteger();
@Override @Override
public boolean getAsBoolean(); public boolean getAsBoolean();
@Override @Override
public byte getAsByte(); public byte getAsByte();
@Override @Override
public char getAsCharacter(); public char getAsCharacter();
@Override @Override
public double getAsDouble(); public double getAsDouble();
@Override @Override
public float getAsFloat(); public float getAsFloat();
@Override @Override
public int getAsInt(); public int getAsInt();
@Override @Override
public long getAsLong(); public long getAsLong();
@Override @Override
public Number getAsNumber(); public Number getAsNumber();
@Override @Override
public short getAsShort(); public short getAsShort();
@Override @Override
public String getAsString(); public String getAsString();
@Override @Override
public int hashCode(); public int hashCode();
@Override @Override
public Iterator<JsonElement> iterator(); public Iterator<JsonElement> iterator();
public int size(); public int size();
} }

View File

@ -9,48 +9,48 @@ import java.math.BigInteger;
* @author * @author
*/ */
public abstract interface JsonElement { public abstract interface JsonElement {
public JsonElement deepCopy(); public JsonElement deepCopy();
public BigDecimal getAsBigDecimal(); public BigDecimal getAsBigDecimal();
public BigInteger getAsBigInteger(); public BigInteger getAsBigInteger();
public boolean getAsBoolean(); public boolean getAsBoolean();
public byte getAsByte(); public byte getAsByte();
public char getAsCharacter(); public char getAsCharacter();
public double getAsDouble(); public double getAsDouble();
public float getAsFloat(); public float getAsFloat();
public int getAsInt(); public int getAsInt();
public JsonArray getAsJsonArray(); public JsonArray getAsJsonArray();
public JsonNull getAsJsonNull(); public JsonNull getAsJsonNull();
public JsonObject getAsJsonObject(); public JsonObject getAsJsonObject();
public JsonPrimitive getAsJsonPrimitive(); public JsonPrimitive getAsJsonPrimitive();
public long getAsLong(); public long getAsLong();
public Number getAsNumber(); public Number getAsNumber();
public short getAsShort(); public short getAsShort();
public String getAsString(); public String getAsString();
public boolean isJsonArray(); public boolean isJsonArray();
public boolean isJsonNull(); public boolean isJsonNull();
public boolean isJsonObject(); public boolean isJsonObject();
public boolean isJsonPrimitive(); public boolean isJsonPrimitive();
@Override @Override
public String toString(); public String toString();
} }

View File

@ -6,12 +6,12 @@ package cn.citycraft.GsonAgent.api;
* @author * @author
*/ */
public abstract interface JsonNull extends JsonElement { public abstract interface JsonNull extends JsonElement {
@Override @Override
public JsonNull deepCopy(); public JsonNull deepCopy();
@Override @Override
public boolean equals(Object other); public boolean equals(Object other);
@Override @Override
public int hashCode(); public int hashCode();
} }

View File

@ -9,36 +9,36 @@ import java.util.Set;
* @author * @author
*/ */
public abstract interface JsonObject extends JsonElement { public abstract interface JsonObject extends JsonElement {
public void add(String property, JsonElement value); public void add(String property, JsonElement value);
public void addProperty(String property, Boolean value); public void addProperty(String property, Boolean value);
public void addProperty(String property, Character value); public void addProperty(String property, Character value);
public void addProperty(String property, Number value); public void addProperty(String property, Number value);
public void addProperty(String property, String value); public void addProperty(String property, String value);
@Override @Override
public JsonObject deepCopy(); public JsonObject deepCopy();
public Set<Map.Entry<String, JsonElement>> entrySet(); public Set<Map.Entry<String, JsonElement>> entrySet();
@Override @Override
public boolean equals(Object o); public boolean equals(Object o);
public JsonElement get(String memberName); public JsonElement get(String memberName);
public JsonArray getAsJsonArray(String memberName); public JsonArray getAsJsonArray(String memberName);
public JsonObject getAsJsonObject(String memberName); public JsonObject getAsJsonObject(String memberName);
public JsonPrimitive getAsJsonPrimitive(String memberName); public JsonPrimitive getAsJsonPrimitive(String memberName);
public boolean has(String memberName); public boolean has(String memberName);
@Override @Override
public int hashCode(); public int hashCode();
public JsonElement remove(String property); public JsonElement remove(String property);
} }

View File

@ -10,10 +10,10 @@ import cn.citycraft.GsonAgent.api.utils.HandleInterface;
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public abstract interface JsonParser extends HandleInterface { public abstract interface JsonParser extends HandleInterface {
public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException; public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException;
public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException; public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException;
public JsonElement parse(String json) throws JsonSyntaxException; public JsonElement parse(String json) throws JsonSyntaxException;
} }

View File

@ -9,56 +9,56 @@ import java.math.BigInteger;
* @author * @author
*/ */
public abstract interface JsonPrimitive extends JsonElement { public abstract interface JsonPrimitive extends JsonElement {
@Override @Override
public JsonPrimitive deepCopy(); public JsonPrimitive deepCopy();
@Override @Override
public boolean equals(Object obj); public boolean equals(Object obj);
@Override @Override
public BigDecimal getAsBigDecimal(); public BigDecimal getAsBigDecimal();
@Override @Override
public BigInteger getAsBigInteger(); public BigInteger getAsBigInteger();
@Override @Override
public boolean getAsBoolean(); public boolean getAsBoolean();
@Override @Override
public byte getAsByte(); public byte getAsByte();
@Override @Override
public char getAsCharacter(); public char getAsCharacter();
@Override @Override
public double getAsDouble(); public double getAsDouble();
@Override @Override
public float getAsFloat(); public float getAsFloat();
@Override @Override
public int getAsInt(); public int getAsInt();
@Override @Override
public long getAsLong(); public long getAsLong();
@Override @Override
public Number getAsNumber(); public Number getAsNumber();
@Override @Override
public short getAsShort(); public short getAsShort();
@Override @Override
public String getAsString(); public String getAsString();
@Override @Override
public int hashCode(); public int hashCode();
public boolean isBoolean(); public boolean isBoolean();
public boolean isNumber(); public boolean isNumber();
public boolean isString(); public boolean isString();
void setValue(Object primitive); void setValue(Object primitive);
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,50 +6,94 @@ import java.io.IOException;
public abstract interface JsonWriter extends Closeable, Flushable { public abstract interface JsonWriter extends Closeable, Flushable {
public JsonWriter beginArray() throws IOException; /**
* An array with no elements requires no separators or newlines before it is
* closed.
*/
static final int EMPTY_ARRAY = 1;
public JsonWriter beginObject() throws IOException; /**
* A array with at least one value requires a comma and newline before the
* next element.
*/
static final int NONEMPTY_ARRAY = 2;
@Override /**
public void close() throws IOException; * An object with no name/value pairs requires no separators or newlines
* before it is closed.
*/
static final int EMPTY_OBJECT = 3;
public JsonWriter endArray() throws IOException; /**
* An object whose most recent element is a key. The next element must be a
* value.
*/
static final int DANGLING_NAME = 4;
public JsonWriter endObject() throws IOException; /**
* An object with at least one name/value pair requires a comma and newline
* before the next element.
*/
static final int NONEMPTY_OBJECT = 5;
@Override /**
public void flush() throws IOException; * No object or array has been started.
*/
static final int EMPTY_DOCUMENT = 6;
public boolean getSerializeNulls(); /**
* A document with at an array or object.
*/
static final int NONEMPTY_DOCUMENT = 7;
public boolean isHtmlSafe(); /**
* A document that's been closed and cannot be accessed.
*/
static final int CLOSED = 8;
public boolean isLenient(); public JsonWriter beginArray() throws IOException;
public JsonWriter name(String name) throws IOException; public JsonWriter beginObject() throws IOException;
public JsonWriter nameWithoutQuotes(String name) throws IOException; @Override
public void close() throws IOException;
public JsonWriter nullValue() throws IOException; public JsonWriter endArray() throws IOException;
public void setHtmlSafe(boolean htmlSafe); public JsonWriter endObject() throws IOException;
public void setIndent(String indent); @Override
public void flush() throws IOException;
public void setLenient(boolean lenient); public boolean getSerializeNulls();
public void setSerializeNulls(boolean serializeNulls); public boolean isHtmlSafe();
public JsonWriter value(boolean value) throws IOException; public boolean isLenient();
public JsonWriter value(double value) throws IOException; public JsonWriter name(String name) throws IOException;
public JsonWriter value(long value) throws IOException; public JsonWriter nameWithoutQuotes(String name) throws IOException;
public JsonWriter value(Number value) throws IOException; public JsonWriter nullValue() throws IOException;
public JsonWriter value(String value) throws IOException; public void setHtmlSafe(boolean htmlSafe);
void string(String value) throws IOException; 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 stringExtend(String value) throws IOException;
} }

View File

@ -4,6 +4,7 @@
package cn.citycraft.GsonAgent.api.utils; package cn.citycraft.GsonAgent.api.utils;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
/** /**
* Gson代理工具类(来自7老板) * Gson代理工具类(来自7老板)
@ -13,42 +14,110 @@ import java.lang.reflect.Constructor;
*/ */
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })
public class Utils { public class Utils {
public static <T> T deepCopyObject(final T obj) { public static final String[] REPLACEMENT_CHARS;
try { public static final String[] HTML_SAFE_REPLACEMENT_CHARS;
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) { static {
try { REPLACEMENT_CHARS = new String[128];
final Constructor<T> constructor = clzz.getDeclaredConstructor(); for (int i = 0; i <= 0x1f; i++) {
constructor.setAccessible(true); REPLACEMENT_CHARS[i] = String.format("\\u%04x", i);
return constructor.newInstance(); }
} catch (final Exception e) { REPLACEMENT_CHARS['"'] = "\\\"";
throw new RuntimeException(e); REPLACEMENT_CHARS['\\'] = "\\\\";
} REPLACEMENT_CHARS['\t'] = "\\t";
} REPLACEMENT_CHARS['\b'] = "\\b";
REPLACEMENT_CHARS['\n'] = "\\n";
REPLACEMENT_CHARS['\r'] = "\\r";
REPLACEMENT_CHARS['\f'] = "\\f";
HTML_SAFE_REPLACEMENT_CHARS = REPLACEMENT_CHARS.clone();
HTML_SAFE_REPLACEMENT_CHARS['<'] = "\\u003c";
HTML_SAFE_REPLACEMENT_CHARS['>'] = "\\u003e";
HTML_SAFE_REPLACEMENT_CHARS['&'] = "\\u0026";
HTML_SAFE_REPLACEMENT_CHARS['='] = "\\u003d";
HTML_SAFE_REPLACEMENT_CHARS['\''] = "\\u0027";
}
public static <T> T newInstance(final Class<T> clzz, final Class[] types, final Object[] args) { public static <T> T deepCopyObject(final T obj) {
if (args.length == 0 || types.length == 0) { try {
return newInstance(clzz); return (T) obj.getClass().getDeclaredMethod("deepCopy").invoke(obj);
} } catch (final Exception e) {
try { throw new RuntimeException(e);
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) { public static Field getDeclareField(final Class clzz, final String fieldName) throws Exception {
final Class[] argClasses = new Class[args.length]; final Field field = clzz.getDeclaredField(fieldName);
for (int i = 0; i < argClasses.length; i++) { field.setAccessible(true);
argClasses[i] = args[i].getClass(); return field;
} }
return newInstance(clzz, argClasses, args);
} public static Object invokeField(final Object object, final Field field) {
try {
return field.get(object);
} catch (final Exception e) {
}
return null;
}
public static int[] invokeIntArrayField(final Object object, final Field field) {
try {
return (int[]) invokeField(object, field);
} catch (final Exception e) {
}
return null;
}
public static int invokeIntField(final Object object, final Field field) {
try {
return ((Number) invokeField(object, field)).intValue();
} catch (final Exception e) {
}
return 0;
}
public static String invokeStringField(final Object object, final Field field) {
try {
return (String) invokeField(object, field);
} catch (final Exception e) {
}
return null;
}
public static void modifyField(final Object object, final Field field, final Object value) {
try {
field.set(object, value);
} catch (final Exception 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

@ -22,114 +22,114 @@ import cn.citycraft.GsonAgent.nms.stream.JsonWriterHandle;
*/ */
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })
public class GsonHandle implements Gson { public class GsonHandle implements Gson {
private final net.minecraft.util.com.google.gson.Gson handle; private final net.minecraft.util.com.google.gson.Gson handle;
public GsonHandle() { public GsonHandle() {
handle = new net.minecraft.util.com.google.gson.Gson(); handle = new net.minecraft.util.com.google.gson.Gson();
} }
@Override @Override
public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Class<T> classOfT) throws JsonSyntaxException { public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Class<T> classOfT) throws JsonSyntaxException {
return getHandle().fromJson(((JsonElementHandle) json).getHandle(), classOfT); return getHandle().fromJson(((JsonElementHandle) json).getHandle(), classOfT);
} }
@Override @Override
public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Type typeOfT) throws JsonSyntaxException { public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Type typeOfT) throws JsonSyntaxException {
return getHandle().fromJson(((JsonElementHandle) json).getHandle(), typeOfT); return getHandle().fromJson(((JsonElementHandle) json).getHandle(), typeOfT);
} }
@Override @Override
public <T> T fromJson(final cn.citycraft.GsonAgent.api.stream.JsonReader reader, final Type typeOfT) throws JsonIOException, JsonSyntaxException { 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); return getHandle().fromJson(((JsonReaderHandle) reader).getHandle(), typeOfT);
} }
@Override @Override
public <T> T fromJson(final Reader json, final Class<T> classOfT) throws JsonSyntaxException, JsonIOException { public <T> T fromJson(final Reader json, final Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
return getHandle().fromJson(json, classOfT); return getHandle().fromJson(json, classOfT);
} }
@Override @Override
public <T> T fromJson(final Reader json, final Type typeOfT) throws JsonIOException, JsonSyntaxException { public <T> T fromJson(final Reader json, final Type typeOfT) throws JsonIOException, JsonSyntaxException {
return getHandle().fromJson(json, typeOfT); return getHandle().fromJson(json, typeOfT);
} }
@Override @Override
public <T> T fromJson(final String json, final Class<T> classOfT) throws JsonSyntaxException { public <T> T fromJson(final String json, final Class<T> classOfT) throws JsonSyntaxException {
return getHandle().fromJson(json, classOfT); return getHandle().fromJson(json, classOfT);
} }
@Override @Override
public <T> T fromJson(final String json, final Type typeOfT) throws JsonSyntaxException { public <T> T fromJson(final String json, final Type typeOfT) throws JsonSyntaxException {
return getHandle().fromJson(json, typeOfT); return getHandle().fromJson(json, typeOfT);
} }
@Override @Override
public <T> TypeAdapterHandle<T> getAdapter(final Class<T> type) { public <T> TypeAdapterHandle<T> getAdapter(final Class<T> type) {
return new TypeAdapterHandle(getHandle().getAdapter(type)); return new TypeAdapterHandle(getHandle().getAdapter(type));
} }
@Override @Override
public <T> TypeAdapterHandle<T> getAdapter(final cn.citycraft.GsonAgent.api.reflect.TypeToken<T> type) { 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())); return new TypeAdapterHandle(getHandle().getAdapter((net.minecraft.util.com.google.gson.reflect.TypeToken<T>) type.getHandle()));
} }
@Override @Override
public <T> TypeAdapterHandle<T> getDelegateAdapter(final cn.citycraft.GsonAgent.api.TypeAdapterFactory skipPast, final cn.citycraft.GsonAgent.api.reflect.TypeToken<T> type) { 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())); 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() { public net.minecraft.util.com.google.gson.Gson getHandle() {
return handle; return handle;
} }
@Override @Override
public String toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement) { public String toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement) {
return getHandle().toJson(((JsonElementHandle) jsonElement).getHandle()); return getHandle().toJson(((JsonElementHandle) jsonElement).getHandle());
} }
@Override @Override
public void toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement, final Appendable writer) throws JsonIOException { public void toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement, final Appendable writer) throws JsonIOException {
getHandle().toJson(((JsonElementHandle) jsonElement).getHandle(), writer); getHandle().toJson(((JsonElementHandle) jsonElement).getHandle(), writer);
} }
@Override @Override
public void toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement, final cn.citycraft.GsonAgent.api.stream.JsonWriter writer) throws JsonIOException { 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()); getHandle().toJson(((JsonElementHandle) jsonElement).getHandle(), ((JsonWriterHandle) writer).getHandle());
} }
@Override @Override
public String toJson(final Object src) { public String toJson(final Object src) {
return getHandle().toJson(src); return getHandle().toJson(src);
} }
@Override @Override
public void toJson(final Object src, final Appendable writer) throws JsonIOException { public void toJson(final Object src, final Appendable writer) throws JsonIOException {
getHandle().toJson(src, writer); getHandle().toJson(src, writer);
} }
@Override @Override
public String toJson(final Object src, final Type typeOfSrc) { public String toJson(final Object src, final Type typeOfSrc) {
return getHandle().toJson(src, typeOfSrc); return getHandle().toJson(src, typeOfSrc);
} }
@Override @Override
public void toJson(final Object src, final Type typeOfSrc, final Appendable writer) throws JsonIOException { public void toJson(final Object src, final Type typeOfSrc, final Appendable writer) throws JsonIOException {
getHandle().toJson(src, typeOfSrc, writer); getHandle().toJson(src, typeOfSrc, writer);
} }
@Override @Override
public void toJson(final Object src, final Type typeOfSrc, final cn.citycraft.GsonAgent.api.stream.JsonWriter writer) throws JsonIOException { 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()); getHandle().toJson(src, typeOfSrc, ((JsonWriterHandle) writer).getHandle());
} }
@Override @Override
public JsonElementHandle toJsonTree(final Object src) { public JsonElementHandle toJsonTree(final Object src) {
return new JsonElementHandle(getHandle().toJsonTree(src)); return new JsonElementHandle(getHandle().toJsonTree(src));
} }
@Override @Override
public JsonElementHandle toJsonTree(final Object src, final Type typeOfSrc) { public JsonElementHandle toJsonTree(final Object src, final Type typeOfSrc) {
return new JsonElementHandle(getHandle().toJsonTree(src, typeOfSrc)); return new JsonElementHandle(getHandle().toJsonTree(src, typeOfSrc));
} }
} }

View File

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

View File

@ -17,127 +17,127 @@ import cn.citycraft.GsonAgent.api.utils.Utils;
*/ */
public class JsonElementHandle implements JsonElement { public class JsonElementHandle implements JsonElement {
private net.minecraft.util.com.google.gson.JsonElement handle; private net.minecraft.util.com.google.gson.JsonElement handle;
public JsonElementHandle() { public JsonElementHandle() {
} }
public JsonElementHandle(final net.minecraft.util.com.google.gson.JsonElement handle) { public JsonElementHandle(final net.minecraft.util.com.google.gson.JsonElement handle) {
this.handle = handle; this.handle = handle;
} }
@Override @Override
public cn.citycraft.GsonAgent.api.JsonElement deepCopy() { public cn.citycraft.GsonAgent.api.JsonElement deepCopy() {
return new JsonElementHandle(Utils.deepCopyObject(this.getHandle())); return new JsonElementHandle(Utils.deepCopyObject(this.getHandle()));
} }
@Override @Override
public BigDecimal getAsBigDecimal() { public BigDecimal getAsBigDecimal() {
return getHandle().getAsBigDecimal(); return getHandle().getAsBigDecimal();
} }
@Override @Override
public BigInteger getAsBigInteger() { public BigInteger getAsBigInteger() {
return getHandle().getAsBigInteger(); return getHandle().getAsBigInteger();
} }
@Override @Override
public boolean getAsBoolean() { public boolean getAsBoolean() {
return getHandle().getAsBoolean(); return getHandle().getAsBoolean();
} }
@Override @Override
public byte getAsByte() { public byte getAsByte() {
return getHandle().getAsByte(); return getHandle().getAsByte();
} }
@Override @Override
public char getAsCharacter() { public char getAsCharacter() {
return getHandle().getAsCharacter(); return getHandle().getAsCharacter();
} }
@Override @Override
public double getAsDouble() { public double getAsDouble() {
return getHandle().getAsDouble(); return getHandle().getAsDouble();
} }
@Override @Override
public float getAsFloat() { public float getAsFloat() {
return getHandle().getAsFloat(); return getHandle().getAsFloat();
} }
@Override @Override
public int getAsInt() { public int getAsInt() {
return getHandle().getAsInt(); return getHandle().getAsInt();
} }
@Override @Override
public JsonArrayHandle getAsJsonArray() { public JsonArrayHandle getAsJsonArray() {
return new JsonArrayHandle(getHandle().getAsJsonArray()); return new JsonArrayHandle(getHandle().getAsJsonArray());
} }
@Override @Override
public JsonNullHandle getAsJsonNull() { public JsonNullHandle getAsJsonNull() {
return new JsonNullHandle(getHandle().getAsJsonNull()); return new JsonNullHandle(getHandle().getAsJsonNull());
} }
@Override @Override
public JsonObjectHandle getAsJsonObject() { public JsonObjectHandle getAsJsonObject() {
return new JsonObjectHandle(getHandle().getAsJsonObject()); return new JsonObjectHandle(getHandle().getAsJsonObject());
} }
@Override @Override
public JsonPrimitiveHandle getAsJsonPrimitive() { public JsonPrimitiveHandle getAsJsonPrimitive() {
return new JsonPrimitiveHandle(getHandle().getAsJsonPrimitive()); return new JsonPrimitiveHandle(getHandle().getAsJsonPrimitive());
} }
@Override @Override
public long getAsLong() { public long getAsLong() {
return getHandle().getAsLong(); return getHandle().getAsLong();
} }
@Override @Override
public Number getAsNumber() { public Number getAsNumber() {
return getHandle().getAsNumber(); return getHandle().getAsNumber();
} }
@Override @Override
public short getAsShort() { public short getAsShort() {
return getHandle().getAsShort(); return getHandle().getAsShort();
} }
@Override @Override
public String getAsString() { public String getAsString() {
return getHandle().getAsString(); return getHandle().getAsString();
} }
public net.minecraft.util.com.google.gson.JsonElement getHandle() { public net.minecraft.util.com.google.gson.JsonElement getHandle() {
return handle; return handle;
} }
@Override @Override
public boolean isJsonArray() { public boolean isJsonArray() {
return getHandle().isJsonArray(); return getHandle().isJsonArray();
} }
@Override @Override
public boolean isJsonNull() { public boolean isJsonNull() {
return getHandle().isJsonNull(); return getHandle().isJsonNull();
} }
@Override @Override
public boolean isJsonObject() { public boolean isJsonObject() {
return getHandle().isJsonObject(); return getHandle().isJsonObject();
} }
@Override @Override
public boolean isJsonPrimitive() { public boolean isJsonPrimitive() {
return getHandle().isJsonPrimitive(); return getHandle().isJsonPrimitive();
} }
@Override @Override
public String toString() { public String toString() {
return getHandle().toString(); return getHandle().toString();
} }
} }

View File

@ -9,28 +9,28 @@ import cn.citycraft.GsonAgent.api.JsonNull;
*/ */
public class JsonNullHandle extends JsonElementHandle implements JsonNull { public class JsonNullHandle extends JsonElementHandle implements JsonNull {
private final net.minecraft.util.com.google.gson.JsonNull handle; private final net.minecraft.util.com.google.gson.JsonNull handle;
public JsonNullHandle() { public JsonNullHandle() {
this(net.minecraft.util.com.google.gson.JsonNull.INSTANCE); this(net.minecraft.util.com.google.gson.JsonNull.INSTANCE);
} }
protected JsonNullHandle(final net.minecraft.util.com.google.gson.JsonNull handle) { protected JsonNullHandle(final net.minecraft.util.com.google.gson.JsonNull handle) {
this.handle = handle; this.handle = handle;
} }
@Override @Override
public cn.citycraft.GsonAgent.api.JsonNull deepCopy() { public cn.citycraft.GsonAgent.api.JsonNull deepCopy() {
return new JsonNullHandle(); return new JsonNullHandle();
} }
@Override @Override
public boolean equals(final Object other) { public boolean equals(final Object other) {
return handle.equals(other); return handle.equals(other);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return handle.hashCode(); return handle.hashCode();
} }
} }

View File

@ -16,116 +16,116 @@ import net.minecraft.util.com.google.gson.internal.LinkedTreeMap;
*/ */
public class JsonObjectHandle extends JsonElementHandle implements JsonObject { public class JsonObjectHandle extends JsonElementHandle implements JsonObject {
private static Field membersField; private static Field membersField;
static { static {
try { try {
membersField = net.minecraft.util.com.google.gson.JsonObject.class.getDeclaredField("members"); membersField = net.minecraft.util.com.google.gson.JsonObject.class.getDeclaredField("members");
membersField.setAccessible(true); membersField.setAccessible(true);
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private final net.minecraft.util.com.google.gson.JsonObject handle; private final net.minecraft.util.com.google.gson.JsonObject handle;
private AbstractMap<String, net.minecraft.util.com.google.gson.JsonElement> members; private AbstractMap<String, net.minecraft.util.com.google.gson.JsonElement> members;
public JsonObjectHandle() { public JsonObjectHandle() {
this(new net.minecraft.util.com.google.gson.JsonObject()); this(new net.minecraft.util.com.google.gson.JsonObject());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected JsonObjectHandle(final net.minecraft.util.com.google.gson.JsonObject handle) { protected JsonObjectHandle(final net.minecraft.util.com.google.gson.JsonObject handle) {
this.handle = handle; this.handle = handle;
try { try {
members = (AbstractMap<String, net.minecraft.util.com.google.gson.JsonElement>) membersField.get(this.handle); members = (AbstractMap<String, net.minecraft.util.com.google.gson.JsonElement>) membersField.get(this.handle);
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override @Override
public void add(final String property, final cn.citycraft.GsonAgent.api.JsonElement value) { public void add(final String property, final cn.citycraft.GsonAgent.api.JsonElement value) {
handle.add(property, ((JsonElementHandle) value).getHandle()); handle.add(property, ((JsonElementHandle) value).getHandle());
} }
@Override @Override
public void addProperty(final String property, final Boolean value) { public void addProperty(final String property, final Boolean value) {
handle.addProperty(property, value); handle.addProperty(property, value);
} }
@Override @Override
public void addProperty(final String property, final Character value) { public void addProperty(final String property, final Character value) {
handle.addProperty(property, value); handle.addProperty(property, value);
} }
@Override @Override
public void addProperty(final String property, final Number value) { public void addProperty(final String property, final Number value) {
handle.addProperty(property, value); handle.addProperty(property, value);
} }
@Override @Override
public void addProperty(final String property, final String value) { public void addProperty(final String property, final String value) {
handle.addProperty(property, value); handle.addProperty(property, value);
} }
@Override @Override
public cn.citycraft.GsonAgent.api.JsonObject deepCopy() { public cn.citycraft.GsonAgent.api.JsonObject deepCopy() {
return new JsonObjectHandle(Utils.deepCopyObject(this.handle)); return new JsonObjectHandle(Utils.deepCopyObject(this.handle));
} }
@Override @Override
public Set<Map.Entry<String, cn.citycraft.GsonAgent.api.JsonElement>> entrySet() { public Set<Map.Entry<String, cn.citycraft.GsonAgent.api.JsonElement>> entrySet() {
try { try {
final Map<String, cn.citycraft.GsonAgent.api.JsonElement> result = new LinkedTreeMap<>(); 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()) { for (final Map.Entry<String, net.minecraft.util.com.google.gson.JsonElement> entry : members.entrySet()) {
result.put(entry.getKey(), new JsonElementHandle(entry.getValue())); result.put(entry.getKey(), new JsonElementHandle(entry.getValue()));
} }
return result.entrySet(); return result.entrySet();
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;
} }
@Override @Override
public boolean equals(final Object o) { public boolean equals(final Object o) {
return handle.equals(o); return handle.equals(o);
} }
@Override @Override
public JsonElementHandle get(final String memberName) { public JsonElementHandle get(final String memberName) {
return new JsonElementHandle(handle.get(memberName)); return new JsonElementHandle(handle.get(memberName));
} }
@Override @Override
public JsonArrayHandle getAsJsonArray(final String memberName) { public JsonArrayHandle getAsJsonArray(final String memberName) {
return new JsonArrayHandle(handle.getAsJsonArray(memberName)); return new JsonArrayHandle(handle.getAsJsonArray(memberName));
} }
@Override @Override
public JsonObjectHandle getAsJsonObject(final String memberName) { public JsonObjectHandle getAsJsonObject(final String memberName) {
return new JsonObjectHandle(handle.getAsJsonObject(memberName)); return new JsonObjectHandle(handle.getAsJsonObject(memberName));
} }
@Override @Override
public JsonPrimitiveHandle getAsJsonPrimitive(final String memberName) { public JsonPrimitiveHandle getAsJsonPrimitive(final String memberName) {
return new JsonPrimitiveHandle(handle.getAsJsonPrimitive(memberName)); return new JsonPrimitiveHandle(handle.getAsJsonPrimitive(memberName));
} }
@Override @Override
public boolean has(final String memberName) { public boolean has(final String memberName) {
return handle.has(memberName); return handle.has(memberName);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return handle.hashCode(); return handle.hashCode();
} }
@Override @Override
public JsonElementHandle remove(final String property) { public JsonElementHandle remove(final String property) {
return new JsonElementHandle(handle.remove(property)); return new JsonElementHandle(handle.remove(property));
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,205 +4,483 @@ import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import cn.citycraft.GsonAgent.api.utils.Utils;
import net.minecraft.util.com.google.gson.stream.JsonWriter; import net.minecraft.util.com.google.gson.stream.JsonWriter;
public class JsonWriterHandle implements cn.citycraft.GsonAgent.api.stream.JsonWriter { public class JsonWriterHandle extends JsonWriter implements cn.citycraft.GsonAgent.api.stream.JsonWriter {
/*
* From RFC 4627, "All Unicode characters may be placed within the
* quotation marks except for the characters that must be escaped:
* quotation mark, reverse solidus, and the control characters
* (U+0000 through U+001F)."
*
* We also escape '\u2028' and '\u2029', which JavaScript interprets as
* newline characters. This prevents eval() from failing with a syntax
* error. http://code.google.com/p/google-gson/issues/detail?id=341
*/
private static Field stackField, stackSizeField, indentField, separatorField;
private static String[] REPLACEMENT_CHARS; static {
final Class<JsonWriter> clzz = JsonWriter.class;
try {
stackField = Utils.getDeclareField(clzz, "stack");
stackSizeField = Utils.getDeclareField(clzz, "stackSize");
indentField = Utils.getDeclareField(clzz, "indent");
separatorField = Utils.getDeclareField(clzz, "separator");
} catch (final Exception e) {
e.printStackTrace();
}
}
private static String[] HTML_SAFE_REPLACEMENT_CHARS; /**
* The output data, containing at most one top-level array or object.
*/
private final Writer out;
private String deferredName;
static { private boolean withoutQuotes = false;
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; /**
* Creates a new instance that writes a JSON-encoded stream to {@code out}.
* For best performance, ensure {@link Writer} is buffered; wrapping in
* {@link java.io.BufferedWriter BufferedWriter} if necessary.
*
* @param out
*/
public JsonWriterHandle(final Writer out) {
super(out);
this.out = out;
}
private Writer out; /**
* Begins encoding a new array. Each call to this method must be paired with
* a call to {@link #endArray}.
*
* @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle beginArray() throws IOException {
writeDeferredName();
return open(EMPTY_ARRAY, "[");
}
private boolean withoutQuotes = false; /**
* Begins encoding a new object. Each call to this method must be paired
* with a call to {@link #endObject}.
*
* @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle beginObject() throws IOException {
writeDeferredName();
return open(EMPTY_OBJECT, "{");
}
public JsonWriterHandle(final Writer out) { /**
this(new JsonWriter(out)); * Flushes and closes this writer and the underlying {@link Writer}.
this.out = out; *
} * @throws IOException
* if the JSON document is incomplete.
*/
@Override
public void close() throws IOException {
out.close();
final int size = this.getStackSize();
if (size > 1 || size == 1 && this.getStack()[size - 1] != NONEMPTY_DOCUMENT) {
throw new IOException("Incomplete document");
}
this.setStackSize(0);
}
protected JsonWriterHandle(final JsonWriter handle) { /**
this.handle = handle; * Ends encoding the current array.
} *
* @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle endArray() throws IOException {
return close(EMPTY_ARRAY, NONEMPTY_ARRAY, "]");
}
@Override /**
public JsonWriterHandle beginArray() throws IOException { * Ends encoding the current object.
getHandle().beginArray(); *
return this; * @return this writer.
} * @throws java.io.IOException
*/
@Override
public JsonWriterHandle endObject() throws IOException {
return close(EMPTY_OBJECT, NONEMPTY_OBJECT, "}");
}
@Override /**
public JsonWriterHandle beginObject() throws IOException { * Ensures all buffered data is written to the underlying {@link Writer} and
getHandle().beginObject(); * flushes that writer.
return this; *
} * @throws java.io.IOException
*/
@Override
public void flush() throws IOException {
if (this.getStackSize() == 0) {
throw new IllegalStateException("JsonWriterHandle is closed.");
}
out.flush();
}
@Override public JsonWriterHandle getHandle() {
public void close() throws IOException { return this;
getHandle().close(); }
}
@Override public String getIndent() {
public JsonWriterHandle endArray() throws IOException { return Utils.invokeStringField(this, indentField);
getHandle().endArray(); }
return this;
}
@Override public String getSeparator() {
public JsonWriterHandle endObject() throws IOException { return Utils.invokeStringField(this, separatorField);
getHandle().endObject(); }
return this;
}
@Override public int[] getStack() {
public void flush() throws IOException { return Utils.invokeIntArrayField(this, stackField);
getHandle().flush(); }
}
public JsonWriter getHandle() { public int getStackSize() {
return handle; return Utils.invokeIntField(this, stackSizeField);
} }
@Override /**
public boolean getSerializeNulls() { * Encodes the property name.
return getHandle().getSerializeNulls(); *
} * @param name
* the name of the forthcoming value. May not be null.
* @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle name(final String name) throws IOException {
if (name == null) {
throw new NullPointerException("name == null");
}
if (deferredName != null) {
throw new IllegalStateException();
}
if (this.getStackSize() == 0) {
throw new IllegalStateException("JsonWriterHandle is closed.");
}
deferredName = name;
withoutQuotes = false;
return this;
}
@Override @Override
public boolean isHtmlSafe() { public JsonWriterHandle nameWithoutQuotes(final String name) throws IOException {
return getHandle().isHtmlSafe(); this.name(name);
} withoutQuotes = true;
return this;
}
@Override /**
public boolean isLenient() { * Encodes {@code null}.
return getHandle().isLenient(); *
} * @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle nullValue() throws IOException {
if (deferredName != null) {
if (this.getSerializeNulls()) {
writeDeferredName();
} else {
deferredName = null;
return this; // skip the name and the value
}
}
beforeValue(false);
out.write("null");
return this;
}
@Override public void setSeparator(final String separator) {
public JsonWriterHandle name(final String name) throws IOException { Utils.modifyField(this, separatorField, separator);
getHandle().name(name); }
withoutQuotes = false;
return this;
}
@Override public void setStack(final int index, final int value) {
public JsonWriterHandle nameWithoutQuotes(final String name) throws IOException { final int[] newStack = this.getStack();
getHandle().name(name); newStack[index] = value;
withoutQuotes = true; this.setStack(newStack);
return this; }
}
@Override public void setStack(final int[] stack) {
public JsonWriterHandle nullValue() throws IOException { Utils.modifyField(this, stackField, stack);
getHandle().nullValue(); }
return this;
}
@Override public void setStackSize(final int stackSize) {
public void setHtmlSafe(final boolean htmlSafe) { Utils.modifyField(this, stackSizeField, stackSize);
getHandle().setHtmlSafe(htmlSafe); }
}
@Override @Override
public void setIndent(final String indent) { public void stringExtend(final String value) throws IOException {
getHandle().setIndent(indent); final String[] replacements = this.isHtmlSafe() ? Utils.HTML_SAFE_REPLACEMENT_CHARS : Utils.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 < 128) {
replacement = replacements[c];
if (replacement == null) {
continue;
}
} else if (c == '\u2028') {
replacement = "\\u2028";
} else if (c == '\u2029') {
replacement = "\\u2029";
} else {
continue;
}
if (last < i) {
out.write(value, last, i - last);
}
out.write(replacement);
last = i + 1;
}
if (last < length) {
out.write(value, last, length - last);
}
if (!this.withoutQuotes) {
this.out.write("\"");
}
withoutQuotes = false;
}
@Override /**
public void setLenient(final boolean lenient) { * Encodes {@code value}.
getHandle().setLenient(lenient); *
} * @param value
* @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle value(final boolean value) throws IOException {
writeDeferredName();
beforeValue(false);
out.write(value ? "true" : "false");
return this;
}
@Override /**
public void setSerializeNulls(final boolean serializeNulls) { * Encodes {@code value}.
getHandle().setSerializeNulls(serializeNulls); *
} * @param value
* a finite value. May not be {@link Double#isNaN() NaNs} or
* {@link Double#isInfinite() infinities}.
* @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle value(final double value) throws IOException {
if (Double.isNaN(value) || Double.isInfinite(value)) {
throw new IllegalArgumentException("Numeric values must be finite, but was " + value);
}
writeDeferredName();
beforeValue(false);
out.append(Double.toString(value));
return this;
}
@Override /**
public void string(final String value) throws IOException { * Encodes {@code value}.
final String[] replacements = getHandle().isHtmlSafe() ? HTML_SAFE_REPLACEMENT_CHARS : REPLACEMENT_CHARS; *
if (!this.withoutQuotes) { * @param value
this.out.write("\""); * @return this writer.
} * @throws java.io.IOException
int last = 0; */
final int length = value.length(); @Override
for (int i = 0; i < length; i++) { public JsonWriterHandle value(final long value) throws IOException {
final char c = value.charAt(i); writeDeferredName();
String replacement; beforeValue(false);
if (c < '€') { out.write(Long.toString(value));
replacement = replacements[c]; return this;
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); * Encodes {@code value}.
} *
this.out.write(replacement); * @param value
last = i + 1; * a finite value. May not be {@link Double#isNaN() NaNs} or
} * {@link Double#isInfinite() infinities}.
if (last < length) { * @return this writer.
this.out.write(value, last, length - last); * @throws java.io.IOException
} */
if (!this.withoutQuotes) { @Override
this.out.write("\""); public JsonWriterHandle value(final Number value) throws IOException {
} if (value == null) {
withoutQuotes = false; return nullValue();
} }
writeDeferredName();
final String string = value.toString();
if (!this.isLenient() && (string.equals("-Infinity") || string.equals("Infinity") || string.equals("NaN"))) {
throw new IllegalArgumentException("Numeric values must be finite, but was " + value);
}
beforeValue(false);
out.append(string);
return this;
}
@Override /**
public JsonWriterHandle value(final boolean value) throws IOException { * Encodes {@code value}.
getHandle().value(value); *
return this; * @param value
} * the literal string value, or null to encode a null literal.
* @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle value(final String value) throws IOException {
if (value == null) {
return nullValue();
}
writeDeferredName();
beforeValue(false);
stringExtend(value);
return this;
}
@Override /**
public JsonWriterHandle value(final double value) throws IOException { * Inserts any necessary separators and whitespace before a name. Also
getHandle().value(value); * adjusts the stack to expect the name's value.
return this; */
} private void beforeName() throws IOException {
final int context = peek();
if (context == NONEMPTY_OBJECT) { // first in object
out.write(',');
} else if (context != EMPTY_OBJECT) { // not in an object!
throw new IllegalStateException("Nesting problem.");
}
newline();
replaceTop(DANGLING_NAME);
}
@Override /**
public JsonWriterHandle value(final long value) throws IOException { * Inserts any necessary separators and whitespace before a literal value,
getHandle().value(value); * inline array, or inline object. Also adjusts the stack to expect either a
return this; * closing bracket or another element.
} *
* @param root
* true if the value is a new array or object, the two values
* permitted as top-level elements.
*/
@SuppressWarnings("fallthrough")
private void beforeValue(final boolean root) throws IOException {
switch (peek()) {
case NONEMPTY_DOCUMENT:
if (!this.isLenient()) {
throw new IllegalStateException("JSON must have only one top-level value.");
}
// fall-through
case EMPTY_DOCUMENT: // first in document
if (!this.isLenient() && !root) {
throw new IllegalStateException("JSON must start with an array or an object.");
}
replaceTop(NONEMPTY_DOCUMENT);
break;
case EMPTY_ARRAY: // first in array
replaceTop(NONEMPTY_ARRAY);
newline();
break;
case NONEMPTY_ARRAY: // another in array
out.append(',');
newline();
break;
case DANGLING_NAME: // value for name
out.append(this.getSeparator());
replaceTop(NONEMPTY_OBJECT);
break;
default:
throw new IllegalStateException("Nesting problem.");
}
}
@Override /**
public JsonWriterHandle value(final Number value) throws IOException { * Closes the current scope by appending any necessary whitespace and the
getHandle().value(value); * given bracket.
return this; */
} private JsonWriterHandle close(final int empty, final int nonempty, final String closeBracket) throws IOException {
final int context = peek();
if (context != nonempty && context != empty) {
throw new IllegalStateException("Nesting problem.");
}
if (deferredName != null) {
throw new IllegalStateException("Dangling name: " + deferredName);
}
this.setStackSize(this.getStackSize() - 1);
if (context == nonempty) {
newline();
}
out.write(closeBracket);
return this;
}
@Override private void newline() throws IOException {
public JsonWriterHandle value(final String value) throws IOException { if (this.getIndent() == null) {
getHandle().value(value); return;
return this; }
} out.write("\n");
for (int i = 1, size = this.getStackSize(); i < size; i++) {
out.write(this.getIndent());
}
}
/**
* Enters a new scope by appending any necessary whitespace and the given
* bracket.
*/
private JsonWriterHandle open(final int empty, final String openBracket) throws IOException {
beforeValue(true);
push(empty);
out.write(openBracket);
return this;
}
/**
* Returns the value on the top of the stack.
*/
private int peek() {
if (this.getStackSize() == 0) {
throw new IllegalStateException("JsonWriterHandle is closed.");
}
return this.getStack()[this.getStackSize() - 1];
}
private void push(final int newTop) {
int[] stacks = this.getStack();
if (this.getStackSize() == stacks.length) {
final int[] newStack = new int[stacks.length * 2];
System.arraycopy(stacks, 0, newStack, 0, stacks.length);
stacks = newStack;
this.setStack(stacks);
}
this.setStack(this.getStackSize(), newTop);
this.setStackSize(this.getStackSize() + 1);
}
/**
* Replace the value on the top of the stack with the given value.
*/
private void replaceTop(final int topOfStack) {
this.getStack()[this.getStackSize() - 1] = topOfStack;
}
private void writeDeferredName() throws IOException {
if (deferredName != null) {
beforeName();
stringExtend(deferredName);
deferredName = null;
}
}
} }

View File

@ -20,109 +20,109 @@ import cn.citycraft.GsonAgent.normal.stream.JsonWriterHandle;
*/ */
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })
public class GsonHandle implements Gson { public class GsonHandle implements Gson {
final com.google.gson.Gson handle = new com.google.gson.Gson(); final com.google.gson.Gson handle = new com.google.gson.Gson();
@Override @Override
public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Class<T> classOfT) throws JsonSyntaxException { public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Class<T> classOfT) throws JsonSyntaxException {
return getHandle().fromJson(((JsonElementHandle) json).getHandle(), classOfT); return getHandle().fromJson(((JsonElementHandle) json).getHandle(), classOfT);
} }
@Override @Override
public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Type typeOfT) throws JsonSyntaxException { public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Type typeOfT) throws JsonSyntaxException {
return getHandle().fromJson(((JsonElementHandle) json).getHandle(), typeOfT); return getHandle().fromJson(((JsonElementHandle) json).getHandle(), typeOfT);
} }
@Override @Override
public <T> T fromJson(final cn.citycraft.GsonAgent.api.stream.JsonReader reader, final Type typeOfT) throws JsonIOException, JsonSyntaxException { 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); return getHandle().fromJson(((JsonReaderHandle) reader).getHandle(), typeOfT);
} }
@Override @Override
public <T> T fromJson(final Reader json, final Class<T> classOfT) throws JsonSyntaxException, JsonIOException { public <T> T fromJson(final Reader json, final Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
return getHandle().fromJson(json, classOfT); return getHandle().fromJson(json, classOfT);
} }
@Override @Override
public <T> T fromJson(final Reader json, final Type typeOfT) throws JsonIOException, JsonSyntaxException { public <T> T fromJson(final Reader json, final Type typeOfT) throws JsonIOException, JsonSyntaxException {
return getHandle().fromJson(json, typeOfT); return getHandle().fromJson(json, typeOfT);
} }
@Override @Override
public <T> T fromJson(final String json, final Class<T> classOfT) throws JsonSyntaxException { public <T> T fromJson(final String json, final Class<T> classOfT) throws JsonSyntaxException {
return getHandle().fromJson(json, classOfT); return getHandle().fromJson(json, classOfT);
} }
@Override @Override
public <T> T fromJson(final String json, final Type typeOfT) throws JsonSyntaxException { public <T> T fromJson(final String json, final Type typeOfT) throws JsonSyntaxException {
return getHandle().fromJson(json, typeOfT); return getHandle().fromJson(json, typeOfT);
} }
@Override @Override
public <T> TypeAdapterHandle<T> getAdapter(final Class<T> type) { public <T> TypeAdapterHandle<T> getAdapter(final Class<T> type) {
return new TypeAdapterHandle(getHandle().getAdapter(type)); return new TypeAdapterHandle(getHandle().getAdapter(type));
} }
@Override @Override
public <T> TypeAdapterHandle<T> getAdapter(final cn.citycraft.GsonAgent.api.reflect.TypeToken<T> type) { 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())); return new TypeAdapterHandle(getHandle().getAdapter((com.google.gson.reflect.TypeToken<T>) type.getHandle()));
} }
@Override @Override
public <T> TypeAdapterHandle<T> getDelegateAdapter(final cn.citycraft.GsonAgent.api.TypeAdapterFactory skipPast, final cn.citycraft.GsonAgent.api.reflect.TypeToken<T> type) { 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())); return new TypeAdapterHandle(getHandle().getDelegateAdapter(((TypeAdapterFactoryHandle) skipPast).getHandle(), (com.google.gson.reflect.TypeToken<T>) type.getHandle()));
} }
public com.google.gson.Gson getHandle() { public com.google.gson.Gson getHandle() {
return handle; return handle;
} }
@Override @Override
public String toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement) { public String toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement) {
return getHandle().toJson(((JsonElementHandle) jsonElement).getHandle()); return getHandle().toJson(((JsonElementHandle) jsonElement).getHandle());
} }
@Override @Override
public void toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement, final Appendable writer) throws JsonIOException { public void toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement, final Appendable writer) throws JsonIOException {
getHandle().toJson(((JsonElementHandle) jsonElement).getHandle(), writer); getHandle().toJson(((JsonElementHandle) jsonElement).getHandle(), writer);
} }
@Override @Override
public void toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement, final cn.citycraft.GsonAgent.api.stream.JsonWriter writer) throws JsonIOException { 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()); getHandle().toJson(((JsonElementHandle) jsonElement).getHandle(), ((JsonWriterHandle) writer).getHandle());
} }
@Override @Override
public String toJson(final Object src) { public String toJson(final Object src) {
return getHandle().toJson(src); return getHandle().toJson(src);
} }
@Override @Override
public void toJson(final Object src, final Appendable writer) throws JsonIOException { public void toJson(final Object src, final Appendable writer) throws JsonIOException {
getHandle().toJson(src, writer); getHandle().toJson(src, writer);
} }
@Override @Override
public String toJson(final Object src, final Type typeOfSrc) { public String toJson(final Object src, final Type typeOfSrc) {
return getHandle().toJson(src, typeOfSrc); return getHandle().toJson(src, typeOfSrc);
} }
@Override @Override
public void toJson(final Object src, final Type typeOfSrc, final Appendable writer) throws JsonIOException { public void toJson(final Object src, final Type typeOfSrc, final Appendable writer) throws JsonIOException {
getHandle().toJson(src, typeOfSrc, writer); getHandle().toJson(src, typeOfSrc, writer);
} }
@Override @Override
public void toJson(final Object src, final Type typeOfSrc, final cn.citycraft.GsonAgent.api.stream.JsonWriter writer) throws JsonIOException { 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()); getHandle().toJson(src, typeOfSrc, ((JsonWriterHandle) writer).getHandle());
} }
@Override @Override
public JsonElementHandle toJsonTree(final Object src) { public JsonElementHandle toJsonTree(final Object src) {
return new JsonElementHandle(getHandle().toJsonTree(src)); return new JsonElementHandle(getHandle().toJsonTree(src));
} }
@Override @Override
public JsonElementHandle toJsonTree(final Object src, final Type typeOfSrc) { public JsonElementHandle toJsonTree(final Object src, final Type typeOfSrc) {
return new JsonElementHandle(getHandle().toJsonTree(src, typeOfSrc)); return new JsonElementHandle(getHandle().toJsonTree(src, typeOfSrc));
} }
} }

View File

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

View File

@ -17,127 +17,127 @@ import cn.citycraft.GsonAgent.api.utils.Utils;
*/ */
public class JsonElementHandle implements JsonElement { public class JsonElementHandle implements JsonElement {
private com.google.gson.JsonElement handle; private com.google.gson.JsonElement handle;
public JsonElementHandle() { public JsonElementHandle() {
} }
public JsonElementHandle(final com.google.gson.JsonElement handle) { public JsonElementHandle(final com.google.gson.JsonElement handle) {
this.handle = handle; this.handle = handle;
} }
@Override @Override
public cn.citycraft.GsonAgent.api.JsonElement deepCopy() { public cn.citycraft.GsonAgent.api.JsonElement deepCopy() {
return new JsonElementHandle(Utils.deepCopyObject(this.getHandle())); return new JsonElementHandle(Utils.deepCopyObject(this.getHandle()));
} }
@Override @Override
public BigDecimal getAsBigDecimal() { public BigDecimal getAsBigDecimal() {
return getHandle().getAsBigDecimal(); return getHandle().getAsBigDecimal();
} }
@Override @Override
public BigInteger getAsBigInteger() { public BigInteger getAsBigInteger() {
return getHandle().getAsBigInteger(); return getHandle().getAsBigInteger();
} }
@Override @Override
public boolean getAsBoolean() { public boolean getAsBoolean() {
return getHandle().getAsBoolean(); return getHandle().getAsBoolean();
} }
@Override @Override
public byte getAsByte() { public byte getAsByte() {
return getHandle().getAsByte(); return getHandle().getAsByte();
} }
@Override @Override
public char getAsCharacter() { public char getAsCharacter() {
return getHandle().getAsCharacter(); return getHandle().getAsCharacter();
} }
@Override @Override
public double getAsDouble() { public double getAsDouble() {
return getHandle().getAsDouble(); return getHandle().getAsDouble();
} }
@Override @Override
public float getAsFloat() { public float getAsFloat() {
return getHandle().getAsFloat(); return getHandle().getAsFloat();
} }
@Override @Override
public int getAsInt() { public int getAsInt() {
return getHandle().getAsInt(); return getHandle().getAsInt();
} }
@Override @Override
public JsonArrayHandle getAsJsonArray() { public JsonArrayHandle getAsJsonArray() {
return new JsonArrayHandle(getHandle().getAsJsonArray()); return new JsonArrayHandle(getHandle().getAsJsonArray());
} }
@Override @Override
public JsonNullHandle getAsJsonNull() { public JsonNullHandle getAsJsonNull() {
return new JsonNullHandle(getHandle().getAsJsonNull()); return new JsonNullHandle(getHandle().getAsJsonNull());
} }
@Override @Override
public JsonObjectHandle getAsJsonObject() { public JsonObjectHandle getAsJsonObject() {
return new JsonObjectHandle(getHandle().getAsJsonObject()); return new JsonObjectHandle(getHandle().getAsJsonObject());
} }
@Override @Override
public JsonPrimitiveHandle getAsJsonPrimitive() { public JsonPrimitiveHandle getAsJsonPrimitive() {
return new JsonPrimitiveHandle(getHandle().getAsJsonPrimitive()); return new JsonPrimitiveHandle(getHandle().getAsJsonPrimitive());
} }
@Override @Override
public long getAsLong() { public long getAsLong() {
return getHandle().getAsLong(); return getHandle().getAsLong();
} }
@Override @Override
public Number getAsNumber() { public Number getAsNumber() {
return getHandle().getAsNumber(); return getHandle().getAsNumber();
} }
@Override @Override
public short getAsShort() { public short getAsShort() {
return getHandle().getAsShort(); return getHandle().getAsShort();
} }
@Override @Override
public String getAsString() { public String getAsString() {
return getHandle().getAsString(); return getHandle().getAsString();
} }
public com.google.gson.JsonElement getHandle() { public com.google.gson.JsonElement getHandle() {
return handle; return handle;
} }
@Override @Override
public boolean isJsonArray() { public boolean isJsonArray() {
return getHandle().isJsonArray(); return getHandle().isJsonArray();
} }
@Override @Override
public boolean isJsonNull() { public boolean isJsonNull() {
return getHandle().isJsonNull(); return getHandle().isJsonNull();
} }
@Override @Override
public boolean isJsonObject() { public boolean isJsonObject() {
return getHandle().isJsonObject(); return getHandle().isJsonObject();
} }
@Override @Override
public boolean isJsonPrimitive() { public boolean isJsonPrimitive() {
return getHandle().isJsonPrimitive(); return getHandle().isJsonPrimitive();
} }
@Override @Override
public String toString() { public String toString() {
return getHandle().toString(); return getHandle().toString();
} }
} }

View File

@ -9,28 +9,28 @@ import cn.citycraft.GsonAgent.api.JsonNull;
*/ */
public class JsonNullHandle extends JsonElementHandle implements JsonNull { public class JsonNullHandle extends JsonElementHandle implements JsonNull {
private final com.google.gson.JsonNull handle; private final com.google.gson.JsonNull handle;
public JsonNullHandle() { public JsonNullHandle() {
this(com.google.gson.JsonNull.INSTANCE); this(com.google.gson.JsonNull.INSTANCE);
} }
protected JsonNullHandle(final com.google.gson.JsonNull handle) { protected JsonNullHandle(final com.google.gson.JsonNull handle) {
this.handle = handle; this.handle = handle;
} }
@Override @Override
public cn.citycraft.GsonAgent.api.JsonNull deepCopy() { public cn.citycraft.GsonAgent.api.JsonNull deepCopy() {
return new JsonNullHandle(); return new JsonNullHandle();
} }
@Override @Override
public boolean equals(final Object other) { public boolean equals(final Object other) {
return handle.equals(other); return handle.equals(other);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return handle.hashCode(); return handle.hashCode();
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,203 +6,481 @@ import java.lang.reflect.Field;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
public class JsonWriterHandle implements cn.citycraft.GsonAgent.api.stream.JsonWriter { import cn.citycraft.GsonAgent.api.utils.Utils;
private static String[] REPLACEMENT_CHARS; public class JsonWriterHandle extends JsonWriter implements cn.citycraft.GsonAgent.api.stream.JsonWriter {
/*
* From RFC 4627, "All Unicode characters may be placed within the
* quotation marks except for the characters that must be escaped:
* quotation mark, reverse solidus, and the control characters
* (U+0000 through U+001F)."
*
* We also escape '\u2028' and '\u2029', which JavaScript interprets as
* newline characters. This prevents eval() from failing with a syntax
* error. http://code.google.com/p/google-gson/issues/detail?id=341
*/
private static Field stackField, stackSizeField, indentField, separatorField;
private static String[] HTML_SAFE_REPLACEMENT_CHARS; static {
final Class<JsonWriter> clzz = JsonWriter.class;
try {
stackField = Utils.getDeclareField(clzz, "stack");
stackSizeField = Utils.getDeclareField(clzz, "stackSize");
indentField = Utils.getDeclareField(clzz, "indent");
separatorField = Utils.getDeclareField(clzz, "separator");
} catch (final Exception e) {
e.printStackTrace();
}
}
static { /**
try { * The output data, containing at most one top-level array or object.
Field field = JsonWriter.class.getDeclaredField("REPLACEMENT_CHARS"); */
field.setAccessible(true); private final Writer out;
REPLACEMENT_CHARS = (String[]) field.get(null); private String deferredName;
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 boolean withoutQuotes = false;
private Writer out; /**
* Creates a new instance that writes a JSON-encoded stream to {@code out}.
* For best performance, ensure {@link Writer} is buffered; wrapping in
* {@link java.io.BufferedWriter BufferedWriter} if necessary.
*
* @param out
*/
public JsonWriterHandle(final Writer out) {
super(out);
this.out = out;
}
private boolean withoutQuotes = false; /**
* Begins encoding a new array. Each call to this method must be paired with
* a call to {@link #endArray}.
*
* @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle beginArray() throws IOException {
writeDeferredName();
return open(EMPTY_ARRAY, "[");
}
public JsonWriterHandle(final Writer out) { /**
this(new JsonWriter(out)); * Begins encoding a new object. Each call to this method must be paired
this.out = out; * with a call to {@link #endObject}.
} *
* @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle beginObject() throws IOException {
writeDeferredName();
return open(EMPTY_OBJECT, "{");
}
protected JsonWriterHandle(final JsonWriter handle) { /**
this.handle = handle; * Flushes and closes this writer and the underlying {@link Writer}.
} *
* @throws IOException
* if the JSON document is incomplete.
*/
@Override
public void close() throws IOException {
out.close();
final int size = this.getStackSize();
if (size > 1 || size == 1 && this.getStack()[size - 1] != NONEMPTY_DOCUMENT) {
throw new IOException("Incomplete document");
}
this.setStackSize(0);
}
@Override /**
public JsonWriterHandle beginArray() throws IOException { * Ends encoding the current array.
getHandle().beginArray(); *
return this; * @return this writer.
} * @throws java.io.IOException
*/
@Override
public JsonWriterHandle endArray() throws IOException {
return close(EMPTY_ARRAY, NONEMPTY_ARRAY, "]");
}
@Override /**
public JsonWriterHandle beginObject() throws IOException { * Ends encoding the current object.
getHandle().beginObject(); *
return this; * @return this writer.
} * @throws java.io.IOException
*/
@Override
public JsonWriterHandle endObject() throws IOException {
return close(EMPTY_OBJECT, NONEMPTY_OBJECT, "}");
}
@Override /**
public void close() throws IOException { * Ensures all buffered data is written to the underlying {@link Writer} and
getHandle().close(); * flushes that writer.
} *
* @throws java.io.IOException
*/
@Override
public void flush() throws IOException {
if (this.getStackSize() == 0) {
throw new IllegalStateException("JsonWriterHandle is closed.");
}
out.flush();
}
@Override public JsonWriterHandle getHandle() {
public JsonWriterHandle endArray() throws IOException { return this;
getHandle().endArray(); }
return this;
}
@Override public String getIndent() {
public JsonWriterHandle endObject() throws IOException { return Utils.invokeStringField(this, indentField);
getHandle().endObject(); }
return this;
}
@Override public String getSeparator() {
public void flush() throws IOException { return Utils.invokeStringField(this, separatorField);
getHandle().flush(); }
}
public JsonWriter getHandle() { public int[] getStack() {
return handle; return Utils.invokeIntArrayField(this, stackField);
} }
@Override public int getStackSize() {
public boolean getSerializeNulls() { return Utils.invokeIntField(this, stackSizeField);
return getHandle().getSerializeNulls(); }
}
@Override /**
public boolean isHtmlSafe() { * Encodes the property name.
return getHandle().isHtmlSafe(); *
} * @param name
* the name of the forthcoming value. May not be null.
* @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle name(final String name) throws IOException {
if (name == null) {
throw new NullPointerException("name == null");
}
if (deferredName != null) {
throw new IllegalStateException();
}
if (this.getStackSize() == 0) {
throw new IllegalStateException("JsonWriterHandle is closed.");
}
deferredName = name;
withoutQuotes = false;
return this;
}
@Override @Override
public boolean isLenient() { public JsonWriterHandle nameWithoutQuotes(final String name) throws IOException {
return getHandle().isLenient(); this.name(name);
} withoutQuotes = true;
return this;
}
@Override /**
public JsonWriterHandle name(final String name) throws IOException { * Encodes {@code null}.
getHandle().name(name); *
withoutQuotes = false; * @return this writer.
return this; * @throws java.io.IOException
} */
@Override
public JsonWriterHandle nullValue() throws IOException {
if (deferredName != null) {
if (this.getSerializeNulls()) {
writeDeferredName();
} else {
deferredName = null;
return this; // skip the name and the value
}
}
beforeValue(false);
out.write("null");
return this;
}
@Override public void setSeparator(final String separator) {
public JsonWriterHandle nameWithoutQuotes(final String name) throws IOException { Utils.modifyField(this, separatorField, separator);
getHandle().name(name); }
withoutQuotes = true;
return this;
}
@Override public void setStack(final int index, final int value) {
public JsonWriterHandle nullValue() throws IOException { final int[] newStack = this.getStack();
getHandle().nullValue(); newStack[index] = value;
return this; this.setStack(newStack);
} }
@Override public void setStack(final int[] stack) {
public void setHtmlSafe(final boolean htmlSafe) { Utils.modifyField(this, stackField, stack);
getHandle().setHtmlSafe(htmlSafe); }
}
@Override public void setStackSize(final int stackSize) {
public void setIndent(final String indent) { Utils.modifyField(this, stackSizeField, stackSize);
getHandle().setIndent(indent); }
}
@Override public void stringExtend(final String value) throws IOException {
public void setLenient(final boolean lenient) { final String[] replacements = this.isHtmlSafe() ? Utils.HTML_SAFE_REPLACEMENT_CHARS : Utils.REPLACEMENT_CHARS;
getHandle().setLenient(lenient); 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 < 128) {
replacement = replacements[c];
if (replacement == null) {
continue;
}
} else if (c == '\u2028') {
replacement = "\\u2028";
} else if (c == '\u2029') {
replacement = "\\u2029";
} else {
continue;
}
if (last < i) {
out.write(value, last, i - last);
}
out.write(replacement);
last = i + 1;
}
if (last < length) {
out.write(value, last, length - last);
}
if (!this.withoutQuotes) {
this.out.write("\"");
}
withoutQuotes = false;
}
@Override /**
public void setSerializeNulls(final boolean serializeNulls) { * Encodes {@code value}.
getHandle().setSerializeNulls(serializeNulls); *
} * @param value
* @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle value(final boolean value) throws IOException {
writeDeferredName();
beforeValue(false);
out.write(value ? "true" : "false");
return this;
}
@Override /**
public void string(final String value) throws IOException { * Encodes {@code value}.
final String[] replacements = getHandle().isHtmlSafe() ? HTML_SAFE_REPLACEMENT_CHARS : REPLACEMENT_CHARS; *
if (!this.withoutQuotes) { * @param value
this.out.write("\""); * a finite value. May not be {@link Double#isNaN() NaNs} or
} * {@link Double#isInfinite() infinities}.
int last = 0; * @return this writer.
final int length = value.length(); * @throws java.io.IOException
for (int i = 0; i < length; i++) { */
final char c = value.charAt(i); @Override
String replacement; public JsonWriterHandle value(final double value) throws IOException {
if (c < '€') { if (Double.isNaN(value) || Double.isInfinite(value)) {
replacement = replacements[c]; throw new IllegalArgumentException("Numeric values must be finite, but was " + value);
if (replacement == null) { }
continue; writeDeferredName();
} beforeValue(false);
} else { out.append(Double.toString(value));
if (c == '') { return this;
replacement = "\\u2028"; }
} else {
if (c != '') {
continue;
}
replacement = "\\u2029";
}
}
if (last < i) { /**
this.out.write(value, last, i - last); * Encodes {@code value}.
} *
this.out.write(replacement); * @param value
last = i + 1; * @return this writer.
} * @throws java.io.IOException
if (last < length) { */
this.out.write(value, last, length - last); @Override
} public JsonWriterHandle value(final long value) throws IOException {
if (!this.withoutQuotes) { writeDeferredName();
this.out.write("\""); beforeValue(false);
} out.write(Long.toString(value));
withoutQuotes = false; return this;
} }
@Override /**
public JsonWriterHandle value(final boolean value) throws IOException { * Encodes {@code value}.
getHandle().value(value); *
return this; * @param value
} * a finite value. May not be {@link Double#isNaN() NaNs} or
* {@link Double#isInfinite() infinities}.
* @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle value(final Number value) throws IOException {
if (value == null) {
return nullValue();
}
writeDeferredName();
final String string = value.toString();
if (!this.isLenient() && (string.equals("-Infinity") || string.equals("Infinity") || string.equals("NaN"))) {
throw new IllegalArgumentException("Numeric values must be finite, but was " + value);
}
beforeValue(false);
out.append(string);
return this;
}
@Override /**
public JsonWriterHandle value(final double value) throws IOException { * Encodes {@code value}.
getHandle().value(value); *
return this; * @param value
} * the literal string value, or null to encode a null literal.
* @return this writer.
* @throws java.io.IOException
*/
@Override
public JsonWriterHandle value(final String value) throws IOException {
if (value == null) {
return nullValue();
}
writeDeferredName();
beforeValue(false);
stringExtend(value);
return this;
}
@Override /**
public JsonWriterHandle value(final long value) throws IOException { * Inserts any necessary separators and whitespace before a name. Also
getHandle().value(value); * adjusts the stack to expect the name's value.
return this; */
} private void beforeName() throws IOException {
final int context = peek();
if (context == NONEMPTY_OBJECT) { // first in object
out.write(',');
} else if (context != EMPTY_OBJECT) { // not in an object!
throw new IllegalStateException("Nesting problem.");
}
newline();
replaceTop(DANGLING_NAME);
}
@Override /**
public JsonWriterHandle value(final Number value) throws IOException { * Inserts any necessary separators and whitespace before a literal value,
getHandle().value(value); * inline array, or inline object. Also adjusts the stack to expect either a
return this; * closing bracket or another element.
} *
* @param root
* true if the value is a new array or object, the two values
* permitted as top-level elements.
*/
@SuppressWarnings("fallthrough")
private void beforeValue(final boolean root) throws IOException {
switch (peek()) {
case NONEMPTY_DOCUMENT:
if (!this.isLenient()) {
throw new IllegalStateException("JSON must have only one top-level value.");
}
// fall-through
case EMPTY_DOCUMENT: // first in document
if (!this.isLenient() && !root) {
throw new IllegalStateException("JSON must start with an array or an object.");
}
replaceTop(NONEMPTY_DOCUMENT);
break;
case EMPTY_ARRAY: // first in array
replaceTop(NONEMPTY_ARRAY);
newline();
break;
case NONEMPTY_ARRAY: // another in array
out.append(',');
newline();
break;
case DANGLING_NAME: // value for name
out.append(this.getSeparator());
replaceTop(NONEMPTY_OBJECT);
break;
default:
throw new IllegalStateException("Nesting problem.");
}
}
@Override /**
public JsonWriterHandle value(final String value) throws IOException { * Closes the current scope by appending any necessary whitespace and the
getHandle().value(value); * given bracket.
return this; */
} private JsonWriterHandle close(final int empty, final int nonempty, final String closeBracket) throws IOException {
final int context = peek();
if (context != nonempty && context != empty) {
throw new IllegalStateException("Nesting problem.");
}
if (deferredName != null) {
throw new IllegalStateException("Dangling name: " + deferredName);
}
this.setStackSize(this.getStackSize() - 1);
if (context == nonempty) {
newline();
}
out.write(closeBracket);
return this;
}
private void newline() throws IOException {
if (this.getIndent() == null) {
return;
}
out.write("\n");
for (int i = 1, size = this.getStackSize(); i < size; i++) {
out.write(this.getIndent());
}
}
/**
* Enters a new scope by appending any necessary whitespace and the given
* bracket.
*/
private JsonWriterHandle open(final int empty, final String openBracket) throws IOException {
beforeValue(true);
push(empty);
out.write(openBracket);
return this;
}
/**
* Returns the value on the top of the stack.
*/
private int peek() {
if (this.getStackSize() == 0) {
throw new IllegalStateException("JsonWriterHandle is closed.");
}
return this.getStack()[this.getStackSize() - 1];
}
private void push(final int newTop) {
int[] stacks = this.getStack();
if (this.getStackSize() == stacks.length) {
final int[] newStack = new int[stacks.length * 2];
System.arraycopy(stacks, 0, newStack, 0, stacks.length);
stacks = newStack;
this.setStack(stacks);
}
this.setStack(this.getStackSize(), newTop);
this.setStackSize(this.getStackSize() + 1);
}
/**
* Replace the value on the top of the stack with the given value.
*/
private void replaceTop(final int topOfStack) {
this.getStack()[this.getStackSize() - 1] = topOfStack;
}
private void writeDeferredName() throws IOException {
if (deferredName != null) {
beforeName();
stringExtend(deferredName);
deferredName = null;
}
}
} }