jansupol / jsonbapi

0 stars 0 forks source link

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

Open jansupol opened 6 years ago

jansupol commented 6 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" ?

jansupol commented 6 years ago
jansupol commented 6 years ago

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

jansupol commented 6 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.