I'm using the Json.Net Newtonsoft component throughout my project at work.
From time to time I stumble into the implementation details of the library. For example when I'm fetching the JProperty's value, I'm forced to use the .Value property:
var displaySettings = options.DisplaySettings.Value;
This is annoying, as one has to remember that the properties of given dynamic object can be really retrieved by "Value" property, without really knowing its type at runtime. (And additionally, there is more linq code to Select the "Value", which clutters the code)
Is there a way to easily wrap the Json object or maybe use the Newtonsoft component more proper way?
I see I'm getting down-voted, but I think there is a problem with JObject encapsulation.
If I use "var" to store the bool variable, the test would fail, because the variable will contain JValue object.
[Test]
public void TestProperties()
{
dynamic testee = JsonConvert.DeserializeObject(@"
{
TestBool:true
}
");
var result = testee.TestBool;
Assert.That(result, Is.EqualTo(true));
}
This test will pass:
Assert.That(result.Value, Is.EqualTo(true));
The test will result in:
Expected: True But was:
Also, to prove that this isn't NUnit issue:
dynamic testee = JsonConvert.DeserializeObject(@"
{
TestBool:true
}
");
var result = testee.TestBool;
if (result)
{
Assert.Pass();
return;
}
The above will throw exception on "if" clause.
I wonder if it's a common practice on stackoverflow to down-vote people without asking for clarification. Nice culture. (I can take, that some of the people new to given technology would be discouraged to ask questions at all)
Thanks, AD