JsonValue
Struct · namespace System · Since 0.21
struct JsonValueReading (Json.Parse), building and writing (JsonValue.ToString / ToIndentedString).
var doc = try Json.Parse("{\"name\": \"Ann\", \"tags\": [\"a\", \"b\"], \"age\": 30}");string name = doc["name"].AsString(); // "Ann"int age = doc["age"].AsInt(); // 30foreach (var tag in doc["tags"].Items()) // the elements of an array Console.WriteLine(tag.AsString());
var o = JsonValue.NewObject();o.Set("ok", JsonValue.Bool(true));o.Set("list", JsonValue.NewArray());o["list"].Add(JsonValue.Number(1.5));Console.WriteLine(o.ToString()); // {"ok":true,"list":[1.5]}A JsonValue is a small struct: copies of an array or object share its elements (like List<T>). Objects keep the order in which their keys were added. Numbers are doubles. Asking a missing key of an object gives a JSON null, so chains like doc[“a”][“b”] need no checks in between; AsString(), AsInt(), … end the program with a panic if the value has another kind (check it with IsString(), Kind, …).
Fields
Section titled “Fields”| Name | Description |
|---|---|
Kind |
The kind of the value: null, bool, number, string, array or object. |
JsonKind KindThe kind of the value: null, bool, number, string, array or object.
Static methods
Section titled “Static methods”| Name | Description |
|---|---|
Null |
The JSON value null. |
Bool |
The JSON value true or false. |
Number |
A JSON number. |
String |
A JSON string. |
NewArray |
A new, empty array (JsonValue.Add appends elements). |
NewObject |
A new, empty object (JsonValue.Set adds members). |
static JsonValue Null()The JSON value null.
static JsonValue Bool(bool value)The JSON value true or false.
Number
Section titled “Number”static JsonValue Number(double value)A JSON number.
String
Section titled “String”static JsonValue String(string value)A JSON string.
NewArray
Section titled “NewArray”static JsonValue NewArray()A new, empty array (JsonValue.Add appends elements).
NewObject
Section titled “NewObject”static JsonValue NewObject()A new, empty object (JsonValue.Set adds members).
Methods
Section titled “Methods”| Name | Description |
|---|---|
IsNull |
Whether the value is null (also a missing member of an object). |
IsBool |
Whether the value is true or false. |
IsNumber |
Whether the value is a number. |
IsString |
Whether the value is a string. |
IsArray |
Whether the value is an array. |
IsObject |
Whether the value is an object. |
AsBool |
The value of a bool. |
AsNumber |
The value of a number. |
AsInt |
The value of a number as an int. |
AsInt64 |
The value of a number as an int64. |
AsString |
The text of a string. |
Count |
The number of elements of an array or members of an object (0 for the other kinds). |
Get |
Element index of an array (also value[index]). |
Set |
Replaces element index of an array (also value[index] = x). |
Add |
Appends value to an array. |
Items |
The elements of an array (none for the other kinds). |
Has |
Whether an object has the member key (false for the other kinds). |
Remove |
Removes the member key of an object (nothing happens if it has none). |
Keys |
The keys of an object in their order (none for the other kinds). |
ToString |
The value as compact JSON text: {"ok":true,"list":[1.5]}. |
ToIndentedString |
The value as JSON text with line breaks and indent spaces per level. |
IsNull
Section titled “IsNull”bool IsNull()Whether the value is null (also a missing member of an object).
IsBool
Section titled “IsBool”bool IsBool()Whether the value is true or false.
IsNumber
Section titled “IsNumber”bool IsNumber()Whether the value is a number.
IsString
Section titled “IsString”bool IsString()Whether the value is a string.
IsArray
Section titled “IsArray”bool IsArray()Whether the value is an array.
IsObject
Section titled “IsObject”bool IsObject()Whether the value is an object.
AsBool
Section titled “AsBool”bool AsBool()The value of a bool.
Panics when the value is not a bool.
AsNumber
Section titled “AsNumber”double AsNumber()The value of a number.
Panics when the value is not a number.
int AsInt()The value of a number as an int.
Panics when the value is not a number, not a whole number or not in the range of int.
AsInt64
Section titled “AsInt64”int64 AsInt64()The value of a number as an int64.
Panics when the value is not a number, not a whole number or not in the range of int64.
AsString
Section titled “AsString”string AsString()The text of a string.
Panics when the value is not a string.
int Count()The number of elements of an array or members of an object (0 for the other kinds).
JsonValue Get(int index)Element index of an array (also value[index]).
Panics when the value is not an array or index is outside of it.
JsonValue Get(string key)The member key of an object (also value["key"]).
Returns the JSON null if there is no such member (also for the other kinds), so chains like doc["a"]["b"] need no checks in between.
void Set(int index, JsonValue value)Replaces element index of an array (also value[index] = x).
Panics when the value is not an array or index is outside of it.
void Set(string key, JsonValue value)Adds or replaces the member key of an object (also value["key"] = x).
Panics when the value is not an object.
void Add(JsonValue value)Appends value to an array.
Panics when the value is not an array.
JsonValue[] Items()The elements of an array (none for the other kinds).
bool Has(string key)Whether an object has the member key (false for the other kinds).
Remove
Section titled “Remove”void Remove(string key)Removes the member key of an object (nothing happens if it has none).
Panics when the value is not an object.
string[] Keys()The keys of an object in their order (none for the other kinds).
ToString
Section titled “ToString”string ToString()The value as compact JSON text: {"ok":true,"list":[1.5]}.
ToIndentedString
Section titled “ToIndentedString”string ToIndentedString(int indent)The value as JSON text with line breaks and indent spaces per level.
string ToIndentedString()The value as JSON text with line breaks and 2 spaces per level.