JamesNK/Newtonsoft.Json

Deserializing JSON with SupportMultipleContent requires an extra Read() operation

WelliSolutions opened this issue · 1 comments

Steps to reproduce

using System.IO;
using Newtonsoft.Json;

string json = "{id:\"1\"}{id:\"2\"}";
using var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json));
var serializer = new JsonSerializer();
using var sr =new StreamReader(stream);
using var jsonTextReader = new JsonTextReader(sr);
jsonTextReader.SupportMultipleContent = true;
var obj1 = serializer.Deserialize(jsonTextReader);
//jsonTextReader.Read();
var obj2 = serializer.Deserialize(jsonTextReader);

Expected behavior

The code works as is. It reads the first object, then the second object

Actual behavior

It throws a JsonSerializationException.

Unexpected token while deserializing object: EndObject. Path '', line 1, position 43.

In order to work around it, uncomment the line jsonTextReader.Read();.

Using JsontReader.Read() here is the intended behavior, which is also documented: https://www.newtonsoft.com/json/help/html/ReadMultipleContentWithJsonReader.htm