Support decoding partial streams
See original GitHub issueThe following test:
@Serializable
class SimpleObject(val name: String)
@Test
fun testDecode() {
val inputStream = """
{
"name": "Object1"
}
{
"name": "Object2"
}
""".trimIndent().byteInputStream()
println("1")
println(Json.decodeFromStream<SimpleObject>(inputStream).name)
println("2")
println(Json.decodeFromStream<SimpleObject>(inputStream).name)
}
fails with:
Unexpected JSON token at offset 25: Expected EOF after parsing, but had { instead
JSON input: kotlinx.serialization.json.internal.ArrayAsSequence@2c4ca0f9
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 25: Expected EOF after parsing, but had { instead
JSON input: kotlinx.serialization.json.internal.ArrayAsSequence@2c4ca0f9
at app//kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24)
at app//kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:32)
at app//kotlinx.serialization.json.internal.AbstractJsonLexer.fail(AbstractJsonLexer.kt:524)
at app//kotlinx.serialization.json.internal.AbstractJsonLexer.fail$default(AbstractJsonLexer.kt:523)
at app//kotlinx.serialization.json.internal.AbstractJsonLexer.expectEof(AbstractJsonLexer.kt:163)
at app//kotlinx.serialization.json.JvmStreamsKt.decodeFromStream(JvmStreams.kt:66)
Is there a specific reason to mandate a trailing EOF? Being able to parse partial streams would be nice. For an example to extract a Json from a markdown file or to read an array of objects without comas like the example above.
PS: there could be a decodeFromStreamFully()
that also closes the inputStream
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Show authors - Springer Link
Partial decoding is an effective way to improve the processing efficiency in many video applications such as video retrieval and mobile video display....
Read more >Request full-text - ResearchGate
In this paper, one partial decoding scheme is presented, in which user can specify the region of interest(ROI) to be decoded. In particular,...
Read more >opusfile: Decoding - Opus Codec
These functions retrieve actual decoded audio data from the stream. The general functions, op_read() and op_read_float() return 16-bit or floating-point output, ...
Read more >NNET2 online decoding PartialTraceback function
Hi all,. I found out that the PartialTraceback function is removed from NNET2 decoding. In the old GMM-HMM decoder online-gmm-decoder-faster.
Read more >Using Coding Services - OSS Nokalva
Partial decoding is not supported for SET OF or SEQUENCE OF with the ValueInFile directive applied. If a decoding error occurs in the...
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
That’s something we’re planning to work on
The key reason here is to
InputStream
being the wrong abstraction.In order for JSON decoding to be efficient (or, at least, to be not terrible), IS has to be read in a buffered manner, otherwise, all the decoding does is round-tripping between
InputStream.read()
calls byte-by-byte.As soon as one starts buffering, there is no way out – when the JSON object is read, we cannot leave the stream as is, because we could’ve read the piece of the next object that will be lost for good. IS neither provides an API “read to this buffer without consuming it + commit the offset” nor “return this N bytes back to the stream”.
Taking this into account, we basically have three options: