Skip to content

Commit

Permalink
Merge branch 'google:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
sakibguy committed Oct 12, 2021
2 parents a20d54a + bda2e3d commit dd0578b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 15 deletions.
2 changes: 2 additions & 0 deletions gson/src/main/java/com/google/gson/GsonBuilder.java
Expand Up @@ -130,6 +130,8 @@ public GsonBuilder() {
this.timeStyle = gson.timeStyle;
this.factories.addAll(gson.builderFactories);
this.hierarchyFactories.addAll(gson.builderHierarchyFactories);
this.objectToNumberStrategy = gson.objectToNumberStrategy;
this.numberToNumberStrategy = gson.numberToNumberStrategy;
}

/**
Expand Down
20 changes: 15 additions & 5 deletions gson/src/main/java/com/google/gson/LongSerializationPolicy.java
Expand Up @@ -17,7 +17,7 @@
package com.google.gson;

/**
* Defines the expected format for a {@code long} or {@code Long} type when its serialized.
* Defines the expected format for a {@code long} or {@code Long} type when it is serialized.
*
* @since 1.3
*
Expand All @@ -26,25 +26,35 @@
*/
public enum LongSerializationPolicy {
/**
* This is the "default" serialization policy that will output a {@code long} object as a JSON
* This is the "default" serialization policy that will output a {@code Long} object as a JSON
* number. For example, assume an object has a long field named "f" then the serialized output
* would be:
* {@code {"f":123}}.
* {@code {"f":123}}
*
* <p>A {@code null} value is serialized as {@link JsonNull}.
*/
DEFAULT() {
@Override public JsonElement serialize(Long value) {
if (value == null) {
return JsonNull.INSTANCE;
}
return new JsonPrimitive(value);
}
},

/**
* Serializes a long value as a quoted string. For example, assume an object has a long field
* named "f" then the serialized output would be:
* {@code {"f":"123"}}.
* {@code {"f":"123"}}
*
* <p>A {@code null} value is serialized as {@link JsonNull}.
*/
STRING() {
@Override public JsonElement serialize(Long value) {
return new JsonPrimitive(String.valueOf(value));
if (value == null) {
return JsonNull.INSTANCE;
}
return new JsonPrimitive(value.toString());
}
};

Expand Down
6 changes: 3 additions & 3 deletions gson/src/main/java/com/google/gson/ToNumberPolicy.java
Expand Up @@ -71,11 +71,11 @@ public enum ToNumberPolicy implements ToNumberStrategy {
try {
Double d = Double.valueOf(value);
if ((d.isInfinite() || d.isNaN()) && !in.isLenient()) {
throw new MalformedJsonException("JSON forbids NaN and infinities: " + d + in);
throw new MalformedJsonException("JSON forbids NaN and infinities: " + d + "; at path " + in.getPath());
}
return d;
} catch (NumberFormatException doubleE) {
throw new JsonParseException("Cannot parse " + value, doubleE);
throw new JsonParseException("Cannot parse " + value + "; at path " + in.getPath(), doubleE);
}
}
}
Expand All @@ -91,7 +91,7 @@ public enum ToNumberPolicy implements ToNumberStrategy {
try {
return new BigDecimal(value);
} catch (NumberFormatException e) {
throw new JsonParseException("Cannot parse " + value, e);
throw new JsonParseException("Cannot parse " + value + "; at path " + in.getPath(), e);
}
}
}
Expand Down
Expand Up @@ -283,7 +283,7 @@ JsonElement nextJsonElement() throws IOException {
}

@Override public String toString() {
return getClass().getSimpleName();
return getClass().getSimpleName() + locationString();
}

