Home .NET DynamicObject,JSONand near future

DynamicObject,JSONand near future

by admin

In this article I want to introduce you with a small application to work with JSONdata, demonstrating the features available to us in .NET 4.0. The JSON format and working with dynamic data types will be superficially discussed.


There’s a great joke known to the masses:
DynamicObject,JSONand near future
(JavaScript Object Notation) is a simple data exchange format, easy to read and write for both humans and computers. It is based on a subset of JavaScript programming language defined in ECMA-262 3rd Edition – December 1999 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

So, normally, when we receive JSON objects in our applications we have to prepare an infrastructure to support them (for example with the DataContractJsonSerializer and solutions like of this ).However, it takes a significant amount of time from the developer. Because of this, I have a burning 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 – Provides us with a simple class, by inheriting from which we can get the dynamic behavior of an objectat runtime. By inheriting from this classand overriding some of its methods, all the basic logic we need to do this is implemented.
If you are interested in DLR hosting and other delights and want to know a bit more aboutthe subject, you can take a look at our performance here as well as slides ( here and here ).

By development

In order not to be inconvenienced by JSON, I suggest using(merge and reference) the solution from James Newton titled JSON.NET , this project is free and satisfies all the basic requirements for working with JSON within the .NET stack (including LINQ).

Yes, and we need an IDE that supports .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 try to see what properties our computer has :

> > computer.CPU
> > computer.Drives
"DVD read/writer" ,
"500 gigabyte hard drive"
> > computer.Drives[0]
"DVD read/writer"
* This source code was highlighted with Source Code Highlighter

Nothing unusual so far, considering that we don’t know what DynamicJSON is all about, which implementation we will 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 that’s all it takes from us to make this example work, I’m overjoyed by this kind of thing, so I recommend that you use this kind of practice in your solutions as well.
If anyone is interested in the upcoming DLR, I can talk more in depth, leave feedback

You may also like