-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
It would be great if we could access the currentValue
from the JsonWriteContext
. It would give us more flexibility during filtering.
In my particular case I have implemented a BackReferenceAwareBeanSerializer
to be able to break bi-directional relationships. As a side effect I'm also able to break circular references within the path.
For this to work the JsonWriteContext
needs to know the currentValue
.
Unfortunately the JsonWriteContext.writeValue
method is final, as well as the UTF8JsonGenerators.writeStartObject/writeStartArray
methods, so I was not able to simply call setCurrentValue
directly from within these classes. I had to abuse a TypeSerializer
as this one can act as a callback on any write operation.
Currently I had to override these classes and it would be great if that could be simplified:
- JsonWriteContext (add
currentValue
attribute) - UTF8JsonGenerator (set
_writeContext
to new context) - MappingJsonFactory (override
_createUTF8Generator
andcopy
to use new generator) - AsPropertyTypeSerializer (override all
writePrefix
methods to set currentValue on context) - BeanSerializerBase (that my actual use-case)
- ObjectMapper (call
setDefaultTyping
andsetSerializerFactory
)
During serialization the BackReferenceAwareBeanSerializer
walks back up the context hierarchy and compares the pojo with the currentValues and writes the "level" of that as @backReference
.
The ObjectIdGenerator feature would not be sufficient here as generated ids are "global". This implementation simply breaks references but continues serializing each path indepentantly.