@JsonSerialize ignoring properties with annotation?

14 views
Skip to first unread message

Howard Rees

unread,
Jul 7, 2019, 5:14:14 AM7/7/19
to jackson-user
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?

Any help would be much appreciated.

Tatu Saloranta

unread,
Jul 14, 2019, 3:41:37 AM7/14/19
to jackson-user
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 +- 
 
Reply all
Reply to author
Forward
0 new messages