What I try to achieve
Using Jackson for serialization, there is the @JsonRawValue
annotation to achieve that a string value is serialized without quotes.
For instance, with the following code
data class SomeData {
@JsonRawValue
value: String
}
I can serialize SomeData("{}")
and obtain:
{ "value": {} }
(Without the annotation it would be { "value": "{}" }
.)
I want to achieve the same using Kotlin serialization.
Why do I want that
The reason why I want this is that I get an object via Rest, I store it in a DB, later load it from there and return it back without changing the content. It doesn't concern me what is contained in the object. So I absolutely do not need it to be parsed or deserialized in any way. In the worst case the content is altered somewhere in the process. In the best case it is just some superfluous computation.
What I tried
It seems that there is no corresponding mechanism in kotlinx-serialization that supports this out-of-the box.
Looking at the source code, it seems that I would need an instance of class JsonPrimitive
(or JsonLiteral
) with a String
value
, but isString
set to false, but there is no way to get that.
Another approach may be to write a serializer that serializes the String
value without quotes, but I do not know how.