C# Newtonsoft.JSON library works very well in most situation but there is no easy way to read concatenated JSON like one below:
{"some":"thing"}{"some":"other thing"}{"some":"third thing"}
This code that pretty straightforward but it throws exception if I try to deserialize more than one object:
using (var reader = new StreamReader(File.Open("data.txt", FileMode.Open)))
{
using (var jr = new JsonTextReader(reader))
{
var data1 = js.Deserialize<Data>(jr));
var data2 = js.Deserialize<Data>(jr)); // <--- Exception is thrown here
}
}
There are couple workarounds. First one is reformatting the whole list of objects into a JSON array. Such approach works fine for small amounts of data but if file does not fit in memory then situation becomes quite complicated.
The other workaround is splitting whole text into separate JSON objects and parsing one object at a time. This solution would handle big amounts of data but implementation is a bit more complex since it requires some sort of JSON parsing.
Is there an easier way to read JSON objects in such concatenated JSON file?