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.
|
||||
GSON 2.x sets fields of the requested type only
|
||||
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() {
|
||||
Gson gson = builder.setVersion(1.0).create();
|
||||
String json = "{\"a\":3,\"b\":4,\"c\":5,\"d\":6}";
|
||||
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
|
||||
// their default values.
|
||||
assertEquals(A, version1_2.a);
|
||||
assertEquals(B, version1_2.b);
|
||||
assertEquals(C, version1_2.c);
|
||||
assertEquals(D, version1_2.d);
|
||||
Version1_2 version1_2 = gson.fromJson(json, Version1_2.class);
|
||||
// Since the class is versioned to be after 1.0, we expect null
|
||||
// This is the new behavior in Gson 2.0
|
||||
assertNull(version1_2);
|
||||
}
|
||||
|
||||
public void testVersionedGsonWithUnversionedClassesSerialization() {
|
||||
|
Loading…
Reference in New Issue
Block a user