Json decoding using non JsonOutput
See original GitHub issueFor now, it is impossible to decode JsonElement
from decoder inside of serializer if that decoder
was not created by Json
. Example:
object : KSerializer<Any> {
override val descriptor = StringDescriptor.withName("Example")
override fun deserialize(decoder: Decoder) {
val json = JsonElementSerializer.deserialize(decoder)
}
override serialize(encoder: Encoder, obj: Any) {
TODO()
}
}
Object will throw exception in deserialize
method each time when this serializer will not be used with Json formatter.
Is it possible to solve this problem?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:20 (11 by maintainers)
Top Results From Across the Web
Processing JSON
JsonOutput is responsible for serialising Groovy objects into JSON strings. It can be seen as companion object to JsonSlurper, being a JSON ......
Read more >json — JSON encoder and decoder — Python 3.11.1 ...
If indent is a non-negative integer or string, then JSON array elements and object members will be pretty-printed with that indent level.
Read more >Working with JSON in Groovy - Baeldung
To convert an instance of that class to a JSON String, we need to use the JsonOutput class and make a call to...
Read more >Encoding and decoding JSON in Swift - Ralf Ebert
Using the Codable protocol and JSONDecoder/JSONEncoder, JSON formats can be parsed and generated in Swift.
Read more >Parsing JSON output in Robot framework [TypeError
*** Settings *** *** Variables *** ${our_json} { "output": { "ParentKey": { "key": "9b92e663a66c0cc1", "id": "uid=26" } } } *** Test Case ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Closing as won’t fix
Yes, and values of these objects can still be either primitives or nested objects. I’ve already described it above (https://github.com/Kotlin/kotlinx.serialization/issues/615#issuecomment-564563279) why it is impossible to do with non json-specific decoder and why it works for json-specific ones.
It is not the matter of “we don’t want to allow to deserialize JsonElement with a non-JsonInput decoder”, it is just impossible for an arbitrary
JsonElement
.Non-polymorphic serialization of JsonElement with a custom decoder is, though, possible, but then it will be impossible to deserialize it back. But such serialization doesn’t follow the principle of least surprise and, thus, is inappropriate.
Polymorphic serialization is possible (and enables deserialization by an arbitrary decoder), but produces a really weird format that definitely is not an expected JSON and also doesn’t follow PoLA. Moreover, if an unprepared user will just wrap a
JsonInput
into any kind of delegation, his JSON, from a pretty{"key": "value", "key2", null}
representation will become["kotlinx.serialization.json.JsonObject", {"key": ["kotlinx.serialization.json.JsonPrimitive", "value"], "key2": ["kotlinx.serialization.json.JsonNull", "Unit"]}]
. Also a no-go