Skip to content

Commit cd748df

Browse files
authored
Fix LongSerializationPolicy null handling being inconsistent with Gson (#1990)
Gson does not actually use the specified LongSerializationPolicy but instead uses type adapters which emulate the behavior. However, previously Gson's implementation did not match LongSerializationPolicy regarding null handling. Because it is rather unlikely that LongSerializationPolicy has been used on its own, this commit adjusts its implementation to match Gson's behavior (instead of the other way around).
1 parent fe30b85 commit cd748df

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

Diff for: gson/src/main/java/com/google/gson/LongSerializationPolicy.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.google.gson;
1818

1919
/**
20-
* Defines the expected format for a {@code long} or {@code Long} type when its serialized.
20+
* Defines the expected format for a {@code long} or {@code Long} type when it is serialized.
2121
*
2222
* @since 1.3
2323
*
@@ -26,25 +26,35 @@
2626
*/
2727
public enum LongSerializationPolicy {
2828
/**
29-
* This is the "default" serialization policy that will output a {@code long} object as a JSON
29+
* This is the "default" serialization policy that will output a {@code Long} object as a JSON
3030
* number. For example, assume an object has a long field named "f" then the serialized output
3131
* would be:
32-
* {@code {"f":123}}.
32+
* {@code {"f":123}}
33+
*
34+
* <p>A {@code null} value is serialized as {@link JsonNull}.
3335
*/
3436
DEFAULT() {
3537
@Override public JsonElement serialize(Long value) {
38+
if (value == null) {
39+
return JsonNull.INSTANCE;
40+
}
3641
return new JsonPrimitive(value);
3742
}
3843
},
3944

4045
/**
4146
* Serializes a long value as a quoted string. For example, assume an object has a long field
4247
* named "f" then the serialized output would be:
43-
* {@code {"f":"123"}}.
48+
* {@code {"f":"123"}}
49+
*
50+
* <p>A {@code null} value is serialized as {@link JsonNull}.
4451
*/
4552
STRING() {
4653
@Override public JsonElement serialize(Long value) {
47-
return new JsonPrimitive(String.valueOf(value));
54+
if (value == null) {
55+
return JsonNull.INSTANCE;
56+
}
57+
return new JsonPrimitive(value.toString());
4858
}
4959
};
5060

Diff for: gson/src/test/java/com/google/gson/LongSerializationPolicyTest.java

+26-6
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,31 @@ public class LongSerializationPolicyTest extends TestCase {
2929
public void testDefaultLongSerialization() throws Exception {
3030
JsonElement element = LongSerializationPolicy.DEFAULT.serialize(1556L);
3131
assertTrue(element.isJsonPrimitive());
32-
32+
3333
JsonPrimitive jsonPrimitive = element.getAsJsonPrimitive();
3434
assertFalse(jsonPrimitive.isString());
3535
assertTrue(jsonPrimitive.isNumber());
3636
assertEquals(1556L, element.getAsLong());
3737
}
38-
38+
3939
public void testDefaultLongSerializationIntegration() {
4040
Gson gson = new GsonBuilder()
41-
.setLongSerializationPolicy(LongSerializationPolicy.DEFAULT)
42-
.create();
41+
.setLongSerializationPolicy(LongSerializationPolicy.DEFAULT)
42+
.create();
4343
assertEquals("[1]", gson.toJson(new long[] { 1L }, long[].class));
4444
assertEquals("[1]", gson.toJson(new Long[] { 1L }, Long[].class));
4545
}
4646

47+
public void testDefaultLongSerializationNull() {
48+
LongSerializationPolicy policy = LongSerializationPolicy.DEFAULT;
49+
assertTrue(policy.serialize(null).isJsonNull());
50+
51+
Gson gson = new GsonBuilder()
52+
.setLongSerializationPolicy(policy)
53+
.create();
54+
assertEquals("null", gson.toJson(null, Long.class));
55+
}
56+
4757
public void testStringLongSerialization() throws Exception {
4858
JsonElement element = LongSerializationPolicy.STRING.serialize(1556L);
4959
assertTrue(element.isJsonPrimitive());
@@ -56,9 +66,19 @@ public void testStringLongSerialization() throws Exception {
5666

5767
public void testStringLongSerializationIntegration() {
5868
Gson gson = new GsonBuilder()
59-
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
60-
.create();
69+
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
70+
.create();
6171
assertEquals("[\"1\"]", gson.toJson(new long[] { 1L }, long[].class));
6272
assertEquals("[\"1\"]", gson.toJson(new Long[] { 1L }, Long[].class));
6373
}
74+
75+
public void testStringLongSerializationNull() {
76+
LongSerializationPolicy policy = LongSerializationPolicy.STRING;
77+
assertTrue(policy.serialize(null).isJsonNull());
78+
79+
Gson gson = new GsonBuilder()
80+
.setLongSerializationPolicy(policy)
81+
.create();
82+
assertEquals("null", gson.toJson(null, Long.class));
83+
}
6484
}

0 commit comments

Comments
 (0)