Home
Insert
Update snippet 'Lightweight syntax for creating JSon objects using Newtonsoft's Json.Net'
Title
Passcode
Description
There's an easy way to create JSon objects using Newtonsoft's Json.Net. The common problem with Json.Net is that there's usualy a lot of overhead when creating Json with it as there're a lot of 'new', parentheses, nested objects when we don't need them. And it becomes annoying quite fast. DU Json and toJson function allow to create Json tree and convert it to Json.Net JObject hierarchy. On the examples the usage of lightweight syntax doesn't give a lot of win but it will be more clearer when it come to more complicated objects.
Source code
#if INTERACTIVE #r @"Newtonsoft.Json.dll" #endif open Newtonsoft.Json.Linq type Json = | JObj of Json seq | JProp of string * Json | JArr of Json seq | JVal of obj let (!!) (o: obj) = JVal o let rec toJson = function | JVal v -> new JValue(v) :> JToken | JProp(name, (JProp(_) as v)) -> new JProperty(name, new JObject(toJson v)) :> JToken | JProp(name, v) -> new JProperty(name, toJson v) :> JToken | JArr items -> new JArray(items |> Seq.map toJson) :> JToken | JObj props -> new JObject(props |> Seq.map toJson) :> JToken // Suppose we want to create the following Json object: // { // "id": 123, // "props": { // "prop": [ // { "id": 1, v: 123 }, // { "id": 2, v: 456 } // ] // } // } // // Then the simplified lightweight Json creation will look like this: let j = JObj [ JProp("id", !! "123"); JProp( "props", JProp( "prop", JArr [ JObj [JProp("id", !! 1); JProp("v", !! 123)]; JObj [JProp("id", !! 2); JProp("v", !! 456)] ])) ] let json = toJson j // Compare it to pure Json.Net. let jj = new JObject([ new JProperty("id", "123"); new JProperty( "props", new JObject( new JProperty( "prop", new JArray([ new JObject([new JProperty("id", 1); new JProperty("v", 123)]); new JObject([new JProperty("id", 2); new JProperty("v", 456)]) ])))) ])
Tags
json
json
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update