Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json.Net boolean parsing issue

Tags:

json

json.net

JObject.Parse(jsonString) is causing issue for boolean data. e.g. The json is :

{
    "BoolParam": true
}

I used the following code to parse:

JObject data = JObject.Parse(str1);
foreach (var x in data)
{
  string name = x.Key;
  Console.Write(name + " (");
  JToken value = x.Value;
  Console.Write(value.Type + ")\n");
  Console.WriteLine(value);
}

This print out the value as : BoolParam (Boolean) : True

The case sensitivity causes issue as I save this json for later use. The saved json looks like

{
    "BoolParam": True
}

However, when i later use it, the JObject.Parse(str) throws error as invalid Json :Unexpected character encountered while parsing value: T. Path 'BoolParam', line 2, position 15.

If I change the case from "True" to "true", it works. I dont want to add this hack to change the case when saving but is there a better way to handle this scenario.

like image 767
surya Avatar asked Sep 25 '13 16:09

surya


1 Answers

I dont want to add this hack to change the case when saving but is there a better way to handle this scenario.

No, you have to produce valid JSON when saving if you want to be able to later deserialize it with a JSON serializer such as Newtonsoft JSON. So fixing your saving routing is the right way to go here.

like image 81
Darin Dimitrov Avatar answered Sep 18 '22 07:09

Darin Dimitrov