Suppose I have an input stream coming in that is a sequence of objects, like so:
{ "value" : 1}
{ "valueFromOtherType" : 2}
{ "value" : 3}
{ "value" : 4}
How can I deserialize these objects in C#? Using System.Text.Json is preferred, Newtonsoft is fine. It would be straightforward if the data was wrapped in brackets, I could just deserialize into an array. But I don't have control on the incoming data stream.
Using System.Text.Json I could use JsonSerializer.DeserializeAsync, but only after I create my only stream class to wrap the existing data stream with brackets as it comes in. It'd still be a bit undesirable because I have to wait for the entire (synthetic) array to be read before I can look at the first element.
JsonTextReaderwith theSupportMultipleContentflag set to true. See stackoverflow.com/q/38429059/10263. Note the example in the linked question uses aStringReader, but you can substitute aStreamReaderinstead.System.Text.Jsonperhaps this answer by Nathan to Line delimited json serializing and de-serializing does what you need. That question also has an answer for Json.NET so it might be a true duplicate.