From 986461a0982caa2acaadc30fa11bf57c536313c7 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Fri, 4 Mar 2011 10:46:41 -0600 Subject: [PATCH] streamline rebased Gson into source; rebase was done to prevent conflict with other plugins (like upcoming WorldGuard release) which also use Gson; Bukkit doesn't yet handle such conflicts properly --- MANIFEST.MF | 2 - .../bukkit/mcteam/factions/entities/EM.java | 5 +- ...onymousAndLocalClassExclusionStrategy.java | 39 + src/com/bukkit/mcteam/gson/Cache.java | 61 + .../gson/CamelCaseSeparatorNamingPolicy.java | 73 ++ .../gson/CircularReferenceException.java | 45 + .../gson/CompositionFieldNamingPolicy.java | 46 + .../mcteam/gson/DefaultTypeAdapters.java | 893 +++++++++++++ .../gson/DelegatingJsonElementVisitor.java | 117 ++ .../gson/DisjunctionExclusionStrategy.java | 52 + src/com/bukkit/mcteam/gson/Escaper.java | 160 +++ .../bukkit/mcteam/gson/ExclusionStrategy.java | 95 ++ ...ationDeserializationExclusionStrategy.java | 39 + ...otationSerializationExclusionStrategy.java | 40 + .../bukkit/mcteam/gson/FieldAttributes.java | 229 ++++ .../bukkit/mcteam/gson/FieldNamingPolicy.java | 99 ++ .../mcteam/gson/FieldNamingStrategy.java | 40 + .../mcteam/gson/FieldNamingStrategy2.java | 38 + .../gson/FieldNamingStrategy2Adapter.java | 38 + .../mcteam/gson/GenericArrayTypeImpl.java | 70 + src/com/bukkit/mcteam/gson/Gson.java | 595 +++++++++ src/com/bukkit/mcteam/gson/GsonBuilder.java | 570 +++++++++ .../gson/InnerClassExclusionStrategy.java | 43 + .../bukkit/mcteam/gson/InstanceCreator.java | 92 ++ .../mcteam/gson/JavaFieldNamingPolicy.java | 51 + src/com/bukkit/mcteam/gson/JsonArray.java | 312 +++++ .../gson/JsonArrayDeserializationVisitor.java | 112 ++ .../gson/JsonDeserializationContext.java | 44 + .../JsonDeserializationContextDefault.java | 88 ++ .../gson/JsonDeserializationVisitor.java | 112 ++ .../bukkit/mcteam/gson/JsonDeserializer.java | 88 ++ .../JsonDeserializerExceptionWrapper.java | 71 ++ src/com/bukkit/mcteam/gson/JsonElement.java | 338 +++++ .../mcteam/gson/JsonElementVisitor.java | 47 + .../mcteam/gson/JsonFieldNameValidator.java | 56 + .../bukkit/mcteam/gson/JsonIOException.java | 46 + src/com/bukkit/mcteam/gson/JsonNull.java | 72 ++ src/com/bukkit/mcteam/gson/JsonObject.java | 208 +++ .../JsonObjectDeserializationVisitor.java | 135 ++ .../mcteam/gson/JsonParseException.java | 64 + src/com/bukkit/mcteam/gson/JsonParser.java | 97 ++ src/com/bukkit/mcteam/gson/JsonPrimitive.java | 387 ++++++ .../mcteam/gson/JsonSerializationContext.java | 49 + .../gson/JsonSerializationContextDefault.java | 59 + .../mcteam/gson/JsonSerializationVisitor.java | 236 ++++ .../bukkit/mcteam/gson/JsonSerializer.java | 86 ++ .../bukkit/mcteam/gson/JsonStreamParser.java | 122 ++ .../mcteam/gson/JsonSyntaxException.java | 47 + .../bukkit/mcteam/gson/JsonTreeNavigator.java | 111 ++ .../mcteam/gson/LongSerializationPolicy.java | 74 ++ .../LowerCamelCaseSeparatorNamingPolicy.java | 43 + .../mcteam/gson/LowerCaseNamingPolicy.java | 50 + src/com/bukkit/mcteam/gson/LruCache.java | 66 + .../mcteam}/gson/MapAsArrayTypeAdapter.java | 8 +- .../mcteam/gson/MappedObjectConstructor.java | 112 ++ .../bukkit/mcteam/gson/MemoryRefStack.java | 87 ++ .../gson/ModifierBasedExclusionStrategy.java | 53 + .../gson/ModifyFirstLetterNamingPolicy.java | 107 ++ .../mcteam/gson/NullExclusionStrategy.java | 37 + .../bukkit/mcteam/gson/ObjectConstructor.java | 47 + .../bukkit/mcteam/gson/ObjectNavigator.java | 169 +++ .../mcteam/gson/ObjectNavigatorFactory.java | 61 + .../bukkit/mcteam/gson/ObjectTypePair.java | 136 ++ src/com/bukkit/mcteam/gson/Pair.java | 62 + .../gson/ParameterizedTypeHandlerMap.java | 200 +++ .../mcteam/gson/ParameterizedTypeImpl.java | 91 ++ src/com/bukkit/mcteam/gson/Preconditions.java | 48 + src/com/bukkit/mcteam/gson/Primitives.java | 114 ++ .../gson/RecursiveFieldNamingPolicy.java | 46 + ...ameAnnotationInterceptingNamingPolicy.java | 48 + src/com/bukkit/mcteam/gson/Streams.java | 189 +++ .../gson/SyntheticFieldExclusionStrategy.java | 44 + src/com/bukkit/mcteam/gson/TypeAdapter.java | 35 + src/com/bukkit/mcteam/gson/TypeInfo.java | 76 ++ src/com/bukkit/mcteam/gson/TypeInfoArray.java | 69 + .../mcteam/gson/TypeInfoCollection.java | 46 + .../bukkit/mcteam/gson/TypeInfoFactory.java | 175 +++ src/com/bukkit/mcteam/gson/TypeInfoMap.java | 58 + src/com/bukkit/mcteam/gson/TypeUtils.java | 95 ++ .../UpperCamelCaseSeparatorNamingPolicy.java | 45 + .../mcteam/gson/UpperCaseNamingPolicy.java | 48 + .../bukkit/mcteam/gson/VersionConstants.java | 29 + .../mcteam/gson/VersionExclusionStrategy.java | 67 + .../mcteam/gson/annotations/Expose.java | 79 ++ .../gson/annotations/SerializedName.java | 74 ++ .../bukkit/mcteam/gson/annotations/Since.java | 61 + .../bukkit/mcteam/gson/annotations/Until.java | 66 + .../mcteam/gson/annotations/package-info.java | 6 + src/com/bukkit/mcteam/gson/package-info.java | 11 + .../bukkit/mcteam/gson/reflect/TypeToken.java | 375 ++++++ .../mcteam/gson/reflect/package-info.java | 6 + .../bukkit/mcteam/gson/stream/JsonReader.java | 1121 +++++++++++++++++ .../bukkit/mcteam/gson/stream/JsonScope.java | 71 ++ .../bukkit/mcteam/gson/stream/JsonToken.java | 85 ++ .../bukkit/mcteam/gson/stream/JsonWriter.java | 553 ++++++++ .../gson/stream/MalformedJsonException.java | 44 + 96 files changed, 11699 insertions(+), 7 deletions(-) create mode 100644 src/com/bukkit/mcteam/gson/AnonymousAndLocalClassExclusionStrategy.java create mode 100644 src/com/bukkit/mcteam/gson/Cache.java create mode 100644 src/com/bukkit/mcteam/gson/CamelCaseSeparatorNamingPolicy.java create mode 100644 src/com/bukkit/mcteam/gson/CircularReferenceException.java create mode 100644 src/com/bukkit/mcteam/gson/CompositionFieldNamingPolicy.java create mode 100644 src/com/bukkit/mcteam/gson/DefaultTypeAdapters.java create mode 100644 src/com/bukkit/mcteam/gson/DelegatingJsonElementVisitor.java create mode 100644 src/com/bukkit/mcteam/gson/DisjunctionExclusionStrategy.java create mode 100644 src/com/bukkit/mcteam/gson/Escaper.java create mode 100644 src/com/bukkit/mcteam/gson/ExclusionStrategy.java create mode 100644 src/com/bukkit/mcteam/gson/ExposeAnnotationDeserializationExclusionStrategy.java create mode 100644 src/com/bukkit/mcteam/gson/ExposeAnnotationSerializationExclusionStrategy.java create mode 100644 src/com/bukkit/mcteam/gson/FieldAttributes.java create mode 100644 src/com/bukkit/mcteam/gson/FieldNamingPolicy.java create mode 100644 src/com/bukkit/mcteam/gson/FieldNamingStrategy.java create mode 100644 src/com/bukkit/mcteam/gson/FieldNamingStrategy2.java create mode 100644 src/com/bukkit/mcteam/gson/FieldNamingStrategy2Adapter.java create mode 100644 src/com/bukkit/mcteam/gson/GenericArrayTypeImpl.java create mode 100644 src/com/bukkit/mcteam/gson/Gson.java create mode 100644 src/com/bukkit/mcteam/gson/GsonBuilder.java create mode 100644 src/com/bukkit/mcteam/gson/InnerClassExclusionStrategy.java create mode 100644 src/com/bukkit/mcteam/gson/InstanceCreator.java create mode 100644 src/com/bukkit/mcteam/gson/JavaFieldNamingPolicy.java create mode 100644 src/com/bukkit/mcteam/gson/JsonArray.java create mode 100644 src/com/bukkit/mcteam/gson/JsonArrayDeserializationVisitor.java create mode 100644 src/com/bukkit/mcteam/gson/JsonDeserializationContext.java create mode 100644 src/com/bukkit/mcteam/gson/JsonDeserializationContextDefault.java create mode 100644 src/com/bukkit/mcteam/gson/JsonDeserializationVisitor.java create mode 100644 src/com/bukkit/mcteam/gson/JsonDeserializer.java create mode 100644 src/com/bukkit/mcteam/gson/JsonDeserializerExceptionWrapper.java create mode 100644 src/com/bukkit/mcteam/gson/JsonElement.java create mode 100644 src/com/bukkit/mcteam/gson/JsonElementVisitor.java create mode 100644 src/com/bukkit/mcteam/gson/JsonFieldNameValidator.java create mode 100644 src/com/bukkit/mcteam/gson/JsonIOException.java create mode 100644 src/com/bukkit/mcteam/gson/JsonNull.java create mode 100644 src/com/bukkit/mcteam/gson/JsonObject.java create mode 100644 src/com/bukkit/mcteam/gson/JsonObjectDeserializationVisitor.java create mode 100644 src/com/bukkit/mcteam/gson/JsonParseException.java create mode 100644 src/com/bukkit/mcteam/gson/JsonParser.java create mode 100644 src/com/bukkit/mcteam/gson/JsonPrimitive.java create mode 100644 src/com/bukkit/mcteam/gson/JsonSerializationContext.java create mode 100644 src/com/bukkit/mcteam/gson/JsonSerializationContextDefault.java create mode 100644 src/com/bukkit/mcteam/gson/JsonSerializationVisitor.java create mode 100644 src/com/bukkit/mcteam/gson/JsonSerializer.java create mode 100644 src/com/bukkit/mcteam/gson/JsonStreamParser.java create mode 100644 src/com/bukkit/mcteam/gson/JsonSyntaxException.java create mode 100644 src/com/bukkit/mcteam/gson/JsonTreeNavigator.java create mode 100644 src/com/bukkit/mcteam/gson/LongSerializationPolicy.java create mode 100644 src/com/bukkit/mcteam/gson/LowerCamelCaseSeparatorNamingPolicy.java create mode 100644 src/com/bukkit/mcteam/gson/LowerCaseNamingPolicy.java create mode 100644 src/com/bukkit/mcteam/gson/LruCache.java rename src/com/{google => bukkit/mcteam}/gson/MapAsArrayTypeAdapter.java (95%) create mode 100644 src/com/bukkit/mcteam/gson/MappedObjectConstructor.java create mode 100644 src/com/bukkit/mcteam/gson/MemoryRefStack.java create mode 100644 src/com/bukkit/mcteam/gson/ModifierBasedExclusionStrategy.java create mode 100644 src/com/bukkit/mcteam/gson/ModifyFirstLetterNamingPolicy.java create mode 100644 src/com/bukkit/mcteam/gson/NullExclusionStrategy.java create mode 100644 src/com/bukkit/mcteam/gson/ObjectConstructor.java create mode 100644 src/com/bukkit/mcteam/gson/ObjectNavigator.java create mode 100644 src/com/bukkit/mcteam/gson/ObjectNavigatorFactory.java create mode 100644 src/com/bukkit/mcteam/gson/ObjectTypePair.java create mode 100644 src/com/bukkit/mcteam/gson/Pair.java create mode 100644 src/com/bukkit/mcteam/gson/ParameterizedTypeHandlerMap.java create mode 100644 src/com/bukkit/mcteam/gson/ParameterizedTypeImpl.java create mode 100644 src/com/bukkit/mcteam/gson/Preconditions.java create mode 100644 src/com/bukkit/mcteam/gson/Primitives.java create mode 100644 src/com/bukkit/mcteam/gson/RecursiveFieldNamingPolicy.java create mode 100644 src/com/bukkit/mcteam/gson/SerializedNameAnnotationInterceptingNamingPolicy.java create mode 100644 src/com/bukkit/mcteam/gson/Streams.java create mode 100644 src/com/bukkit/mcteam/gson/SyntheticFieldExclusionStrategy.java create mode 100644 src/com/bukkit/mcteam/gson/TypeAdapter.java create mode 100644 src/com/bukkit/mcteam/gson/TypeInfo.java create mode 100644 src/com/bukkit/mcteam/gson/TypeInfoArray.java create mode 100644 src/com/bukkit/mcteam/gson/TypeInfoCollection.java create mode 100644 src/com/bukkit/mcteam/gson/TypeInfoFactory.java create mode 100644 src/com/bukkit/mcteam/gson/TypeInfoMap.java create mode 100644 src/com/bukkit/mcteam/gson/TypeUtils.java create mode 100644 src/com/bukkit/mcteam/gson/UpperCamelCaseSeparatorNamingPolicy.java create mode 100644 src/com/bukkit/mcteam/gson/UpperCaseNamingPolicy.java create mode 100644 src/com/bukkit/mcteam/gson/VersionConstants.java create mode 100644 src/com/bukkit/mcteam/gson/VersionExclusionStrategy.java create mode 100644 src/com/bukkit/mcteam/gson/annotations/Expose.java create mode 100644 src/com/bukkit/mcteam/gson/annotations/SerializedName.java create mode 100644 src/com/bukkit/mcteam/gson/annotations/Since.java create mode 100644 src/com/bukkit/mcteam/gson/annotations/Until.java create mode 100644 src/com/bukkit/mcteam/gson/annotations/package-info.java create mode 100644 src/com/bukkit/mcteam/gson/package-info.java create mode 100644 src/com/bukkit/mcteam/gson/reflect/TypeToken.java create mode 100644 src/com/bukkit/mcteam/gson/reflect/package-info.java create mode 100644 src/com/bukkit/mcteam/gson/stream/JsonReader.java create mode 100644 src/com/bukkit/mcteam/gson/stream/JsonScope.java create mode 100644 src/com/bukkit/mcteam/gson/stream/JsonToken.java create mode 100644 src/com/bukkit/mcteam/gson/stream/JsonWriter.java create mode 100644 src/com/bukkit/mcteam/gson/stream/MalformedJsonException.java diff --git a/MANIFEST.MF b/MANIFEST.MF index 5c137bda..9d885be5 100644 --- a/MANIFEST.MF +++ b/MANIFEST.MF @@ -1,3 +1 @@ Manifest-Version: 1.0 -Class-Path: ../gson.jar - diff --git a/src/com/bukkit/mcteam/factions/entities/EM.java b/src/com/bukkit/mcteam/factions/entities/EM.java index 7caabfb5..dcfd25a1 100644 --- a/src/com/bukkit/mcteam/factions/entities/EM.java +++ b/src/com/bukkit/mcteam/factions/entities/EM.java @@ -10,7 +10,7 @@ import org.bukkit.entity.Player; import com.bukkit.mcteam.factions.Factions; import com.bukkit.mcteam.factions.util.*; import com.bukkit.mcteam.util.DiscUtil; -import com.google.gson.*; +import com.bukkit.mcteam.gson.*; /** * This is a entity manager that persists object to json. @@ -125,6 +125,7 @@ public class EM { folderBoard.mkdirs(); class jsonFileFilter implements FileFilter { + @Override public boolean accept(File file) { return (file.getName().toLowerCase().endsWith(ext) && file.isFile()); } @@ -204,6 +205,7 @@ public class EM { folderFollower.mkdirs(); class jsonFileFilter implements FileFilter { + @Override public boolean accept(File file) { return (file.getName().toLowerCase().endsWith(ext) && file.isFile()); } @@ -292,6 +294,7 @@ public class EM { class jsonFileFilter implements FileFilter { + @Override public boolean accept(File file) { return (file.getName().toLowerCase().endsWith(ext) && file.isFile()); } diff --git a/src/com/bukkit/mcteam/gson/AnonymousAndLocalClassExclusionStrategy.java b/src/com/bukkit/mcteam/gson/AnonymousAndLocalClassExclusionStrategy.java new file mode 100644 index 00000000..c12a45e4 --- /dev/null +++ b/src/com/bukkit/mcteam/gson/AnonymousAndLocalClassExclusionStrategy.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.bukkit.mcteam.gson; + + +/** + * Strategy for excluding anonymous and local classes. + * + * @author Joel Leitch + */ +final class AnonymousAndLocalClassExclusionStrategy implements ExclusionStrategy { + + public boolean shouldSkipField(FieldAttributes f) { + return isAnonymousOrLocal(f.getDeclaredClass()); + } + + public boolean shouldSkipClass(Class clazz) { + return isAnonymousOrLocal(clazz); + } + + private boolean isAnonymousOrLocal(Class clazz) { + return !Enum.class.isAssignableFrom(clazz) + && (clazz.isAnonymousClass() || clazz.isLocalClass()); + } +} diff --git a/src/com/bukkit/mcteam/gson/Cache.java b/src/com/bukkit/mcteam/gson/Cache.java new file mode 100644 index 00000000..02ddd4f6 --- /dev/null +++ b/src/com/bukkit/mcteam/gson/Cache.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.bukkit.mcteam.gson; + +/** + * Defines generic cache interface. + * + * @author Inderjeet Singh + * @author Joel Leitch + */ +interface Cache { + + /** + * Adds the new value object into the cache for the given key. If the key already + * exists, then this method will override the value for the key. + * + * @param key the key identifier for the {@code value} object + * @param value the value object to store in the cache + */ + void addElement(K key, V value); + + /** + * Retrieve the cached value for the given {@code key}. + * + * @param key the key identifying the value + * @return the cached value for the given {@code key} + */ + V getElement(K key); + + /** + * Removes the value from the cache for the given key. + * + * @param key the key identifying the value to remove + * @return the value for the given {@code key} + */ + V removeElement(K key); + + /** + * Removes everything from this cache. + */ + void clear(); + + /** + * @return the number of objects in this cache + */ + int size(); +} diff --git a/src/com/bukkit/mcteam/gson/CamelCaseSeparatorNamingPolicy.java b/src/com/bukkit/mcteam/gson/CamelCaseSeparatorNamingPolicy.java new file mode 100644 index 00000000..21cebbec --- /dev/null +++ b/src/com/bukkit/mcteam/gson/CamelCaseSeparatorNamingPolicy.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.bukkit.mcteam.gson; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.Collection; + +/** + * Converts the field name that uses camel-case define word separation into separate words that + * are separated by the provided {@code separatorString}. + * + *

The following is an example:

+ *
+ * class IntWrapper {
+ *   public int integerField = 0;
+ * }
+ *
+ * CamelCaseSeparatorNamingPolicy policy = new CamelCaseSeparatorNamingPolicy("_");
+ * String translatedFieldName =
+ *     policy.translateName(IntWrapper.class.getField("integerField"));
+ *
+ * assert("integer_Field".equals(translatedFieldName));
+ * 
+ * + * @author Joel Leitch + */ +final class CamelCaseSeparatorNamingPolicy extends RecursiveFieldNamingPolicy { + private final String separatorString; + + /** + * Constructs a new CamelCaseSeparatorNamingPolicy object that will add the + * {@code separatorString} between each of the words separated by camel case. + * + * @param separatorString the string value to place between words + * @throws IllegalArgumentException thrown if the {@code separatorString} parameter + * is null or empty. + */ + public CamelCaseSeparatorNamingPolicy(String separatorString) { + Preconditions.checkNotNull(separatorString); + Preconditions.checkArgument(!"".equals(separatorString)); + this.separatorString = separatorString; + } + + @Override + protected String translateName(String target, Type fieldType, + Collection annnotations) { + StringBuilder translation = new StringBuilder(); + for (int i = 0; i < target.length(); i++) { + char character = target.charAt(i); + if (Character.isUpperCase(character) && translation.length() != 0) { + translation.append(separatorString); + } + translation.append(character); + } + + return translation.toString(); + } +} diff --git a/src/com/bukkit/mcteam/gson/CircularReferenceException.java b/src/com/bukkit/mcteam/gson/CircularReferenceException.java new file mode 100644 index 00000000..a79e2482 --- /dev/null +++ b/src/com/bukkit/mcteam/gson/CircularReferenceException.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.bukkit.mcteam.gson; + +/** + * Exception class to indicate a circular reference error. + * This class is not part of the public API and hence is not public. + * + * @author Inderjeet Singh + * @author Joel Leitch + */ +final class CircularReferenceException extends RuntimeException { + private static final long serialVersionUID = 7444343294106513081L; + private final Object offendingNode; + + CircularReferenceException(Object offendingNode) { + super("circular reference error"); + this.offendingNode = offendingNode; + } + + public IllegalStateException createDetailedException(FieldAttributes offendingField) { + StringBuilder msg = new StringBuilder(getMessage()); + if (offendingField != null) { + msg.append("\n ").append("Offending field: ").append(offendingField.getName() + "\n"); + } + if (offendingNode != null) { + msg.append("\n ").append("Offending object: ").append(offendingNode); + } + return new IllegalStateException(msg.toString(), this); + } +} diff --git a/src/com/bukkit/mcteam/gson/CompositionFieldNamingPolicy.java b/src/com/bukkit/mcteam/gson/CompositionFieldNamingPolicy.java new file mode 100644 index 00000000..0d9d1004 --- /dev/null +++ b/src/com/bukkit/mcteam/gson/CompositionFieldNamingPolicy.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.bukkit.mcteam.gson; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.Collection; + +/** + * Performs numerous field naming translations wrapped up as one object. + * + * @author Joel Leitch + */ +abstract class CompositionFieldNamingPolicy extends RecursiveFieldNamingPolicy { + + private final RecursiveFieldNamingPolicy[] fieldPolicies; + + public CompositionFieldNamingPolicy(RecursiveFieldNamingPolicy... fieldNamingPolicies) { + if (fieldNamingPolicies == null) { + throw new NullPointerException("naming policies can not be null."); + } + this.fieldPolicies = fieldNamingPolicies; + } + + @Override + protected String translateName(String target, Type fieldType, Collection annotations) { + for (RecursiveFieldNamingPolicy policy : fieldPolicies) { + target = policy.translateName(target, fieldType, annotations); + } + return target; + } +} diff --git a/src/com/bukkit/mcteam/gson/DefaultTypeAdapters.java b/src/com/bukkit/mcteam/gson/DefaultTypeAdapters.java new file mode 100644 index 00000000..c2e9255a --- /dev/null +++ b/src/com/bukkit/mcteam/gson/DefaultTypeAdapters.java @@ -0,0 +1,893 @@ +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.bukkit.mcteam.gson; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.sql.Time; +import java.sql.Timestamp; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Collection; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.SortedSet; +import java.util.StringTokenizer; +import java.util.TreeSet; +import java.util.UUID; + +/** + * List of all the default type adapters ({@link JsonSerializer}s, {@link JsonDeserializer}s, + * and {@link InstanceCreator}s. + * + * @author Inderjeet Singh + * @author Joel Leitch + */ +final class DefaultTypeAdapters { + + private static final DefaultDateTypeAdapter DATE_TYPE_ADAPTER = new DefaultDateTypeAdapter(); + private static final DefaultJavaSqlDateTypeAdapter JAVA_SQL_DATE_TYPE_ADAPTER = + new DefaultJavaSqlDateTypeAdapter(); + private static final DefaultTimeTypeAdapter TIME_TYPE_ADAPTER = + new DefaultTimeTypeAdapter(); + private static final DefaultTimestampDeserializer TIMESTAMP_DESERIALIZER = + new DefaultTimestampDeserializer(); + + @SuppressWarnings("rawtypes") + private static final EnumTypeAdapter ENUM_TYPE_ADAPTER = new EnumTypeAdapter(); + private static final UrlTypeAdapter URL_TYPE_ADAPTER = new UrlTypeAdapter(); + private static final UriTypeAdapter URI_TYPE_ADAPTER = new UriTypeAdapter(); + private static final UuidTypeAdapter UUUID_TYPE_ADAPTER = new UuidTypeAdapter(); + private static final LocaleTypeAdapter LOCALE_TYPE_ADAPTER = new LocaleTypeAdapter(); + private static final CollectionTypeAdapter COLLECTION_TYPE_ADAPTER = new CollectionTypeAdapter(); + private static final MapTypeAdapter MAP_TYPE_ADAPTER = new MapTypeAdapter(); + private static final BigDecimalTypeAdapter BIG_DECIMAL_TYPE_ADAPTER = new BigDecimalTypeAdapter(); + private static final BigIntegerTypeAdapter BIG_INTEGER_TYPE_ADAPTER = new BigIntegerTypeAdapter(); + + private static final BooleanTypeAdapter BOOLEAN_TYPE_ADAPTER = new BooleanTypeAdapter(); + private static final ByteTypeAdapter BYTE_TYPE_ADAPTER = new ByteTypeAdapter(); + private static final CharacterTypeAdapter CHARACTER_TYPE_ADAPTER = new CharacterTypeAdapter(); + private static final DoubleDeserializer DOUBLE_TYPE_ADAPTER = new DoubleDeserializer(); + private static final FloatDeserializer FLOAT_TYPE_ADAPTER = new FloatDeserializer(); + private static final IntegerTypeAdapter INTEGER_TYPE_ADAPTER = new IntegerTypeAdapter(); + private static final LongDeserializer LONG_DESERIALIZER = new LongDeserializer(); + private static final NumberTypeAdapter NUMBER_TYPE_ADAPTER = new NumberTypeAdapter(); + private static final ShortTypeAdapter SHORT_TYPE_ADAPTER = new ShortTypeAdapter(); + private static final StringTypeAdapter STRING_TYPE_ADAPTER = new StringTypeAdapter(); + + private static final PropertiesCreator PROPERTIES_CREATOR = new PropertiesCreator(); + private static final TreeSetCreator TREE_SET_CREATOR = new TreeSetCreator(); + private static final HashSetCreator HASH_SET_CREATOR = new HashSetCreator(); + private static final GregorianCalendarTypeAdapter GREGORIAN_CALENDAR_TYPE_ADAPTER = + new GregorianCalendarTypeAdapter(); + + // The constants DEFAULT_SERIALIZERS, DEFAULT_DESERIALIZERS, and DEFAULT_INSTANCE_CREATORS + // must be defined after the constants for the type adapters. Otherwise, the type adapter + // constants will appear as nulls. + private static final ParameterizedTypeHandlerMap> DEFAULT_SERIALIZERS = + createDefaultSerializers(); + private static final ParameterizedTypeHandlerMap> DEFAULT_DESERIALIZERS = + createDefaultDeserializers(); + private static final ParameterizedTypeHandlerMap> DEFAULT_INSTANCE_CREATORS = + createDefaultInstanceCreators(); + + private static ParameterizedTypeHandlerMap> createDefaultSerializers() { + ParameterizedTypeHandlerMap> map = + new ParameterizedTypeHandlerMap>(); + + map.registerForTypeHierarchy(Enum.class, ENUM_TYPE_ADAPTER); + map.register(URL.class, URL_TYPE_ADAPTER); + map.register(URI.class, URI_TYPE_ADAPTER); + map.register(UUID.class, UUUID_TYPE_ADAPTER); + map.register(Locale.class, LOCALE_TYPE_ADAPTER); + map.registerForTypeHierarchy(Collection.class, COLLECTION_TYPE_ADAPTER); + map.registerForTypeHierarchy(Map.class, MAP_TYPE_ADAPTER); + map.register(Date.class, DATE_TYPE_ADAPTER); + map.register(java.sql.Date.class, JAVA_SQL_DATE_TYPE_ADAPTER); + map.register(Timestamp.class, DATE_TYPE_ADAPTER); + map.register(Time.class, TIME_TYPE_ADAPTER); + map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); + map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); + map.register(BigDecimal.class, BIG_DECIMAL_TYPE_ADAPTER); + map.register(BigInteger.class, BIG_INTEGER_TYPE_ADAPTER); + + // Add primitive serializers + map.register(Boolean.class, BOOLEAN_TYPE_ADAPTER); + map.register(boolean.class, BOOLEAN_TYPE_ADAPTER); + map.register(Byte.class, BYTE_TYPE_ADAPTER); + map.register(byte.class, BYTE_TYPE_ADAPTER); + map.register(Character.class, CHARACTER_TYPE_ADAPTER); + map.register(char.class, CHARACTER_TYPE_ADAPTER); + map.register(Integer.class, INTEGER_TYPE_ADAPTER); + map.register(int.class, INTEGER_TYPE_ADAPTER); + map.register(Number.class, NUMBER_TYPE_ADAPTER); + map.register(Short.class, SHORT_TYPE_ADAPTER); + map.register(short.class, SHORT_TYPE_ADAPTER); + map.register(String.class, STRING_TYPE_ADAPTER); + + map.makeUnmodifiable(); + return map; + } + + private static ParameterizedTypeHandlerMap> createDefaultDeserializers() { + ParameterizedTypeHandlerMap> map = + new ParameterizedTypeHandlerMap>(); + map.registerForTypeHierarchy(Enum.class, wrapDeserializer(ENUM_TYPE_ADAPTER)); + map.register(URL.class, wrapDeserializer(URL_TYPE_ADAPTER)); + map.register(URI.class, wrapDeserializer(URI_TYPE_ADAPTER)); + map.register(UUID.class, wrapDeserializer(UUUID_TYPE_ADAPTER)); + map.register(Locale.class, wrapDeserializer(LOCALE_TYPE_ADAPTER)); + map.registerForTypeHierarchy(Collection.class, wrapDeserializer(COLLECTION_TYPE_ADAPTER)); + map.registerForTypeHierarchy(Map.class, wrapDeserializer(MAP_TYPE_ADAPTER)); + map.register(Date.class, wrapDeserializer(DATE_TYPE_ADAPTER)); + map.register(java.sql.Date.class, wrapDeserializer(JAVA_SQL_DATE_TYPE_ADAPTER)); + map.register(Timestamp.class, wrapDeserializer(TIMESTAMP_DESERIALIZER)); + map.register(Time.class, wrapDeserializer(TIME_TYPE_ADAPTER)); + map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); + map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); + map.register(BigDecimal.class, wrapDeserializer(BIG_DECIMAL_TYPE_ADAPTER)); + map.register(BigInteger.class, wrapDeserializer(BIG_INTEGER_TYPE_ADAPTER)); + + // Add primitive deserializers + map.register(Boolean.class, wrapDeserializer(BOOLEAN_TYPE_ADAPTER)); + map.register(boolean.class, wrapDeserializer(BOOLEAN_TYPE_ADAPTER)); + map.register(Byte.class, wrapDeserializer(BYTE_TYPE_ADAPTER)); + map.register(byte.class, wrapDeserializer(BYTE_TYPE_ADAPTER)); + map.register(Character.class, wrapDeserializer(CHARACTER_TYPE_ADAPTER)); + map.register(char.class, wrapDeserializer(CHARACTER_TYPE_ADAPTER)); + map.register(Double.class, wrapDeserializer(DOUBLE_TYPE_ADAPTER)); + map.register(double.class, wrapDeserializer(DOUBLE_TYPE_ADAPTER)); + map.register(Float.class, wrapDeserializer(FLOAT_TYPE_ADAPTER)); + map.register(float.class, wrapDeserializer(FLOAT_TYPE_ADAPTER)); + map.register(Integer.class, wrapDeserializer(INTEGER_TYPE_ADAPTER)); + map.register(int.class, wrapDeserializer(INTEGER_TYPE_ADAPTER)); + map.register(Long.class, wrapDeserializer(LONG_DESERIALIZER)); + map.register(long.class, wrapDeserializer(LONG_DESERIALIZER)); + map.register(Number.class, wrapDeserializer(NUMBER_TYPE_ADAPTER)); + map.register(Short.class, wrapDeserializer(SHORT_TYPE_ADAPTER)); + map.register(short.class, wrapDeserializer(SHORT_TYPE_ADAPTER)); + map.register(String.class, wrapDeserializer(STRING_TYPE_ADAPTER)); + + map.makeUnmodifiable(); + return map; + } + + private static ParameterizedTypeHandlerMap> createDefaultInstanceCreators() { + ParameterizedTypeHandlerMap> map = + new ParameterizedTypeHandlerMap>(); + map.registerForTypeHierarchy(Map.class, MAP_TYPE_ADAPTER); + + // Add Collection type instance creators + map.registerForTypeHierarchy(Collection.class, COLLECTION_TYPE_ADAPTER); + + map.registerForTypeHierarchy(Set.class, HASH_SET_CREATOR); + map.registerForTypeHierarchy(SortedSet.class, TREE_SET_CREATOR); + map.register(Properties.class, PROPERTIES_CREATOR); + map.makeUnmodifiable(); + return map; + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + private static JsonDeserializer wrapDeserializer(JsonDeserializer deserializer) { + return new JsonDeserializerExceptionWrapper(deserializer); + } + + static ParameterizedTypeHandlerMap> getDefaultSerializers() { + return getDefaultSerializers(false, LongSerializationPolicy.DEFAULT); + } + + static ParameterizedTypeHandlerMap> getDefaultSerializers( + boolean serializeSpecialFloatingPointValues, LongSerializationPolicy longSerializationPolicy) { + ParameterizedTypeHandlerMap> serializers = + new ParameterizedTypeHandlerMap>(); + + // Double primitive + DefaultTypeAdapters.DoubleSerializer doubleSerializer = + new DefaultTypeAdapters.DoubleSerializer(serializeSpecialFloatingPointValues); + serializers.registerIfAbsent(Double.class, doubleSerializer); + serializers.registerIfAbsent(double.class, doubleSerializer); + + // Float primitive + DefaultTypeAdapters.FloatSerializer floatSerializer = + new DefaultTypeAdapters.FloatSerializer(serializeSpecialFloatingPointValues); + serializers.registerIfAbsent(Float.class, floatSerializer); + serializers.registerIfAbsent(float.class, floatSerializer); + + // Long primitive + DefaultTypeAdapters.LongSerializer longSerializer = + new DefaultTypeAdapters.LongSerializer(longSerializationPolicy); + serializers.registerIfAbsent(Long.class, longSerializer); + serializers.registerIfAbsent(long.class, longSerializer); + + serializers.registerIfAbsent(DEFAULT_SERIALIZERS); + return serializers; + } + + static ParameterizedTypeHandlerMap> getDefaultDeserializers() { + return DEFAULT_DESERIALIZERS; + } + + static ParameterizedTypeHandlerMap> getDefaultInstanceCreators() { + return DEFAULT_INSTANCE_CREATORS; + } + + static class DefaultDateTypeAdapter implements JsonSerializer, JsonDeserializer { + private final DateFormat format; + + DefaultDateTypeAdapter() { + this.format = DateFormat.getDateTimeInstance(); + } + + DefaultDateTypeAdapter(final String datePattern) { + this.format = new SimpleDateFormat(datePattern); + } + + DefaultDateTypeAdapter(final int style) { + this.format = DateFormat.getDateInstance(style); + } + + public DefaultDateTypeAdapter(final int dateStyle, final int timeStyle) { + this.format = DateFormat.getDateTimeInstance(dateStyle, timeStyle); + } + + // These methods need to be synchronized since JDK DateFormat classes are not thread-safe + // See issue 162 + public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { + synchronized (format) { + String dateFormatAsString = format.format(src); + return new JsonPrimitive(dateFormatAsString); + } + } + + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + if (!(json instanceof JsonPrimitive)) { + throw new JsonParseException("The date should be a string value"); + } + try { + synchronized (format) { + return format.parse(json.getAsString()); + } + } catch (ParseException e) { + throw new JsonSyntaxException(e); + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(DefaultDateTypeAdapter.class.getSimpleName()); + sb.append('(').append(format.getClass().getSimpleName()).append(')'); + return sb.toString(); + } + } + + static class DefaultJavaSqlDateTypeAdapter implements JsonSerializer, + JsonDeserializer { + private final DateFormat format; + DefaultJavaSqlDateTypeAdapter() { + this.format = new SimpleDateFormat("MMM d, yyyy"); + } + + public JsonElement serialize(java.sql.Date src, Type typeOfSrc, + JsonSerializationContext context) { + synchronized (format) { + String dateFormatAsString = format.format(src); + return new JsonPrimitive(dateFormatAsString); + } + } + + public java.sql.Date deserialize(JsonElement json, Type typeOfT, + JsonDeserializationContext context) throws JsonParseException { + if (!(json instanceof JsonPrimitive)) { + throw new JsonParseException("The date should be a string value"); + } + try { + synchronized (format) { + Date date = format.parse(json.getAsString()); + return new java.sql.Date(date.getTime()); + } + } catch (ParseException e) { + throw new JsonSyntaxException(e); + } + } + } + + static class DefaultTimestampDeserializer implements JsonDeserializer { + public Timestamp deserialize(JsonElement json, Type typeOfT, + JsonDeserializationContext context) throws JsonParseException { + Date date = context.deserialize(json, Date.class); + return new Timestamp(date.getTime()); + } + } + + static class DefaultTimeTypeAdapter implements JsonSerializer