Skip to content
CShift0.32

JsonValue

Struct · namespace System · Since 0.21

struct JsonValue

Reading (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(); // 30
foreach (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, …).

Name Description
Kind The kind of the value: null, bool, number, string, array or object.
JsonKind Kind

The kind of the value: null, bool, number, string, array or object.

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.

static JsonValue Number(double value)

A JSON number.

static JsonValue String(string value)

A JSON string.

static JsonValue NewArray()

A new, empty array (JsonValue.Add appends elements).

static JsonValue NewObject()

A new, empty object (JsonValue.Set adds members).

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.
bool IsNull()

Whether the value is null (also a missing member of an object).

bool IsBool()

Whether the value is true or false.

bool IsNumber()

Whether the value is a number.

bool IsString()

Whether the value is a string.

bool IsArray()

Whether the value is an array.

bool IsObject()

Whether the value is an object.

bool AsBool()

The value of a bool.

Panics when the value is not a bool.

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.

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.

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).

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).

string ToString()

The value as compact JSON text: {"ok":true,"list":[1.5]}.

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.