-
Notifications
You must be signed in to change notification settings - Fork 637
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
Support decoding partial streams #1789
Comments
Behaviour of You can try to use |
Any reason
Yup, that'd work for the use case above, thanks 👍 . |
Most of the frameworks don't do that either. The general agreement is that it is the stream creator's responsibility to close it unless there are some strong objections. |
I agree it's fine to have the caller close the stream. My point is that if it's the case, it feels weird enforcing a trailing EOF. For an example, Moshi behaves differently for The following code works and outputs both objects: @Test
fun deserialize() {
val value = """
{
"name": "Object1"
}
{
"name": "Object2"
}
""".trimIndent()
val buffer = Buffer().writeUtf8(value)
val adapter = Moshi.Builder().build().adapter(SimpleObject::class.java)
println("1")
println(adapter.fromJson(buffer))
println("2")
println(adapter.fromJson(buffer))
} |
The key reason here is to 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 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:
|
Aaaaah right, good call 👍
Can we have a 4th option that takes a |
That's something we're planning to work on |
The following test:
fails with:
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 inputStreamThe text was updated successfully, but these errors were encountered: