diff --git a/examples/android-proguard-example/src/com/google/gson/examples/android/GsonProguardExampleActivity.java b/examples/android-proguard-example/src/com/google/gson/examples/android/GsonProguardExampleActivity.java index 3ac740e4..851a9051 100644 --- a/examples/android-proguard-example/src/com/google/gson/examples/android/GsonProguardExampleActivity.java +++ b/examples/android-proguard-example/src/com/google/gson/examples/android/GsonProguardExampleActivity.java @@ -41,13 +41,13 @@ public class GsonProguardExampleActivity extends Activity { Cart cart = buildCart(); StringBuilder sb = new StringBuilder(); sb.append("Gson.toJson() example: \n"); - sb.append(" Cart Object: ").append(cart).append("\n"); - sb.append(" Cart JSON: ").append(gson.toJson(cart)).append("\n"); + sb.append(" Cart Object: ").append(cart).append('\n'); + sb.append(" Cart JSON: ").append(gson.toJson(cart)).append('\n'); sb.append("\n\nGson.fromJson() example: \n"); String json = "{buyer:'Happy Camper',creditCard:'4111-1111-1111-1111'," + "lineItems:[{name:'nails',priceInMicros:100000,quantity:100,currencyCode:'USD'}]}"; - sb.append("Cart JSON: ").append(json).append("\n"); - sb.append("Cart Object: ").append(gson.fromJson(json, Cart.class)).append("\n"); + sb.append("Cart JSON: ").append(json).append('\n'); + sb.append("Cart Object: ").append(gson.fromJson(json, Cart.class)).append('\n'); tv.setText(sb.toString()); tv.invalidate(); } diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 1c6d949c..405799b7 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -216,9 +216,9 @@ public final class Gson { * following settings: * * @@ -665,8 +664,8 @@ public final class Gson { * is an example: * *

Let's say we want to write a type adapter that counts the number of objects being read from - * or written to JSON. We can achieve this by writing a type adapter factory that uses the - * getDelegateAdapter method: + * or written to JSON. We can achieve this by writing a type adapter factory that uses the {@code + * getDelegateAdapter} method: * *

{@code
    * class StatsTypeAdapterFactory implements TypeAdapterFactory {
diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java
index f787136d..c04dd543 100644
--- a/gson/src/main/java/com/google/gson/GsonBuilder.java
+++ b/gson/src/main/java/com/google/gson/GsonBuilder.java
@@ -227,8 +227,8 @@ public final class GsonBuilder {
   }
 
   /**
-   * Configure Gson to serialize null fields. By default, Gson omits all fields that are null during
-   * serialization.
+   * Configures Gson to serialize null fields. By default, Gson omits all fields that are null
+   * during serialization.
    *
    * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
    * @since 1.2
@@ -240,10 +240,11 @@ public final class GsonBuilder {
   }
 
   /**
-   * Enabling this feature will only change the serialized form if the map key is a complex type
-   * (i.e. non-primitive) in its serialized JSON form. The default implementation
-   * of map serialization uses {@code toString()} on the key; however, when this is called then one
-   * of the following cases apply:
+   * Configures Gson to serialize {@code Map} objects with complex keys as JSON arrays. Enabling
+   * this feature will only change the serialized form if the map key is a complex type (i.e.
+   * non-primitive) in its serialized JSON form. The default implementation of map
+   * serialization uses {@code toString()} on the key; however, when this is called then one of the
+   * following cases apply:
    *
    * 

Maps as JSON objects * @@ -622,23 +623,24 @@ public final class GsonBuilder { } /** - * Configures Gson to serialize {@code Date} objects according to the style value provided. You - * can call this method or {@link #setDateFormat(String)} multiple times, but only the last - * invocation will be used to decide the serialization format. + * Configures Gson to serialize {@code Date} objects according to the date style value provided. + * You can call this method or {@link #setDateFormat(String)} multiple times, but only the last + * invocation will be used to decide the serialization format. This methods leaves the current + * 'time style' unchanged. * - *

Note that this style value should be one of the predefined constants in the {@code - * DateFormat} class. See the documentation in {@link java.text.DateFormat} for more information - * on the valid style constants. + *

Note that this style value should be one of the predefined constants in the {@link + * DateFormat} class, such as {@link DateFormat#MEDIUM}. See the documentation of the {@link + * DateFormat} class for more information on the valid style constants. * - * @param style the predefined date style that date objects will be serialized/deserialized + * @param dateStyle the predefined date style that date objects will be serialized/deserialized * to/from * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @throws IllegalArgumentException if the style is invalid * @since 1.2 */ @CanIgnoreReturnValue - public GsonBuilder setDateFormat(int style) { - this.dateStyle = checkDateFormatStyle(style); + public GsonBuilder setDateFormat(int dateStyle) { + this.dateStyle = checkDateFormatStyle(dateStyle); this.datePattern = null; return this; } @@ -648,9 +650,9 @@ public final class GsonBuilder { * can call this method or {@link #setDateFormat(String)} multiple times, but only the last * invocation will be used to decide the serialization format. * - *

Note that this style value should be one of the predefined constants in the {@code - * DateFormat} class. See the documentation in {@link java.text.DateFormat} for more information - * on the valid style constants. + *

Note that this style value should be one of the predefined constants in the {@link + * DateFormat} class, such as {@link DateFormat#MEDIUM}. See the documentation of the {@link + * DateFormat} class for more information on the valid style constants. * * @param dateStyle the predefined date style that date objects will be serialized/deserialized * to/from diff --git a/gson/src/main/java/com/google/gson/ReflectionAccessFilter.java b/gson/src/main/java/com/google/gson/ReflectionAccessFilter.java index 44e3df09..6581da8c 100644 --- a/gson/src/main/java/com/google/gson/ReflectionAccessFilter.java +++ b/gson/src/main/java/com/google/gson/ReflectionAccessFilter.java @@ -117,6 +117,11 @@ public interface ReflectionAccessFilter { ? FilterResult.BLOCK_INACCESSIBLE : FilterResult.INDECISIVE; } + + @Override + public String toString() { + return "ReflectionAccessFilter#BLOCK_INACCESSIBLE_JAVA"; + } }; /** @@ -143,6 +148,11 @@ public interface ReflectionAccessFilter { ? FilterResult.BLOCK_ALL : FilterResult.INDECISIVE; } + + @Override + public String toString() { + return "ReflectionAccessFilter#BLOCK_ALL_JAVA"; + } }; /** @@ -169,6 +179,11 @@ public interface ReflectionAccessFilter { ? FilterResult.BLOCK_ALL : FilterResult.INDECISIVE; } + + @Override + public String toString() { + return "ReflectionAccessFilter#BLOCK_ALL_ANDROID"; + } }; /** @@ -196,6 +211,11 @@ public interface ReflectionAccessFilter { ? FilterResult.BLOCK_ALL : FilterResult.INDECISIVE; } + + @Override + public String toString() { + return "ReflectionAccessFilter#BLOCK_ALL_PLATFORM"; + } }; /** diff --git a/gson/src/main/java/com/google/gson/TypeAdapter.java b/gson/src/main/java/com/google/gson/TypeAdapter.java index 5e6f0876..fe4ce8ec 100644 --- a/gson/src/main/java/com/google/gson/TypeAdapter.java +++ b/gson/src/main/java/com/google/gson/TypeAdapter.java @@ -68,11 +68,11 @@ import java.io.Writer; * #write(JsonWriter,Object) write()} must write exactly one value. For primitive types this is * means readers should make exactly one call to {@code nextBoolean()}, {@code nextDouble()}, {@code * nextInt()}, {@code nextLong()}, {@code nextString()} or {@code nextNull()}. Writers should make - * exactly one call to one of value() or nullValue(). For arrays, type - * adapters should start with a call to {@code beginArray()}, convert all elements, and finish with - * a call to {@code endArray()}. For objects, they should start with {@code beginObject()}, convert - * the object, and finish with {@code endObject()}. Failing to convert a value or converting too - * many values may cause the application to crash. + * exactly one call to one of {@code value()} or {@code nullValue()}. For arrays, type adapters + * should start with a call to {@code beginArray()}, convert all elements, and finish with a call to + * {@code endArray()}. For objects, they should start with {@code beginObject()}, convert the + * object, and finish with {@code endObject()}. Failing to convert a value or converting too many + * values may cause the application to crash. * *

Type adapters should be prepared to read null from the stream and write it to the stream. * Alternatively, they should use {@link #nullSafe()} method while registering the type adapter with diff --git a/gson/src/main/java/com/google/gson/reflect/TypeToken.java b/gson/src/main/java/com/google/gson/reflect/TypeToken.java index ceb35dc9..49de4300 100644 --- a/gson/src/main/java/com/google/gson/reflect/TypeToken.java +++ b/gson/src/main/java/com/google/gson/reflect/TypeToken.java @@ -200,7 +200,7 @@ public class TypeToken { return rawType.isAssignableFrom($Gson$Types.getRawType(from)) && isAssignableFrom(from, (GenericArrayType) type); } else { - throw buildUnexpectedTypeError( + throw buildUnsupportedTypeException( type, Class.class, ParameterizedType.class, GenericArrayType.class); } } @@ -309,10 +309,11 @@ public class TypeToken { return false; } - private static AssertionError buildUnexpectedTypeError(Type token, Class... expected) { + private static IllegalArgumentException buildUnsupportedTypeException( + Type token, Class... expected) { // Build exception message - StringBuilder exceptionMessage = new StringBuilder("Unexpected type. Expected one of: "); + StringBuilder exceptionMessage = new StringBuilder("Unsupported type, expected one of: "); for (Class clazz : expected) { exceptionMessage.append(clazz.getName()).append(", "); } @@ -320,10 +321,9 @@ public class TypeToken { .append("but got: ") .append(token.getClass().getName()) .append(", for type token: ") - .append(token.toString()) - .append('.'); + .append(token.toString()); - return new AssertionError(exceptionMessage.toString()); + return new IllegalArgumentException(exceptionMessage.toString()); } /** diff --git a/gson/src/main/java/com/google/gson/stream/JsonReader.java b/gson/src/main/java/com/google/gson/stream/JsonReader.java index 79795605..2f6cae5f 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonReader.java +++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java @@ -1056,7 +1056,6 @@ public class JsonReader implements Closeable { * consumes the closing quote, but does not include it in the returned string. * * @param quote either ' or ". - * @throws NumberFormatException if any unicode escape sequences are malformed. */ private String nextQuotedValue(char quote) throws IOException { // Like nextNonWhitespace, this uses locals 'p' and 'l' to save inner-loop field access. @@ -1527,7 +1526,7 @@ public class JsonReader implements Closeable { } } - private void checkLenient() throws IOException { + private void checkLenient() throws MalformedJsonException { if (strictness != Strictness.LENIENT) { throw syntaxError( "Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON"); @@ -1655,7 +1654,7 @@ public class JsonReader implements Closeable { * backslash. The backslash '\' should have already been read. This supports both Unicode escapes * "u000A" and two-character escapes "\n". * - * @throws MalformedJsonException if any Unicode escape sequences are malformed. + * @throws MalformedJsonException if the escape sequence is malformed */ @SuppressWarnings("fallthrough") private char readEscapeCharacter() throws IOException { @@ -1725,10 +1724,10 @@ public class JsonReader implements Closeable { } /** - * Throws a new IO exception with the given message and a context snippet with this reader's - * content. + * Throws a new {@link MalformedJsonException} with the given message and information about the + * current location. */ - private IOException syntaxError(String message) throws IOException { + private MalformedJsonException syntaxError(String message) throws MalformedJsonException { throw new MalformedJsonException( message + locationString() + "\nSee " + TroubleshootingGuide.createUrl("malformed-json")); } diff --git a/gson/src/main/java/com/google/gson/stream/JsonScope.java b/gson/src/main/java/com/google/gson/stream/JsonScope.java index 04fe4ba8..f77b53da 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonScope.java +++ b/gson/src/main/java/com/google/gson/stream/JsonScope.java @@ -24,28 +24,25 @@ package com.google.gson.stream; */ final class JsonScope { - /** An array with no elements requires no separators or newlines before it is closed. */ + /** An array with no elements requires no separator before the next element. */ static final int EMPTY_ARRAY = 1; - /** An array with at least one value requires a comma and newline before the next element. */ + /** An array with at least one value requires a separator before the next element. */ static final int NONEMPTY_ARRAY = 2; - /** An object with no name/value pairs requires no separators or newlines before it is closed. */ + /** An object with no name/value pairs requires no separator before the next element. */ static final int EMPTY_OBJECT = 3; /** An object whose most recent element is a key. The next element must be a value. */ static final int DANGLING_NAME = 4; - /** - * An object with at least one name/value pair requires a comma and newline before the next - * element. - */ + /** An object with at least one name/value pair requires a separator before the next element. */ static final int NONEMPTY_OBJECT = 5; - /** No object or array has been started. */ + /** No top-level value has been started yet. */ static final int EMPTY_DOCUMENT = 6; - /** A document with at an array or object. */ + /** A top-level value has already been started. */ static final int NONEMPTY_DOCUMENT = 7; /** A document that's been closed and cannot be accessed. */ diff --git a/gson/src/main/java/com/google/gson/stream/JsonWriter.java b/gson/src/main/java/com/google/gson/stream/JsonWriter.java index 23786520..fd0b6b8a 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonWriter.java +++ b/gson/src/main/java/com/google/gson/stream/JsonWriter.java @@ -356,9 +356,9 @@ public class JsonWriter implements Closeable, Flushable { /** * Configures this writer to emit JSON that's safe for direct inclusion in HTML and XML documents. - * This escapes the HTML characters {@code <}, {@code >}, {@code &} and {@code =} before writing - * them to the stream. Without this setting, your XML/HTML encoder should replace these characters - * with the corresponding escape sequences. + * This escapes the HTML characters {@code <}, {@code >}, {@code &}, {@code =} and {@code '} + * before writing them to the stream. Without this setting, your XML/HTML encoder should replace + * these characters with the corresponding escape sequences. */ public final void setHtmlSafe(boolean htmlSafe) { this.htmlSafe = htmlSafe; diff --git a/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java b/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java index b0ee01a4..36a5da44 100644 --- a/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java +++ b/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java @@ -297,7 +297,7 @@ public class JsonPrimitiveTest { new JsonPrimitive(new BigInteger("0")), new JsonPrimitive(0)); MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive(0), new JsonPrimitive(0L)); MoreAsserts.assertEqualsAndHashCode( - new JsonPrimitive(new BigInteger("0")), new JsonPrimitive(0)); + new JsonPrimitive(new BigDecimal("0")), new JsonPrimitive(0)); MoreAsserts.assertEqualsAndHashCode( new JsonPrimitive(Float.NaN), new JsonPrimitive(Double.NaN)); } diff --git a/gson/src/test/java/com/google/gson/MixedStreamTest.java b/gson/src/test/java/com/google/gson/MixedStreamTest.java index afc35baa..42fed496 100644 --- a/gson/src/test/java/com/google/gson/MixedStreamTest.java +++ b/gson/src/test/java/com/google/gson/MixedStreamTest.java @@ -74,11 +74,9 @@ public final class MixedStreamTest { JsonReader jsonReader = new JsonReader(stringReader); jsonReader.beginArray(); - // actual and expected object are inverted in the test. - // gson.fromJson(jsonReader, Car.class) as arg of assertThat() cause an ambiguous method call - assertThat(BLUE_MUSTANG).isEqualTo(gson.fromJson(jsonReader, Car.class)); - assertThat(BLACK_BMW).isEqualTo(gson.fromJson(jsonReader, Car.class)); - assertThat(RED_MIATA).isEqualTo(gson.fromJson(jsonReader, Car.class)); + assertThat(gson.fromJson(jsonReader, Car.class)).isEqualTo(BLUE_MUSTANG); + assertThat(gson.fromJson(jsonReader, Car.class)).isEqualTo(BLACK_BMW); + assertThat(gson.fromJson(jsonReader, Car.class)).isEqualTo(RED_MIATA); jsonReader.endArray(); } diff --git a/gson/src/test/java/com/google/gson/MockExclusionStrategy.java b/gson/src/test/java/com/google/gson/MockExclusionStrategy.java deleted file mode 100644 index f2899ddb..00000000 --- a/gson/src/test/java/com/google/gson/MockExclusionStrategy.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.google.gson; - -/** - * This is a configurable {@link ExclusionStrategy} that can be used for unit testing. - * - * @author Joel Leitch - */ -final class MockExclusionStrategy implements ExclusionStrategy { - private final boolean skipClass; - private final boolean skipField; - - public MockExclusionStrategy(boolean skipClass, boolean skipField) { - this.skipClass = skipClass; - this.skipField = skipField; - } - - @Override - public boolean shouldSkipField(FieldAttributes f) { - return skipField; - } - - @Override - public boolean shouldSkipClass(Class clazz) { - return skipClass; - } -} diff --git a/gson/src/test/java/com/google/gson/PrimitiveTypeAdapter.java b/gson/src/test/java/com/google/gson/PrimitiveTypeAdapter.java index 794e7145..bc626c2c 100644 --- a/gson/src/test/java/com/google/gson/PrimitiveTypeAdapter.java +++ b/gson/src/test/java/com/google/gson/PrimitiveTypeAdapter.java @@ -24,6 +24,8 @@ import java.lang.reflect.Method; /** * Handles type conversion from some object to some primitive (or primitive wrapper instance). * + *

Used by {@link ParameterizedTypeFixtures.MyParameterizedTypeAdapter}. + * * @author Joel Leitch */ final class PrimitiveTypeAdapter { diff --git a/gson/src/test/java/com/google/gson/functional/CollectionTest.java b/gson/src/test/java/com/google/gson/functional/CollectionTest.java index 6e0693ec..54bf68c2 100644 --- a/gson/src/test/java/com/google/gson/functional/CollectionTest.java +++ b/gson/src/test/java/com/google/gson/functional/CollectionTest.java @@ -237,8 +237,8 @@ public class CollectionTest { target.add(objB); String result = gson.toJson(target); - assertThat(result.startsWith("[")).isTrue(); - assertThat(result.endsWith("]")).isTrue(); + assertThat(result).startsWith("["); + assertThat(result).endsWith("]"); for (BagOfPrimitives obj : target) { assertThat(result).contains(obj.getExpectedJson()); } @@ -290,10 +290,8 @@ public class CollectionTest { for (Object bag1 : target) { // Gson 2.0 converts raw objects into maps @SuppressWarnings("unchecked") - Map values = (Map) bag1; - assertThat(values.containsValue(10.0)).isTrue(); - assertThat(values.containsValue(20.0)).isTrue(); - assertThat(values.containsValue("stringValue")).isTrue(); + Map map = (Map) bag1; + assertThat(map.values()).containsExactly(10.0, 20.0, false, "stringValue"); } } diff --git a/gson/src/test/java/com/google/gson/functional/EscapingTest.java b/gson/src/test/java/com/google/gson/functional/EscapingTest.java index 5dd4e70e..1383c67c 100644 --- a/gson/src/test/java/com/google/gson/functional/EscapingTest.java +++ b/gson/src/test/java/com/google/gson/functional/EscapingTest.java @@ -82,7 +82,7 @@ public class EscapingTest { BagOfPrimitives target = new BagOfPrimitives(1L, 1, true, "test' / w'ith\" / \\