jsonb-api
jsonb-api copied to clipboard
Custom Serialization should work on all levels and allow "super- generics"?
Custom Serialization currently doesn't seem to trigger for anything other than the initial object passed into "toJson", should this be the case? I want to serialize every instance of X in a specific way, but this doesn't seem possible at the moment. This could be a new property "DEEP_CUSTOM_SERIALIZATION"
Also should JsonbSerializer<Object> allow me to add custom serialization for every object passed into "toJson" ?
- Issue Imported From: https://github.com/javaee/jsonb-spec/issues/58
- Original Issue Raised By:@Shaunwild97
- Original Issue Assigned To: Unassigned
@m0mus Commented Please be more specific in your request. Sample code would be nice to have for better understanding. :)
@Shaunwild97 Commented
JsonbBuilder
.newBuilder()
.withConfig( new JsonbConfig()
.setProperty( DEEP_CUSTOM_SERIALIZATION, true ) )
.build();
public class EmptyListSerializer implements JsonbSerializer<Collection>
{
@Override
public void serialize( Collection collection, JsonGenerator jg, SerializationContext sc )
{
if ( collection == null )
{
return;
}
if ( collection.isEmpty() )
{
sc.serialize( null, jg );
}
else
{
sc.serialize( collection, jg );
}
}
}
With the above code snippets, If I serialized a class which had multiple Collections in it, they would all trigger the custom serializer. With DEEP_CUSTOM_SERIALIZATION set to false, only the top level Collection would trigger the custom serialization (this is what happens currently by default)
My second point was referring to A JsonbSerializer with generic type <Object> that would work for every object. The generic type in JsonbSerializer should be the superclass of the object, not the object itself.