Skip to content

JsonWriteContext could hold current value. #631

@Antibrumm

Description

@Antibrumm

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 and copy to use new generator)
  • AsPropertyTypeSerializer (override all writePrefix methods to set currentValue on context)
  • BeanSerializerBase (that my actual use-case)
  • ObjectMapper (call setDefaultTyping and setSerializerFactory)

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions