Fixed com.google.gson.functional.VersioningTest.testIgnoreLaterVersionClassDeserialization by changing Gson behavior incompatibly (but more consistent).
GSON 1.x applies different rules for versioning for classes vs fields. So, if you deserialize a JSON into a field that is supposed to be skipped, the field is set to null (or default value). However, if you deserialize it to a top-level class, a default instance is returned. GSON 2.x returns null for the top-level class.
This commit is contained in:
parent
8b21c7770b
commit
2541e658f7
|
@ -53,3 +53,9 @@ GSON 1.x sometimes sets subclass fields when an InstanceCreator returns a subcla
|
||||||
the top level object or a collection element.
|
the top level object or a collection element.
|
||||||
GSON 2.x sets fields of the requested type only
|
GSON 2.x sets fields of the requested type only
|
||||||
com.google.gson.functional.InstanceCreatorTest.testInstanceCreatorReturnsSubTypeForField
|
com.google.gson.functional.InstanceCreatorTest.testInstanceCreatorReturnsSubTypeForField
|
||||||
|
|
||||||
|
GSON 1.x applies different rules for versioning for classes vs fields. So, if you deserialize a
|
||||||
|
JSON into a field that is supposed to be skipped, the field is set to null (or default value).
|
||||||
|
However, if you deserialize it to a top-level class, a default instance is returned.
|
||||||
|
GSON 2.x returns null for the top-level class.
|
||||||
|
com.google.gson.functional.VersioningTest.testIgnoreLaterVersionClassDeserialization
|
|
@ -88,13 +88,10 @@ public class VersioningTest extends TestCase {
|
||||||
public void testIgnoreLaterVersionClassDeserialization() {
|
public void testIgnoreLaterVersionClassDeserialization() {
|
||||||
Gson gson = builder.setVersion(1.0).create();
|
Gson gson = builder.setVersion(1.0).create();
|
||||||
String json = "{\"a\":3,\"b\":4,\"c\":5,\"d\":6}";
|
String json = "{\"a\":3,\"b\":4,\"c\":5,\"d\":6}";
|
||||||
Version1_2 version1_2 = gson.fromJson(json, Version1_2.class);
|
Version1_2 version1_2 = gson.fromJson(json, Version1_2.class);
|
||||||
// Since the class is versioned to be after 1.0, all fields should get set to
|
// Since the class is versioned to be after 1.0, we expect null
|
||||||
// their default values.
|
// This is the new behavior in Gson 2.0
|
||||||
assertEquals(A, version1_2.a);
|
assertNull(version1_2);
|
||||||
assertEquals(B, version1_2.b);
|
|
||||||
assertEquals(C, version1_2.c);
|
|
||||||
assertEquals(D, version1_2.d);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testVersionedGsonWithUnversionedClassesSerialization() {
|
public void testVersionedGsonWithUnversionedClassesSerialization() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user