On Sun, Jul 7, 2019 at 2:14 AM 'Howard Rees' via jackson-user <
[email protected]> wrote:

> Hi,
> I have a requirement to uppercase certain values during serialisation, so
> I've created a custom serialiser that accepts an argument within the
> constructor as I only want this code executing when I use the ObjectMapper.
>
> So in my code when I execute:
>
> ObjectMapper mapper = new ObjectMapper().registerModule(new 
> SimpleModule().addSerializer(new UpperCaseSerializer(true)));
>
> String serialized = mapper.writeValueAsString(person);
>
>
> it appears that the custom serialiser fires for any property that doesn't
> have
>
> @JsonSerialize(using = UpperCaseSerializer.class)
>
>
> attached?   So if I have 10 properties, and this annotation is on only 1,
> I end up with a string where 9 of the values are uppercase and the one I
> actually want is lowercase?   Is there something I'm missing with this?
>

I think you misunderstand role of custom serializers for ObjectMapper, and
annotation overrides.

Serializers are located so that if there is specific annotation dictating
serializer to use, that serializer will be used without any further
attempts. So don't use annotation if you do not want that specific
serializer to be used.

Conversely global registration will be used for all properties for which
there is no annotation to define specific serializer to use. It does
prevent Jackson from using one of default serializers (including POJO
serializer if nothing more specific known about type).

If you want more advanced logic (for example, only use specific serializer
for some annotation), you will need to do something else.

-+ 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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/CAGrxA26onsH_b93UcxWOZ5x9z6xz8ArELgBn7OGz_E2q3ZSEzg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to