Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LongSerializationPolicy null handling being inconsistent with Gson #1990

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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));
}
}