Bunny83 / SimpleJSON

A simple JSON parser in C#
MIT License
735 stars 294 forks source link

Strings should be without quotes #30

Closed mrpuzzler closed 4 years ago

mrpuzzler commented 4 years ago

Parsing array from file. All strings are as quoted strings. Should be without quotes.

mrpuzzler commented 4 years ago

Looks like something goes wrong on my end. Sorry :)

Bunny83 commented 4 years ago

Note that JSONNode.ToString() will always convert that node back to json. If it's a JSONString it will be just the "quoted" string. To actually read the value you can do

string Out = (string)Test["array"][0]; // explicit type conversion operator

// or

string Out = Test["array"][0].Value; // read the string value of the node.

Yes, the "Value" property should be better named "AsString" like the other type conversion properties. However it has "historical reasons" why it's still this way -.-. The implicit and explicit type conversion came much later. So lines like those:

someNode["foo"] = 5;
someNode["bar"] = "test";

become, thanks to implicit type conversion operators, this under the hood:

someNode["foo"] = new JSONNumber(5);
someNode["bar"] = new JSONString("test");

This does work in reverse, but unfortunately not for strings since the default behaviour of C# is using ToString() of an object, even those operators exists. But that's why the explicit cast actually works as expected.

Bunny83 commented 4 years ago

Hmm, actually I think my last statement is actually wrong ^^. C# will use the implicit type conversion over ToString(). So this string Out = Test["array"][0]; should actually work as far as I can tell -.-