What is the correct way to use JSON.NET to parse a stream of JSON objects?

I have a JSON object stream that looks something like this:

{...}{...}{...}{...}... 

Thus, basically a combined list of JSON objects without a separator. How can I deserialize them to IEnumerable<T> using JSON.NET? At the moment I tried something like

 var serializer = new JsonSerializer(); serializer.CheckAdditionalContent = false; using (var reader = new StreamReader(stream)) using (var jsonReader = new JsonTextReader(reader)) { reader.SupportMultipleContent = true; reader.Read(); while (reader.TokenType != JsonToken.None) { yield return serializer.Deserialize<TResult>(reader); } } 

But this fails with

 Newtonsoft.Json.JsonSerializationException: Unexpected token while deserializing object: EndObject. Path '', line 1, position 55. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader) 

Obviously, I need to move the reader after calling Deserialize , but how do I do this?

+12
source share
1 answer

I think if you change your loop a little, everything should work:

 public IEnumerable<TResult> ReadJson<TResult>(Stream stream) { var serializer = new JsonSerializer(); using (var reader = new StreamReader(stream)) using (var jsonReader = new JsonTextReader(reader)) { jsonReader.SupportMultipleContent = true; while (jsonReader.Read()) { yield return serializer.Deserialize<TResult>(jsonReader); } } } 

Note that you must iterate over IEnumerable<TResult> while the Stream passed to this method is open:

 using (var stream = /* some stream */) { IEnumerable<MyClass> result = ReadJson<MyClass>(stream); foreach (var item in result) { Console.WriteLine(item.MyProperty); } } 

Example: https://dotnetfiddle.net/Y2FLuK

+12
source

Source: https://habr.com/ru/post/1502206/


All Articles