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. Issues of JSON-format will be considered superficially, as well as work with dynamic data types.
Json
There is such a cool joke, known to the masses:
JSON (JavaScript Object Notation) is a simple data exchange format that is easy to read and write by both humans and computers. It is based on a subset
of the JavaScript programming language defined in
ECMA-262 3rd Edition - December 1999 . JSON is a text format completely independent of the implementation language, but it uses conventions familiar to programmers in C-like languages such as C, C ++, C #, Java, JavaScript, Perl, Python, and many others. These properties make JSON the ideal language for exchanging data.
Further…')
JSON and your application
Well, usually, when receiving JSON objects in our applications, we need to prepare the infrastructure to support them (for example, using the
DataContractJsonSerializer and deciding on
this ). However, it 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 one pleasure from working with it;)
DynamicObject
DynamicObject - Provides us with a simple class, inheriting from which we can get the dynamic behavior of an object at the execution stage. Inheriting from this class and redefining some of its methods, we implement all the basic logic we need for this.
If the hosting of DLR and other delights interested you and you want to familiarize yourself a little deeper with the question, then you can watch
our presentation
here , as well as slides (
here and
here ).
To develop
In order not to experience inconvenience when working with JSON, I propose to use (merge and reference) a solution from
James Newton called
JSON.NET , this project is free and meets all the basic requirements for working with JSON within the .NET stack (including including LINQ).
Yes, besides, we will 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 ).
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 we 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, the implementation of which we turn to for 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 from us for the work of this example. I like such things excessively, and therefore I recommend that you use this practice in your decisions.
If someone is interested in the topic of the upcoming DLR, I can tell you more deeply, leave feedback