LINQ to JSON

Michael Schwarz on Tuesday, February 12, 2008

James has published a first beta of LINQ to JSON [1] support in his Json.NET library [2]. It looks very similar to my LINQ support in an internal build of Ajax.NET Professional [3] which never has been released.

<pre style="background-color:White;;overflow: auto;"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/

--><span style="color: #000000;">List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Post</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> posts </span><span style="color: #000000;">=</span><span style="color: #000000;"> GetPosts();

JObject rss </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JObject( </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JProperty(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">channel</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JObject( </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JProperty(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">title</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">James Newton-King</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">), </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JProperty(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">link</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">http://james.newtonking.com</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">), </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JProperty(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">description</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">James Newton-King's blog.</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">), </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JProperty(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">item</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JArray( from p </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> posts orderby p.Title select </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JObject( </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JProperty(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">title</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, p.Title), </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JProperty(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">description</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, p.Description), </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JProperty(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">link</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, p.Link), </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JProperty(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">category</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JArray( from c </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> p.Categories select </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> JValue(c))))))))); </span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com -->

The C# example above will generate following JSON output:

<pre style="background-color:White;;overflow: auto;"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/

--><span style="color: #000000;">{ </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">channel</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">: { </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">title</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">: </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">James Newton-King</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">link</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">: </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">http://james.newtonking.com</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">description</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">: </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">James Newton-King's blog.</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">item</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">: [ { </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">title</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">: </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">Json.NET 1.3 + New license + Now on CodePlex</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">description</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">: </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">Annoucing the release of Json.NET 1.3,...</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">link</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">: </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">http://james.newtonking.com/projects/json-net.aspx</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">category</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">: [ </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">Json.NET</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">CodePlex</span><span style="color: #000000;">&quot;</span><span style="color: #000000;"> ] }, { </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">title</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">: </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">LINQ to JSON beta</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">description</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">: </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">Annoucing LINQ to JSON</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">link</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">: </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">http://james.newtonking.com/projects/json-net.aspx</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">category</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">: [ </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">Json.NET</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">LINQ</span><span style="color: #000000;">&quot;</span><span style="color: #000000;"> ] } ] } }</span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com -->