Replies: 8 comments 4 replies
-
Quick note: this
is not supported usage: serialization context (
(although it should warn application code against using the other method either) so basically instance is not valid for usage. But there is a way to set default attributes:
and I am bit surprised that you did not notice that... |
Beta Was this translation helpful? Give feedback.
-
I did notice |
Beta Was this translation helpful? Give feedback.
-
Ok. Usage is not super intuitive, but from unit test (
is the way to go. Note fluent style for construction, instead of constructor usage. No configuration should require modifying |
Beta Was this translation helpful? Give feedback.
-
Also
calls
which leaves us with the same problem as
|
Beta Was this translation helpful? Give feedback.
-
Your suggestion seems to modify a global context, and it seems to be backed by
|
Beta Was this translation helpful? Give feedback.
-
OH here's what I didn't see.. a very unexpected side effect in withSharedAttribute()
|
Beta Was this translation helpful? Give feedback.
-
I think that's a stretch for folks to discover. One normally stops at any sort of .emptyFoo() method is used because normally those are immutable and unusable. Also, fluent interfaces normally return the same instance, so that's unexpected too. |
Beta Was this translation helpful? Give feedback.
-
Anway, thanks for the help. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I recently found myself trying to communicate what amounts to a feature flag into a custom serializer meant to be used by an ObjectIdGenerator via
@JsonIdentityInfo(generator=)
, and setting an attribute on the SerializizerProvider seems to be a way to do that. Initially, I was quite pleased to find that mapper.getSerializerProvider().setAttribute() existed and at first blush it seemed perfect, but shockingly that produces a null pointer exception. This can be seen in both 2.10.2 and in 2.19.0 with this trivial example:I am aware that one can set the attribute via mapper.writer().withAttribute() but that's inconvenient in systems where an ObjectMapper is injected, because then EVERY place that mapper is used, the attribute must be set independently.
I notice that the javadoc on setAttribute is focused on overriding defaults, but it seems like this method should throw a better exception suggesting the correct method, and there should be a setDefaultAttribute() or some other mechanism to allow intialization of a mapper with default attributes (or if it exists it should be mentioned in this javadoc because I couldn't find it ;) ).
Of course I could use a system property, but that's an ugly, needlessly global solution that will cross talk across all instantiated mappers in the JVM.
Beta Was this translation helpful? Give feedback.
All reactions