Getting Started
Modelling JSON
The structure of JSON is quite simple. You can read about the language on json.org.
Each of the JSON constructs can be mapped to a .Net construct as follows:
JSON | ↔ | .Net |
---|---|---|
object | ↔ | Dictionary |
array | ↔ | List |
boolean | ↔ | Boolean |
numeric | ↔ | Double |
string | ↔ | String |
This JSON structure is implemented in .Net through three classes: JsonValue
, JsonObject
, and JsonArray
.
The JsonValue
class is a container class for all of the JSON types. In order for everything to remain strongly typed, the class exposes a property for each JSON type. To implement the JSON null value, the static member JsonValue.Null
was created.
The JsonObject
class is implemented as Dictionary<string, JsonValue>
. As such it can be navigated and manipulated as any other dictionary instance would be.
Similarly to the JsonObject
class, the JsonArray
class is implemented as List<JsonValue>
, which can be navigated and manipulated directly as a list instance.
NOTE Because of JsonObject
's and JsonArray
's inheritance structure, they are fully LINQ-compatible!
Each of these three classes override the ToString()
method to correctly output in a JSON format.
Implicit casts from JsonObject
, JsonArray
, string
, double
, and bool
were created in JsonValue
to simplify coding and readability. Also, comparison operators were overridden for the JsonValue
class. You can read more on these casts and operators below.
All of the extended JSON functionality supported by this library has dedicated object models, where applicable.
Working with JSON directly
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:
jsonBool.Boolean
jsonNum.Number
jsonString.String
jsonObject.Object
jsonArray.Array
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. Note also that we're not just building a string value to be parsed; we're actually building the object model, which will be checked at compile time, practically eliminating the occurrence of typographical errors.
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 corresponding 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).
Pretty Output
There are two methods that create output: ToString()
, which simply outputs the most concise JSON (single line, no white spacing), and GetIndentedString()
, which outputs a multiline, indented string.
Handling Errors
While parsing JSON data (through either the JsonObject
constructor or JsonValue.Parse()
), errors may occur. These errors will be reported by throwing a JsonSyntaxException
. This exception exposes a Location
property which contains a path to the error in JSON Pointer syntax. The messaging has been designed to direct the user directly to the error.
Here are a few examples:
JSON | Error | Error Message |
---|---|---|
{"first":4,"int":no} |
no is not a predefined JSON value |
Value not recognized: 'no} '. Path: '$.int' |
["first",4,"int",no] |
no is not a predefined JSON value |
Value not recognized: 'no] '. Path: '$[3]' |
{"first":4,int:"no"} |
int should be a string value: "int" |
Expected key. Path: '$.first' |
As shown, the error message will give information as to what went wrong and where the error occurred. The location is given in JSON Pointer notation. In the last example, the key could not be determined from the input, so it gave the last-recognized key.
JsonValue.Parse()
will fail quickly at the first error.