micronaut-serialization icon indicating copy to clipboard operation
micronaut-serialization copied to clipboard

Ability to serialize / deserialize boolean properties as numeric values in json

Open cutz opened this issue 1 year ago • 5 comments

Feature description

I have a boolean property that I would like to serialize/deserialize to json as a numeric value 0 or 1. I believe this typically could be done by annotating the property with @JsonFormat(shape=JsonFormat.Shape.Number). As documented micronaut doesn't not support the shape property for JsonFormat and attempts to use it result in the following error:

Annotation @JsonFormat specifies attribute 'shape'. Currently supported attributes include: [timezone, pattern, locale, lenient]

Are there any plans to support this?

cutz avatar Aug 19 '24 16:08 cutz

probably doable yeah

graemerocher avatar Aug 19 '24 18:08 graemerocher

I don't have a feel for how big of an undertaking this would be. If this seems like something that would be a good starter task, and someone is willing to point me in the right direction, I'd be willing to take a stab at a solution.

cutz avatar Aug 19 '24 18:08 cutz

  1. step 1 is disable the compile time check in JsonFormatMapper by modifying getSupportedMemberNames
  2. Then I guess BooleanSerde would need to be modified to override createSpecific for serialization / deserialization with a specific customised serialised to handle the shape

graemerocher avatar Aug 22 '24 13:08 graemerocher

The other solution would be detecting it in the compilation time and and setting a custom serializer/deserializer interface ShapeDeserializer / interface ShapeSerializer + add implementation which would use createSpecific and some logic to do the convertion

dstepanov avatar Aug 22 '24 13:08 dstepanov

that would likely be more efficient yes

graemerocher avatar Aug 22 '24 16:08 graemerocher