JSON constructs can be created directly through the use of the implicit cast operators:

JsonValue jsonBool = false;
JsonValue jsonNum = 42;
JsonValue jsonString = "aString";
JsonValue jsonObject = new JsonObject{{"aKey", 42}};
JsonValue jsonArray = new JsonArray{4, true, "aValue"};

The above code creates five JsonValue instances. To access these values, use these properties:


If a get accessor is used that does not correspond with the JsonValue’s type, an exception is thrown. The default constructor for JsonValue creates a Null value.

These declarations can be combined in the same way as when declaring any other object. For example, a moderately complex JsonObject can be built as follows:

var json = new JsonObject
        {"boolean", true},
        {"number", 42},
        {"string", "a string"},
        {"null", JsonValue.Null},
        {"array", new JsonArray {6.7, true, "a value"}},
        {"object", new JsonObject
                {"aKey", 42},
                {"anotherKey", false}

The object’s structure, and ultimately its output, is quite apparent directly from the code that created it.

Since JsonObject and JsonArray are implemented as strongly typed collections, all of the underlying operations (e.g. Add(), AddRange(), etc.) are accessible, including LINQ. As such, the following statements are valid:

json.Add("newItem", "a new string");
var onlyStrings = json.Select(jkv => jkv.Value.Type == JsonValueType.String).ToJson();

Here, the ToJson() method is an extension method on the IEnumerable<KeyValuePair<string, JsonValue>> type returned by the LINQ Select() method. It returns a JsonObject. There is also a correlating ToJson() method for the IEnumerable<JsonValue> which returns a JsonArray.

As you can see, creating these constructs is quite easy and very readable. As is expected, calling json.ToString() yields:

{"boolean":true,"number":42,"string":"a string","null":null,"array":[6.7,true,"a value"],"object",{"aKey":42,"anotherKey":false}}

Furthermore, feeding this output back into the JsonObject constructor yields the original structure (although using new instances).

Home | Architecture | Usage | Serialization | Schema | XML Conversion | Support


Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.