RuntimeTypeAdapterFactory
overrides explicit control over serializeNulls
behavior of contained objects
#676
Labels
By default
serializeNulls
behavior is off for GSON which is useful for building compact JSON. However, for specific types of objects its necessary to switch this on to make a distinction between (a) properties for which no explicit value has been assigned vs (b) properties for which anull
value has been explicitly assigned.One way this is achieved is to use a
TypeAdapter
implementation to temporarily override the setting of this behavior on theJsonWriter
.E.g.
However, when such a type is part of another class which is serialized within another wrapper object which relies on a
RuntimeTypeAdapterFactory
, these explicit null override behavior can be lost. That is because, the implementation ofTypeAdapter.write
initially serializes the outer value using a temporaryJsonTreeWriter
into aJsonElement
. This temporary writer invokes the custom TypeAdapter and thus accurately collects the explicitnull
values. However,RuntimeTypeAdapterFactory
then serializes theJsonElement
using the externally providedJsonWriter
using its ownserializeNulls
configuration and therefore the explicitnull
values in the element are discarded.A full test is included below:
The text was updated successfully, but these errors were encountered: