Open logicle opened 9 years ago
Still digging.
I believe I'm having a similar issue. I'd like to have two blocks of JSON stored within the same file. The first is read by LitJson, but then I need to use an API that uses its own built-in JSON reader.
I agree - it seems like if streams are supported at all (rather than pure strings), then LitJson ought to leave the stream in the right state.
Here's a simple example of what I'm trying to do:
// Pass in two pieces of concatenated JSON to a StringReader
var stringReader = new StringReader("{}{}");
// Try to read the first JSON object
var obj = LitJson.JsonMapper.ToObject(stringReader);
// Now we want the second block: "{}"
// But this returns "}" since LitJson has already stepped beyond
// what it's supposed to be interested in.
var secondJsonObjText = stringReader.ReadToEnd();
When LitJson creates a JsonReader from a NetworkStream, it will continue to attempt to read even after a complete Json object has been parsed.
This read will block and not return the Json object.
The block happens in the lexer (which is correct, it should if it is in the middle of parsing an object until the rest of the object arrives on the stream):
Here is block of code that demonstrates the bug:
The expected behavior is that JsonMapper.ToObject (and all variants), will return when a complete Json object has been parsed from the stream. If the stream has fragments of another message not yet complete, JsonMapper.ToObject should first return the complete message, then in the next iteration, block on the stream until the rest of the next message arrives and return that object, etc...
Here is a complete c# program that can be used to reproduce and debug the problem:
I would not be surprised it this is actually user error, but this seems like a reasonable usage of passing a TextStream to JsonWrapper/JsonReader.
I will dig through the source to see if there is a sane way to support the expected behavior.