std.json

Implements functionality to read and write JavaScript Object Notation values.

JavaScript Object Notation is a lightweight data interchange format commonly used in web services and configuration files. It's easy for humans to read and write, and it's easy for machines to parse and generate.

Warning: While JSONValue is fine for small-scale use, at the range of hundreds of megabytes it is

known to cause and exacerbate GC problems. If you encounter problems, try replacing it with a stream parser. See also https://forum.dlang.org/post/dzfyaxypmkdrpakmycjv@forum.dlang.org.

Types 6

enumJSONFloatLiteral : string

String literals used to represent special float values within JSON strings.

nan = "NaN"String representation of floating-point NaN
inf = "Infinite"String representation of floating-point Infinity
negativeInf = "-Infinite"String representation of floating-point negative Infinity

Flags that control how JSON is encoded and parsed.

noneStandard parsing and encoding
specialFloatLiterals = 0x1Encode NaN and Inf float values as strings
escapeNonAsciiChars = 0x2Encode non-ASCII characters with a Unicode escape sequence
doNotEscapeSlashes = 0x4Do not escape slashes ('/')
strictParsing = 0x8Strictly follow RFC-8259 grammar when parsing
preserveObjectOrder = 0x16Preserve order of object keys when parsing
enumJSONType : byte

Enumeration of JSON types

null_Indicates the type of a `JSONValue`.
stringditto
integerditto
uintegerditto
float_ditto
arrayditto
objectditto
true_ditto
false_ditto
NULL = null_
STRING = string
INTEGER = integer
UINTEGER = uinteger
FLOAT = float_
ARRAY = array
OBJECT = object
TRUE = true_
FALSE = false_
deprecated Use JSONType and the new enum member names
structJSONValue

JSON value node

