0

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

1
  • You can define strongly-typed classes and deserialize into those instead of using dynamic. Commented Dec 14, 2016 at 20:27

2 Answers 2

1

Deserialize into C# classes.

class Program
{
    static void Main(string[] args)
    {
        string json = @"{ ""FirstName"":""Bob"" }";

        Person person = Newtonsoft.Json.JsonConvert.DeserializeObject<Person>(json);

        Console.WriteLine(person.FirstName);
    }

    public class Person
    {
        public string FirstName { get; set; }
    }
}

http://www.newtonsoft.com/json/help/html/deserializeobject.htm

Sign up to request clarification or add additional context in comments.

Comments

0

You have many options to handle the dynamic values with Json.Net

dynamic jobj = JObject.FromObject(new { DisplaySettings = "aaa" });

var ds1 = jobj.DisplaySettings; //ds1 is JValue
var ds2 = jobj.DisplaySettings.Value; //ds2 is object (boxed string)
var ds3 = (string)jobj.DisplaySettings; //ds3 is string (explicit casting)
string ds4 = jobj.DisplaySettings; //ds4 is string (implicit casting)

1 Comment

The cast for ds3 fails. I've been debugging.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.