-
Notifications
You must be signed in to change notification settings - Fork 636
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
Add JsonDecoder.peekToken() ? #2223
Comments
Would love to see a |
Hi @martinbonnin, I have exactly the same case as you, could you please share how did you manage to decode it with the current version of kotlinx.serialization? Thanks ;) |
@venator85 sorry for the late reply, I don't have all the context anymore but I probably ended up decoding everything to a |
+1 on the solution @martinbonnin mentioned. It allows me to consume a section from the Json, and manually parse or "peek". |
This would be amazing! I have a JSON document that needs to be parsed. But depending on the first token, whether that is an object or an array, it needs to choose between two different serializers. Currently I have to parse the entire document using |
Just found out there is actually an internal API for this, I guess this will do for now. @Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
run {
decoder as kotlinx.serialization.json.internal.StreamingJsonDecoder
when (decoder.lexer.peekNextToken()) {
kotlinx.serialization.json.internal.TC_BEGIN_OBJ -> {
}
else -> {}
}
} |
The ability to parse Json or other formats token by token would be huge for low memory devices. I have some large files that don't fit entirely into memory. This would make parsing them possible. |
@hansenji The problem is how you are going to deal with partially decoded values. It almost only make sense for collections, but at what level of the hierarchy? (top only?). |
Note that |
@pdvrieze Ideally the solution would be a complete |
Btw. I implemented this as a separate library: https://github.com/fab1an/kotlin-json-stream |
Some backends return polymorphic data without a descriptor:
vs
I know this is quite the edge case but I haven't found a way to decode this without going through
JsonElement
and buffering everything.Would it be possible to introduce
jsonDecoder.peekToken()
:This way, users that know they are in a JSON context could do stuff like this:
The text was updated successfully, but these errors were encountered: