JavaScript Object Notation
import std.conv : to; // parse a file or string of json into a usable structure string s = `{ "language": "D", "rating": 3.5, "code": "42" }`; JSONValue j = parseJSON(s); // j and j["language"] return JSONValue, // j["language"].str returns a string writeln(j["language"].str); // "D" writeln(j["rating"].floating); // 3.5 // check a type long x; if (const(JSONValue)* code = "code" in j) { if (code.type() == JSONType.integer) x = code.integer; else x = to!int(code.str); } // create a json struct JSONValue jj = [ "language": "D" ]; // rating doesnt exist yet, so use .object to assign jj.object["rating"] = JSONValue(3.5); // create an array to assign to list jj.object["list"] = JSONValue( ["a", "b", "c"] ); // list already exists, so .object optional jj["list"].array ~= JSONValue("D"); string jjStr = `{"language":"D","list":["a","b","c","D"],"rating":3.5}`; writeln(jj.toString); // jjStr
String literals used to represent special float values within JSON strings.
string representation of floating-point NaN
string representation of floating-point Infinity
string representation of floating-point negative Infinity
Flags that control how json is encoded and parsed.
standard parsing
encode NaN and Inf float values as strings
encode non ascii characters with an unicode escape sequence
do not escape slashes ('/')
Strictly follow RFC-8259 grammar when parsing
JSON type enumeration
Indicates the type of a JSONValue
.
These symbols will be deprecated after 2.082.
This alias will be deprecated after 2.082.
JSON value node
Returns the JSONType of the value stored in this structure.
string s = "{ \"language\": \"D\" }"; JSONValue j = parseJSON(s); writeln(j.type); // JSONType.object writeln(j["language"].type); // JSONType.string
Value getter/setter for JSONType.string
.
JSONException
for read access if type
is not JSONType.string
.JSONValue j = [ "language": "D" ]; // get value writeln(j["language"].str); // "D" // change existing key to new string j["language"].str = "Perl"; writeln(j["language"].str); // "Perl"
Value getter/setter for JSONType.integer
.
JSONException
for read access if type
is not JSONType.integer
.Value getter/setter for JSONType.uinteger
.
JSONException
for read access if type
is not JSONType.uinteger
.Value getter/setter for JSONType.float_
. Note that despite the name, this is a 64-bit double
, not a 32-bit float
.
JSONException
for read access if type
is not JSONType.float_
.Value getter/setter for boolean stored in JSON.
JSONException
for read access if this.type
is not JSONType.true_
or JSONType.false_
.JSONValue j = true; writeln(j.boolean); // true j.boolean = false; writeln(j.boolean); // false j.integer = 12; import std.exception : assertThrown; assertThrown!JSONException(j.boolean);
Value getter/setter for JSONType.object
.
JSONException
for read access if type
is not JSONType.object
. auto a = &(json.object()); json.uinteger = 0; // overwrite AA pointer (*a)["hello"] = "world"; // segmentation fault
Value getter for JSONType.object
. Unlike object
, this retrieves the object by value and can be used in @safe code.
A caveat is that, if the returned value is null, modifications will not be visible:
JSONValue json; json.object = null; json.objectNoRef["hello"] = JSONValue("world"); assert("hello" !in json.object);
JSONException
for read access if type
is not JSONType.object
.Value getter/setter for JSONType.array
.
JSONException
for read access if type
is not JSONType.array
. auto a = &(json.array()); json.uinteger = 0; // overwrite array pointer (*a)[0] = "world"; // segmentation fault
Value getter for JSONType.array
. Unlike array
, this retrieves the array by value and can be used in @safe code.
A caveat is that, if you append to the returned array, the new values aren't visible in the
JSONValue json; json.array = [JSONValue("hello")]; json.arrayNoRef ~= JSONValue("world"); assert(json.array.length == 1);
JSONException
for read access if type
is not JSONType.array
.Test whether the type is JSONType.null_
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
then only the reference to the data will be copied. Otherwise, arg
must be implicitly convertible to one of the following types: typeof(null)
, string
, ulong
, long
, double
, an associative array V[K]
for any V
and K
i.e. a JSON object, any array or bool
. The type will be set accordingly.
JSONValue j = JSONValue( "a string" ); j = JSONValue(42); j = JSONValue( [1, 2, 3] ); writeln(j.type); // JSONType.array j = JSONValue( ["language": "D"] ); writeln(j.type); // JSONType.object
Array syntax for json arrays.
JSONException
if type
is not JSONType.array
.JSONValue j = JSONValue( [42, 43, 44] ); writeln(j[0].integer); // 42 writeln(j[1].integer); // 43
Hash syntax for json objects.
JSONException
if type
is not JSONType.object
.JSONValue j = JSONValue( ["language": "D"] ); writeln(j["language"].str); // "D"
Operator sets value
for element of JSON object by key
.
If JSON value is null, then operator initializes it with object and then sets value
for it.
JSONException
if type
is not JSONType.object
or JSONType.null_
.JSONValue j = JSONValue( ["language": "D"] ); j["language"].str = "Perl"; writeln(j["language"].str); // "Perl"
Support for the in
operator.
Tests wether a key can be found in an object.
const(JSONValue)*
that matches to the key, otherwise null
. JSONException
if the right hand side argument JSONType
is not object
.JSONValue j = [ "language": "D", "author": "walter" ]; string a = ("author" in j).str;
Implements the foreach opApply
interface for json arrays.
Implements the foreach opApply
interface for json objects.
Implicitly calls toJSON
on this JSONValue.
options can be used to tweak the conversion behavior.
Implicitly calls toJSON
on this JSONValue, like toString
, but also passes true as pretty argument.
options can be used to tweak the conversion behavior
Parses a serialized string and returns a tree of JSON values.
JSONException
if string does not follow the JSON grammar or the depth exceeds the max depth, ConvException
if a number in the input cannot be represented by a native D type. T json
| json-formatted string to parse |
int maxDepth
| maximum depth of nesting allowed, -1 disables depth checking |
JSONOptions options
| enable decoding string representations of NaN/Inf as float values |
Parses a serialized string and returns a tree of JSON values.
JSONException
if the depth exceeds the max depth. T json
| json-formatted string to parse |
JSONOptions options
| enable decoding string representations of NaN/Inf as float values |
Takes a tree of JSON values and returns the serialized string.
Any Object types will be serialized in a key-sorted order.
If pretty
is false no whitespaces are generated. If pretty
is true serialized string is formatted to be human-readable. Set the JSONOptions.specialFloatLiterals
flag is set in options
to encode NaN/Infinity as strings.
Exception thrown on JSON errors
© 1999–2018 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_json.html