I am trying to use Json.NET to parse a file of comma-separated JSON objects:
{
JSON ...
},
{
JSON ...
},
{
JSON ...
}
The code below works fine if the stream contains no separators (i.e., commas above removed). However, commas produce an infinite loop where Json.NET keeps reading an "Undefined" token even after end of file was reached:
using (StreamReader fReader = File.OpenText("filename.json"))
using (JsonTextReader jReader = new JsonTextReader(fReader))
{
jReader.SupportMultipleContent = true;
while (jReader.Read())
{
var jToken = JToken.ReadFrom(jReader);
if (jToken is JObject)
Console.WriteLine("JSON object: " + ((JObject)jToken).ToString());
}
}
I tried skipping the comma by reading ahead and using JsonTextReader's Skip() method, but this doesn't work: JsonTextReader apparently buffers ahead, eating up the comma, which gives it indigestion.
It's hard to believe that I'd be the first to run into this problem, but despite searching here for a good bit, I haven't found any relevant posts (at least for C# and Json.NET). Is it really necessary to hack this up from scratch?
ETA: As per Brian Rogers' comment below, Json.NET 11.0.1 and above handle comma-separated JSON, so the above works fine now, commas or not.