Fields
private Store store
private JSONType type_tag
JSONValue(string[string].init) emptyObjectAn enum value that can be used to obtain a `JSONValue` representing an empty JSON object.
{ JSONValue v = void; v.orderedObject = null; return v; }() emptyOrderedObjectAn enum value that can be used to obtain a `JSONValue` representing an empty JSON object. Unlike `emptyObject`, the order of inserted keys is preserved.
JSONValue(JSONValue[].init) emptyArrayAn enum value that can be used to obtain a `JSONValue` representing an empty JSON array.
Methods
JSONType type() @property const pure nothrow @safe @nogcReturns the JSONType of the value stored in this structure.
string str() @property const pure @trusted return scopeValue getter/setter for `JSONType.string`. Throws: `JSONException` for read access if `type` is not `JSONType.string`.
string str(return scope string v) @property pure nothrow @nogc @trusted returnditto
long integer() @property const pure @safeValue getter/setter for `JSONType.integer`. Throws: `JSONException` for read access if `type` is not `JSONType.integer`.
long integer(long v) @property pure nothrow @safe @nogcditto
ulong uinteger() @property const pure @safeValue getter/setter for `JSONType.uinteger`. Throws: `JSONException` for read access if `type` is not `JSONType.uinteger`.
ulong uinteger(ulong v) @property pure nothrow @safe @nogcditto
double floating() @property const pure @safeValue getter/setter for `JSONType.float`. Note that despite the name, this is a 64-bit `double`, not a 32-bit `float`. Throws: `JSONException` for read access if `type` is not `JSONType.float`.
double floating(double v) @property pure nothrow @safe @nogcditto
bool boolean() @property const pure @safeValue getter/setter for boolean stored in JSON. Throws: `JSONException` for read access if `this.type` is not `JSONType.true` or `JSONType.false`.
bool boolean(bool v) @property pure nothrow @safe @nogcditto
inout(JSONValue[string]) object() @property ref inout pure @system return Value getter/setter for unordered `JSONType.object`. Throws: `JSONException` for read access if `type` is not `JSONType.object` or the object is ordered. Note: This is @system because of the fo...
JSONValue[string] object(return scope JSONValue[string] v) @property pure nothrow @nogc @trustedditto
inout(JSONValue[string]) objectNoRef() @property inout pure @trustedValue getter for unordered `JSONType.object`. Unlike `object`, this retrieves the object by value and can be used in @safe code.
inout(OrderedObjectMember[]) orderedObject() @property ref inout pure @system return Value getter/setter for ordered `JSONType.object`. Throws: `JSONException` for read access if `type` is not `JSONType.object` or the object is unordered. Note: This is @system because of the fo...
OrderedObjectMember[] orderedObject(return scope OrderedObjectMember[] v) @property pure nothrow @nogc @trustedditto
inout(OrderedObjectMember[]) orderedObjectNoRef() @property inout pure @trustedValue getter for ordered `JSONType.object`. Unlike `orderedObject`, this retrieves the object by value and can be used in @safe code.
bool isOrdered() @property const pure @trustedReturns `true` if the order of keys of the represented object is being preserved.
inout(JSONValue[]) array() @property ref scope return inout pure @system Value getter/setter for `JSONType.array`. Throws: `JSONException` for read access if `type` is not `JSONType.array`. Note: This is @system because of the following pattern: --- auto a = &(json....
JSONValue[] array(return scope JSONValue[] v) @property pure nothrow @nogc @trusted scopeditto
inout(JSONValue[]) arrayNoRef() @property inout pure @trustedValue getter for `JSONType.array`. Unlike `array`, this retrieves the array by value and can be used in @safe code.
bool isNull() @property const pure nothrow @safe @nogcTest whether the type is `JSONType.null_`
inout(T) get(T)() @property inout const pure @safeA convenience getter that returns this `JSONValue` as the specified D type. Note: Only numeric types, `bool`, `string`, `JSONValue[string]`, and `JSONValue[]` types are accepted Throws: `JSONExcept...
inout(T) get(T : JSONValue[])() @property inout pure @trusted
inout(T) get(T : JSONValue[string])() @property inout pure @trustedditto
private void assign(T)(T arg)
private void assignRef(T)(ref T arg) if (isStaticArray!T)
void opAssign(T)(T arg) if (!isStaticArray!T && !is(T : JSONValue))
void opAssign(T)(ref T arg) if (isStaticArray!T)
inout(JSONValue) opIndex(size_t i) ref inout pure @safeArray syntax for JSON arrays. Throws: `JSONException` if `type` is not `JSONType.array`.
inout(JSONValue) opIndex(return scope string k) ref inout pure @safeHash syntax for JSON objects. Throws: `JSONException` if `type` is not `JSONType.object`.
void opIndexAssign(T)(auto ref T value, string key)Provides support for index assignments, which sets the corresponding value of the JSON object's `key` field to `value`.
void opIndexAssign(T)(T arg, size_t i)ditto
JSONValue opBinary(string op : "~", T)(T arg)
void opOpAssign(string op : "~", T)(T arg)
inout(JSONValue) * opBinaryRight(string op : "in")(string k) inout @safeProvides support for the `in` operator.
bool opEquals(const JSONValue rhs) const @nogc nothrow pure @safeCompare two JSONValues for equality
bool opEquals(ref const JSONValue rhs) const @nogc nothrow pure @trustedditto
hash_t toHash() const @nogc nothrow pure @trustedCalculate a numerical hash value for this value, allowing `JSONValue` to be used in associative arrays.
int opApply(scope int delegate(size_t index, ref JSONValue) dg) @systemImplements the foreach `opApply` interface for json arrays.
int opApply(scope int delegate(string key, ref JSONValue) dg) @systemImplements the foreach `opApply` interface for json objects.
string toString(in JSONOptions options = JSONOptions.none) const @safeImplicitly calls `toJSON` on this JSONValue.
void toString(Out)(Out sink, in JSONOptions options = JSONOptions.none) const
string toPrettyString(in JSONOptions options = JSONOptions.none) const @safeImplicitly calls `toJSON` on this JSONValue, like `toString`, but also passes true as pretty argument.
void toPrettyString(Out)(Out sink, in JSONOptions options = JSONOptions.none) const
Constructors
this(T arg)Constructor for `JSONValue`. If `arg` is a `JSONValue` its value and type will be copied to the new `JSONValue`. Note that this is a shallow copy: if type is `JSONType.object` or `JSONType.array` t...
this(ref T arg)Ditto
this(inout T arg)Ditto
classJSONException : Exception

Exception thrown on JSON errors

Constructors
this(string msg, int line = 0, int pos = 0)
this(string msg, string file, size_t line)

Functions 4

fnJSONValue parseJSON(T)(T json, int maxDepth = - 1, JSONOptions options = JSONOptions.none) if (isSomeFiniteCharInputRange!T)Parses a serialized string and returns a tree of JSON values. Throws: JSONException if string does not follow the JSON grammar or the depth exceeds the max depth, ConvException if a number in the i...
fnJSONValue parseJSON(T)(T json, JSONOptions options) if (isSomeFiniteCharInputRange!T)Parses a serialized string and returns a tree of JSON values. Throws: JSONException if the depth exceeds the max depth. Params: json = json-formatted string to parse options = enable decoding strin...
fnstring toJSON(const ref JSONValue root, in bool pretty = false, in JSONOptions options = JSONOptions.none) @safeTakes a tree of JSON values and returns the serialized string.
fnvoid toJSON(Out)( auto ref Out json, const ref JSONValue root, in bool pretty = false, in JSONOptions options = JSONOptions.none) if (isOutputRange!(Out, char))