Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Added new annotation
@JsonUnboxed
to indicate that class should be (de)serialized as it's underlying field.It can only be used on value classes. This draft atm contains only Java implementation to raise a discussion. Kotlin support is to be added to this PR.
Reasoning
The original intention was to add implicit conversion of Kotlin's
value class
to/from JSON as it's field value. But after taking a closer look atvalue class
I realised that it has all the properties of regularclass
, and implicit conversion is not an option.Then I looked it up and found similar approach in Rust's Serde, where this conversion is made explicit by adding
transparent
toserde
attribute.Naming
I've picked
Unboxed
as this feature reminded me Java's Autoboxing and Unboxing.Unsolved problems
By placing
@JsonReader
on single parameter constructor and by marking all the fields except one with@JsonSkip
it is possible to use@JsonUnboxed
with regular classes. Is it good? Is it bad? I've added tests for this case.It is possible to restrict usage of this annotation only to single field records, for example. Discussion is needed.
Similar technologies in other frameworks/languages
Jackson
@JsonValue
annotation can be placed on a class' field to serialize an entire object using this single field.@JsonValue
can be placed on getter methods too.BUT there's a problem with deserialization. The following code
Will output:
This is because from-value deserialization is used by default, and from-object is explicitly enabled by adding
@JsonCreator
on constructor and@JsonProperty
onString myValue
parameter. This is very confusing and inconsistent.NOTE: We can create
@JsonValue
annotation in Kora but make corresponding behavior consistent between reading and writing JSON.Gson
TODO
Rust serde
https://serde.rs/container-attrs.html#transparent
#[serde(transparent)]
Serialize and deserialize a newtype struct or a braced struct with one field exactly the same as if its one field were serialized and deserialized by itself.