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 isknown 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.
Copyright
Copyright Jeremie Pelletier 2008 - 2009.
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
enumJSONOptions
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_
structJSONValue
JSON value node
Fields
private Store storeprivate JSONType type_tagJSONValue(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.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`.long integer() @property const pure @safeValue getter/setter for `JSONType.integer`. Throws: `JSONException` for read access if `type` is not `JSONType.integer`.ulong uinteger() @property const pure @safeValue getter/setter for `JSONType.uinteger`. Throws: `JSONException` for read access if `type` is not `JSONType.uinteger`.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`.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`.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 @trusteddittoinout(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 @trusteddittoinout(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....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.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...private
void assign(T)(T arg)private
void assignRef(T)(ref T arg) if (isStaticArray!T)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)dittovoid 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 equalityhash_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) conststring 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) constclassJSONException : Exception
Functions 4
fn
JSONValue 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...fn
JSONValue 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...fn
string 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.fn
void toJSON(Out)(
auto ref Out json,
const ref JSONValue root,
in bool pretty = false,
in JSONOptions options = JSONOptions.none) if (isOutputRange!(Out, char))