hollow icon indicating copy to clipboard operation
hollow copied to clipboard

Add Jackson module to allow serialization of HollowObjects

Open DanielThomas opened this issue 6 years ago • 4 comments

This Jackson module allows direct serialization of HollowObjects using Jackson datamapper.

  • It supports both the default and ergonomic generated APIs
  • You can optionally preserve the original order of the fields, because reflection doesn't return methods in their declared order. Implemented by walking the class files to avoid coupling the module to a single consumer/API to get at the schema
  • Single-value non-reference types are automatically expanded

The only situation where the serialized json won't match the original object is when a HollowObject with more than one time, or a reference type, is used as a Map key. In that case, we fall back to the default Jackson behavior of calling toString on the object.

I couldn't see a logic place to put this in the documentation, and you might want this to be a separate artifact, rather than bundled with the json adapter, but felt like a good start.

DanielThomas avatar Oct 12 '18 23:10 DanielThomas

🔜

I've asked for feedback from Drew and Paul. Thanks for your patience, Danny.

ghost avatar Feb 23 '19 03:02 ghost

Hi @DanielThomas, in what scenario would you prefer this over the HollowRecordJsonStringifier?

dkoszewnik avatar Feb 25 '19 21:02 dkoszewnik

@PaulSandoz though there's no guarantee, it seems the order in the class file is the order as declared.

@dkoszewnik the objects in our model have the same shape as our API. For many of our APIs we return Hollow objects directly or objects composed into calculated values at runtime and have them serialize directly in Spring MVC responses.

DanielThomas avatar Feb 25 '19 21:02 DanielThomas

A general concern i have is the Jackson JSON serializer is operating by reflecting over the generated API classes. This is likely to be fragile. The generated API is a representation on the schema (as are the POJOs on the producing side) and that representation could change.

It is the schema + data that should ideally be operated on. For example, see HollowRecordJsonStringifier. Is there a reason that class cannot be used to produce JSON?

PaulSandoz avatar Feb 25 '19 21:02 PaulSandoz