jsonb-api icon indicating copy to clipboard operation
jsonb-api copied to clipboard

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

Open jsonbrobot opened this issue 7 years ago • 3 comments

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 avatar Oct 05 '17 11:10 jsonbrobot

  • Issue Imported From: https://github.com/javaee/jsonb-spec/issues/58
  • Original Issue Raised By:@Shaunwild97
  • Original Issue Assigned To: Unassigned

jsonbrobot avatar May 23 '18 22:05 jsonbrobot

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

jsonbrobot avatar Oct 10 '17 10:10 jsonbrobot

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

jsonbrobot avatar Oct 10 '17 14:10 jsonbrobot