Steffen Dettmer
unread,Jun 22, 2018, 12:44:00 PM6/22/18Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to jsonschema...@googlegroups.com
Hi,
in one schema we have multiple optional lists, as artificial example:
"Type": {
"properties": {
"AList": {
"type": "array",
"items": { ... }
},
"BList": {
"type": "array",
"items": { ... }
},
required: []
}
}
They are optional, normally not present in the JSON on wire. If
they are not present, in JSON it looks like:
"Type": { }
GSON-annotated generated class contains:
class Type {
@SerializedName("AList")
@Expose
private List<AList> aList = new ArrayList<AList>();
@SerializedName("BList")
@Expose
private List<BList> bList = new ArrayList<BList>();
}
and happily parses this, but the resulting Type instance then has
two zero-length lists:
Type instance = gson.fromJson(json);
Assert.assertNotNull(instance.getAList());
Assert.assertEquals(0, instance.getAList().size());
instead of being null, when encoding / serializing this leads to:
"Type": { "AList": [], "BList": [] }
which is not the same as the input was.
I hope I explained well what my issue / question is about.
We can easily fix this (e.g. instance.setAList(null)), but I
wonder why this behavior is correct and expected or if we do
anything wrong.
Am I generally correct that I expect:
Type instance = new Type();
String json = gson.toJson(instance);
to produce:
"Type": { }
or is including the two empty lists actually correct / required
by some standard?
Technically it could be fixed by initializing the members to null
instead of new ArrayList<BList>() I think. Is this true?
Any hints or thoughts?
Best regards,
Steffen