public void promoteNameToValue() throws IOException {
Expand Down
Expand Up @@ -29,21 +29,31 @@ public class LongSerializationPolicyTest extends TestCase {
public void testDefaultLongSerialization() throws Exception {
JsonElement element = LongSerializationPolicy.DEFAULT.serialize(1556L);
assertTrue(element.isJsonPrimitive());

JsonPrimitive jsonPrimitive = element.getAsJsonPrimitive();
assertFalse(jsonPrimitive.isString());
assertTrue(jsonPrimitive.isNumber());
assertEquals(1556L, element.getAsLong());
}

public void testDefaultLongSerializationIntegration() {
Gson gson = new GsonBuilder()
.setLongSerializationPolicy(LongSerializationPolicy.DEFAULT)
.create();
.setLongSerializationPolicy(LongSerializationPolicy.DEFAULT)
.create();
assertEquals("[1]", gson.toJson(new long[] { 1L }, long[].class));
assertEquals("[1]", gson.toJson(new Long[] { 1L }, Long[].class));
}

public void testDefaultLongSerializationNull() {
LongSerializationPolicy policy = LongSerializationPolicy.DEFAULT;
assertTrue(policy.serialize(null).isJsonNull());

Gson gson = new GsonBuilder()
.setLongSerializationPolicy(policy)
.create();
assertEquals("null", gson.toJson(null, Long.class));
}

public void testStringLongSerialization() throws Exception {
JsonElement element = LongSerializationPolicy.STRING.serialize(1556L);
assertTrue(element.isJsonPrimitive());
Expand All @@ -56,9 +66,19 @@ public void testStringLongSerialization() throws Exception {

public void testStringLongSerializationIntegration() {
Gson gson = new GsonBuilder()
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
.create();
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
.create();
assertEquals("[\"1\"]", gson.toJson(new long[] { 1L }, long[].class));
assertEquals("[\"1\"]", gson.toJson(new Long[] { 1L }, Long[].class));
}

public void testStringLongSerializationNull() {
LongSerializationPolicy policy = LongSerializationPolicy.STRING;
assertTrue(policy.serialize(null).isJsonNull());

Gson gson = new GsonBuilder()
.setLongSerializationPolicy(policy)
.create();
assertEquals("null", gson.toJson(null, Long.class));
}
}
24 changes: 24 additions & 0 deletions gson/src/test/java/com/google/gson/ToNumberPolicyTest.java
Expand Up @@ -33,6 +33,12 @@ public void testDouble() throws IOException {
strategy.readNumber(fromString("1e400"));
fail();
} catch (MalformedJsonException expected) {
assertEquals("JSON forbids NaN and infinities: Infinity at line 1 column 6 path $", expected.getMessage());
}
try {
strategy.readNumber(fromString("\"not-a-number\""));
fail();
} catch (NumberFormatException expected) {
}
}

Expand All @@ -52,24 +58,35 @@ public void testLongOrDouble() throws IOException {
strategy.readNumber(fromString("1e400"));
fail();
} catch (MalformedJsonException expected) {
assertEquals("JSON forbids NaN and infinities: Infinity; at path $", expected.getMessage());
}
try {
strategy.readNumber(fromString("\"not-a-number\""));
fail();
} catch (JsonParseException expected) {
assertEquals("Cannot parse not-a-number; at path $", expected.getMessage());
}

assertEquals(Double.NaN, strategy.readNumber(fromStringLenient("NaN")));
assertEquals(Double.POSITIVE_INFINITY, strategy.readNumber(fromStringLenient("Infinity")));
assertEquals(Double.NEGATIVE_INFINITY, strategy.readNumber(fromStringLenient("-Infinity")));
try {
strategy.readNumber(fromString("NaN"));
fail();
} catch (MalformedJsonException expected) {
assertEquals("Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $", expected.getMessage());
}
try {
strategy.readNumber(fromString("Infinity"));
fail();
} catch (MalformedJsonException expected) {
assertEquals("Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $", expected.getMessage());
}
try {
strategy.readNumber(fromString("-Infinity"));
fail();
} catch (MalformedJsonException expected) {
assertEquals("Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $", expected.getMessage());
}
}

Expand All @@ -78,6 +95,13 @@ public void testBigDecimal() throws IOException {
assertEquals(new BigDecimal("10.1"), strategy.readNumber(fromString("10.1")));
assertEquals(new BigDecimal("3.141592653589793238462643383279"), strategy.readNumber(fromString("3.141592653589793238462643383279")));
assertEquals(new BigDecimal("1e400"), strategy.readNumber(fromString("1e400")));

try {
strategy.readNumber(fromString("\"not-a-number\""));
fail();
} catch (JsonParseException expected) {
assertEquals("Cannot parse not-a-number; at path $", expected.getMessage());
}
}

public void testNullsAreNeverExpected() throws IOException {
Expand Down

0 comments on commit dd0578b

Please sign in to comment.