On Sun, Apr 15, 2018 at 10:01 AM, Marcel Overdijk <mar...@overdijk.me> wrote:
> Hi,
> When I have a custom `JsonSerializer` defined via @JsonSerialize does it it
> create only 1 instance of the `JsonSerializer` or multiple?
> @JsonSerialize(using = JsonapiDocument.JsonapiDocumentSerializer::class)
> class JsonapiDocument
> I need to introspect the class to be serialized and want to cache this
> information in the Serializer.
> But to do this I want to make sure that only 1 instance of the Serializer is
> created?
> In the apidocs of the `JsonSerializer` I could't find any information /
> statement regarding this.

If I understand your case correctly, only one instance is created and
cached for lookup
This assumes that annotation is on class; if annotation is on a
property I'd have to check to make
sure whether caching is involved at all.

However: lookup and caching is just the initial part. Many
(de)serializers are further configured by
contextual information (mostly annotations on property accessor(s)) --
if serializer implements
`ContextualSerializer`, then its `createContextual(...)` method is
called when serializer is needed for
specific property (and once for root value, if used for root value).

So, only one instance should be created. I would not count on this as
anything more than as implementation
detail, however -- cache sizes are often bounded (... and in this
particular case, will be bounded in Jackson 3.0;
2.x does not limit it unfortunately) so there is no strict guarantee
that only one instance is ever created.

If such a guarantee is required, it could be achieved in at least 2 ways:

1. Canonicalize in `createContextual()` (that is, verify that `this`
is singleton instance; or if not, return that singleton
2. Use `Module` for providing serializer instead.

and I guess I would go with (1). But this is only assuming it is
critical that just one serializer instance is used.

-+ Tatu +-

You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to