1

I am trying to parse some JSON objects which is made just of (string,string) pairs, in order to emulate Resjson behaviour. The file I am parsing contains this.

{
   "greeting":"Hello world",
   "_greeting.comment":"Hello comment.",
   "_greeting.source":"Original Hello",
}

Please note the last comma is incorrect, and I also used http://jsonlint.com/ to test JSON syntax. It tells me it is incorrect, as I expected. My - slightly modified - code is :

string path = @"d:\resjson\example.resjson";
string jsonText = File.ReadAllText(path);

IDictionary<string, string> dict;
try
{
    dict = JsonConvert.DeserializeObject<IDictionary<string, string>>(jsonText);
}
catch(Exception ex)
{
    // code never reaches here
}

My above code returns the IDictionary with the 3 keys as if the formatting was correct. If I serialize back, the string obtained is without the last comma.

My questions are :

  • Is Newtonsoft.Json so permissive that it allows users slight errors ?
  • If so, can I set the permissiveness so that it is more strict ?
  • Is there a way to check if a string is valid JSON format, using Newtonsoft.Json with and/or without the permissiveness?
4

0 回答 0