-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Goal
I want to have type information to be added only to JSON objects as properties to retain the general structure of the generated JSON, i.e.:
- never add type information for a collection/array type, as a construct
list: ["com.example.MyType", [...]]
can not be handled by standard REST clients, for which the information is irrelevant anyways - don't add the type information to objects that are not serialized as objects, e.g. dates serialized to ISO strings, as again
["java.util.date", "2023-01-01T00:00:00+00:00"]
is not a valid JSON date
Justification
The goal is to have a valid JSON/javascript representation (arrays only for collections/array, but no array wrappers to include type information), just with additional type information for java clients wherever possible.
Proposed Solution
Add a configuration option to the ObjectMapper
(new value to DefaultTyping
) to only add type information properties to objects that are serialized as JSON objects.
Remarks
It is clear that not everything can be deserialized correctly, when using this approach, if there is not enough contextual information (e.g. deserializing a list of dates as a List), but it is better than having no type information at all, while still being able to also have non-Java clients.
Current Workaround
It seems that the only way to do this currently is to override ObjectMapper.DefaultTypeResolverBuilder
similar to here, additionally exclude Date
(serialized to ISO string), classes with custom de/serialization to something other than objects, etc. But it still fails for some generics (where type argument is Date, e.g. a custom Period<Date>
class, where the date fields from
and to
are still generated as ["java.util.date", "2023-..."]
), etc.