question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Json decoding using non JsonOutput

See original GitHub issue

For 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:20 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
qwwdfsadcommented, Aug 17, 2020

Closing as won’t fix

0reactions
qwwdfsadcommented, Mar 12, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found