-
Notifications
You must be signed in to change notification settings - Fork 626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialize string property as json object #2072
Comments
Well, I could make it work, but in not very nice way. Maybe there's a nicer option? And if there's not, it would be cool to have something like in GSON (example above). That's how I made it work: override fun transformSerialize(element: JsonElement): JsonElement {
return Json.decodeFromString<JsonObject>(
element.toString()
.replace("\\", "")
.removePrefix("\"").removeSuffix("\"")
)
} It looks like JsonElement representing String passed to |
This might be helped by a new feature that is yet to be released #2041, which would allow encoding a string directly, without surrounding it in quotes. |
Yes, I think #2041 in the new release would be a way to go. Alternatively, you can write a basic custom serializer (without using JsonTransformingSerializer) — String should be unquoted here. |
Actually, even with #2041 you still would need to write a basic serializer. |
What is your use-case and why do you need this feature?
I have a class like that
Property
jsonData
for saving json object as string, jsonData can be any json object with different contentsWhen sending class to server, I want to have MyClass2 mapped to json like this:
I can't figure out how to achieve this. I was able to do other way around: deserialize json object field to my class string property. But struggle with serialization.
Describe the solution you'd like
In GSON I used to achieve this by making JsonStringFieldAdapter and annotation on property:
I tried to make something similar with kotlinx.serialization
Deserialization seems to be working, but I can't figure out how to do serialization
The text was updated successfully, but these errors were encountered: