-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
currentValueattribute) - UTF8JsonGenerator (set
_writeContextto new context) - MappingJsonFactory (override
_createUTF8Generatorandcopyto use new generator) - AsPropertyTypeSerializer (override all
writePrefixmethods to set currentValue on context) - BeanSerializerBase (that my actual use-case)
- ObjectMapper (call
setDefaultTypingandsetSerializerFactory)
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.