DynamicObject, JSON and the near future

    In this article I want to introduce you to a small application for working with JSON data, demonstrating the features available to us in .NET 4.0. JSON-format issues, as well as work with dynamic data types will be examined superficially.

    Json



    There is such a cool joke known to the masses:

    3763158824_e2f57810c4 [1]JSON (JavaScript Object Notation) is a simple data exchange format that is convenient for reading and writing by both a person and a computer. It is based on a subset of the JavaScript programming language defined in the ECMA-262 3rd Edition - December 1999 standard . JSON is a text format that is completely independent of the implementation language, but it uses conventions familiar to programmers of C-like languages ​​such as C, C ++, C #, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data exchange language. Further…

    JSON and your application



    Well, usually, when receiving objects by JSON in our applications, we must prepare the infrastructure for their support (for example, using the DataContractJsonSerializer and a solution like this ). However, this takes considerable time from the developer. In this regard, I had a passionate desire to put JSON mechanisms on the rails of the dynamic capabilities of .NET 4.0 and get pleasure from working with it;)

    Dynamicobject



    DynamicObject - Provides us with a simple class, inheriting from which we can get the dynamic behavior of the object at runtime. Inheriting from this class and redefining some of its methods, all the basic logic that we need for this is implemented.

    If DLR hosting and other charms are of interest to you and you want to get a little deeper with the question, you can see our presentation here , as well as slides ( here and here ).

    To development



    In order not to experience inconvenience when working with JSON, I suggest using (merging and referencing) a solution from James Newton called JSON.NET , this project is free and meets all the basic requirements for working with JSON within the framework of the .NET stack (including including LINQ).

    Yes, besides, we need an IDE that can work with .NET 4.0b1, for example, Visual Studio 2010 Beta 1 (by the way, #develop is not far behind ).


    We create our application, which will look something like this:
    string input = @"{CPU: 'Intel', Drives: ['DVD read/writer',
    ""500 gigabyte hard drive"" ]}";

    dynamic computer = new DynamicJSON(input);

    * This source code was highlighted with Source Code Highlighter.

    And try to see what properties are found on our computer:

    >> computer.CPU
    "Intel"

    >> computer.Drives
    [
    "DVD read/writer",
    "500 gigabyte hard drive"
    ]

    >> computer.Drives[0]
    "DVD read/writer"

    * This source code was highlighted with Source Code Highlighter.

    So far, nothing unusual, given that we do not know what DynamicJSON is, which we will turn to for the implementation of impressions:

    using Newtonsoft.Json.Linq;

    public class DynamicJSON : DynamicObject
    {
    private JObject _data;

    public DynamicJSON(string data)
    {
    _data = JObject.Parse(data);
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
    result = _data[binder.Name];
    return true;
    }
    }

    * This source code was highlighted with Source Code Highlighter.

    And this is all that is required of us for the work of this example, I am overly pleased with such things, and therefore I recommend that you also use similar practice in your decisions.
    If someone is interested in the topic of the upcoming DLR, I can tell you more deeply, leave feedback

    Also popular now: