jakartaee / jsonb-api

Jakarta JSON Binding
https://eclipse-ee4j.github.io/jsonb-api/
Other
78 stars 39 forks source link

Custom Serialization should work on all levels and allow "super- generics"? #58

Open jsonbrobot opened 7 years ago

jsonbrobot commented 7 years ago

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" ?

jsonbrobot commented 6 years ago
jsonbrobot commented 7 years ago

@m0mus Commented Please be more specific in your request. Sample code would be nice to have for better understanding. :)

jsonbrobot commented 7 years ago

@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.