json
This module defines JSON types, reading, and writing.
Example:
Working with objects
// Create an object conveniently with a helper function.
JSON object = jsonObject();
// We can assign compatible types to keys.
object["key"] = 3;
// We can check for existence.
assert("key" in object);
// We can convert back to some primitive value again.
double num = cast(double) object["key"];
// We can get direct access to the object with a runtime check.
object.object["key"] == object["key"];
// This is not an object!
assertThrown(object["key"].object);
Example:
Working with arrays.
// Create an array conveniently with a helper function.
JSON array = jsonArray();
// We can append some values to the arrays.
array ~= null;
array ~= "a string";
// Another array.
array ~= jsonArray();
// Check the types with some properties.
assert(array[0].isNull);
assert(array[1].isString);
assert(array[2].isArray);
// Once again we can use direct access, runtime checked.
assert(array.array ~= JSON(347));
assert(array[3] == 347);
// We can allocate arrays conveniently with a given size.
JSON anotherArray = jsonArray(10);
assert(anotherArray.length == 10);
// foreach works with size_t on arrays and string on objects.
foreach(size_t index, value; anotherArray) {
// JSON values default to null.
assert(value.isNull);
}
- template isJSONPrimitive(T)
- Determine if a type can represent a JSON primitive type.
- template isJSONArray(T)
- Determine if a type can represent a JSON array.
- template isJSONObject(T)
- Determine if a type can represent a JSON object.
- template isJSON(T)
- Determine if a type can represent any JSON value.
The special JSON type is included here.
- class JSONException: object.Exception;
- The root class for all JSON exceptions.
- class JSONWriteException: json.JSONException;
- This class of exception may be thrown when something goes wrong
while writing JSON data.
- class JSONParseException: json.JSONException;
- This class of exception may be thrown when something goes wrong
while reading JSON data.
- const long line;
- The line number (starting at 1) where the problem occurred.
- const long column;
- The column number (starting at 1) where the problem occurred.
- enum JSON_TYPE;
- The possible types of JSON values.
- NULL
- The JSON value holds null. (This is the default value)
- BOOL
- The JSON value holds explicitly true or false.
- STRING
- The JSON value holds a string.
- INT
- The JSON value holds an integer.
- FLOAT
- The JSON value holds a floating point number.
- OBJECT
- The JSON value holds a JSON object. (associative array)
- ARRAY
- The JSON value holds a JSON array.
- struct JSON;
- A discriminated union representation of any JSON value.
- inout this(long integer);
- Initialize the JSON type from an integer.
- inout this(bool boolean);
- Initialize the JSON type from a boolean.
- inout this(real floating);
- Initialize the JSON type from a floating point value.
- inout this(typeof(null) nothing);
- Initialize the JSON type explicitly to null.
- inout this(inout(string) str);
- Initialize the JSON type as a string.
- inout this(inout(JSON[]) array);
- Initialize the JSON type from an existing JSON array.
- inout this(inout(JSON[string]) object);
- Initialize the JSON type from an existing JSON object.
- pure nothrow @trusted long opAssign(long integer);
- Assign an integer to the JSON value.
- pure nothrow @trusted bool opAssign(bool boolean);
- Assign a boolean to the JSON value.
- pure nothrow @trusted real opAssign(real floating);
- Assign a floating point number to the JSON value.
- pure nothrow @trusted typeof(null) opAssign(typeof(null) nothing);
- Assign null to the JSON value.
- pure nothrow @trusted string opAssign(string str);
- Assign a string the JSON value.
- pure nothrow @trusted JSON[] opAssign(JSON[] array);
- Assign an array to the JSON value.
- pure nothrow @trusted JSON[string] opAssign(JSON[string] object);
- Assign an object to the JSON value.
- const pure nothrow @property @trusted JSON_TYPE type();
- Returns:
An enum describing the current type of the JSON value.
- const pure nothrow @property @trusted bool isBool();
- Returns:
true if this JSON value is a boolean value.
- const pure nothrow @property @trusted bool isNumber();
- Returns:
true if this JSON value contains a numeric type.
This includes boolean values.
- const pure nothrow @property @trusted bool isString();
- Returns:
true if the value is a string.
- const pure nothrow @property @trusted bool isNull();
- Returns:
true if this JSON value is null.
- const pure nothrow @property @trusted bool isArray();
- Returns:
true if this JSON value is an array.
- const pure nothrow @property @trusted bool isObject();
- Returns:
true if this JSON value is an object.
- inout pure @property ref @trusted inout(JSON[]) array();
- Returns:
A reference to the JSON array stored in this object.
Throws:
Exception when the JSON type is not an array.
- inout pure @property ref @trusted inout(JSON[string]) object();
- Returns:
A reference to the JSON object stored in this object.
Throws:
Exception when the JSON type is not an object.
- const @property @trusted size_t length();
- Returns:
The length of the inner JSON array or object.
Throws:
Exception when this is not an array or object.
- pure @property @trusted void length(size_t len);
- Set the length of the inner JSON array.
Throws:
Exception when this is not an array.
- const @trusted string toString();
- Returns:
The JSON value converted to a string.
- inout inout(T) opCast(T)();
- Returns:
The JSON value cast to another value.
Throws:
Exception when the type held in the JSON value does not match.
- inout inout(T) opCast(T : bool)();
- Cast the JSON type to a bool.
For objects, arrays, and strings, this is length > 0.
For numeric types, this is value != 0.
null becomes false.
Returns:
The JSON value cast to a boolean.
- JSON opBinary(string op : "~", T)(T val);
- Concatenate this JSON array with another value.
This will place the value inside of the array, including other arrays.
Example:
JSON arr = jsonArray(); // []
JSON arr2 = arr ~ 3 // [3]
JSON arr3 = arr2 ~ jsonArray() // [3, []]
Params:
Returns:
A new JSON array with the value on the end.
- void put(T)(T val);
- Add a value to this JSON array.
Throws:
Exception when this is not an array.
- void opOpAssign(string op : "~", T)(T val);
- See Also:
put
- inout pure ref @trusted inout(JSON) opIndex(size_t index);
- Params:
| size_t index |
The index for the value in the array. |
Returns:
A reference to a value in the array.
Throws:
Exception when this is not an array.
Throws:
Error when the index is out of bounds.
- inout pure ref @trusted inout(JSON) opIndex(string key);
- Params:
| string key |
The key for the value in the object. |
Returns:
A reference to a value in the object.
Throws:
Exception when this is not an object.
- void opIndexAssign(T)(T value, size_t index);
- When this JSON value is an array, assign a value to an index of it.
Params:
| value |
The value to assign to the index. |
| index |
The index in the array. |
Throws:
Exception when the JSON value is not an array.
- void opIndexAssign(T)(T value, string key);
- When this JSON value is an object, assign a value to a key of it.
Params:
| value |
The value to assign to the index. |
| index |
The key in the object. |
Throws:
Exception when the JSON value is not an object.
- inout inout(JSON*) opBinaryRight(string op : "in")(string key);
- When this JSON value is an object, test for existence of a key in it.
Params:
| key |
The key in the object. |
Returns:
A pointer to the value in the object.
Throws:
Exception when the JSON value is not an object.
- @trusted int opApply(int delegate(ref JSON val) dg);
- Support foreach through JSON values in an array or object.
If the JSON value is not an array or object, foreach does not throw
an exception.
Example:
foreach(value; someArray) { ... }
- @trusted int opApply(int delegate(string key, ref JSON val) dg);
- Support foreach through key-value pairs.
If the JSON value is not an object, foreach does not throw an exception.
If the JSON value is an array, indices will be converted to strings.
Example:
foreach(string key, value; someObject) { ... }
- @trusted int opApply(int delegate(size_t index, ref JSON val) dg);
- Support foreach through index-value pairs.
If the JSON value is not an array or object, no exceptions will be
thrown.
Example:
foreach(size_t index, value; someObject) { ... }
Throws:
Exception when the JSON value is an object.
- @trusted int opApplyReverse(int delegate(ref JSON val) dg);
@trusted int opApplyReverse(int delegate(string key, ref JSON val) dg);
@trusted int opApplyReverse(int delegate(size_t index, ref JSON val) dg);
- foreach_reverse support
- inout bool opEquals(T)(inout(T) other);
- Returns:
true if this JSON value is equal to another value.
- pure nothrow @trusted JSON jsonObject();
- Returns:
A new JSON object.
- pure nothrow @trusted JSON jsonArray(size_t size = 0);
- Params:
| size_t size |
The initial size of the new array. |
Returns:
A new JSON array.
- JSON concat()(auto ref JSON left, auto ref JSON right);
JSON concat()(auto ref JSON left, JSON[] right);
JSON concat()(JSON[] left, auto ref JSON right);
JSON concat()(JSON[] left, JSON[] right);
- Concatenate two JSON arrays
Example:
JSON arr = jsonArray(); // []
arr ~= 1; // [1]
JSON arr2 = jsonArray(); // []
arr2 ~= 3; // [3]
JSON arr3 = arr.concat(arr2); // [1, 3]
Params:
| left |
The first JSON array. |
| right |
The right JSON array. |
Returns:
The concatenation of the two arrays as a JSON value.
Throws:
Exception if either value is not an array.
- JSON convertJSON(JSONCompatible)(auto ref JSONCompatible value);
- Example:
JSON arr = convertJSON([1, 2, 3, 4, 5]);
Params:
| value |
A value which can be converted to JSON. |
Returns:
A JSON object created using the value.
- void writeJSON(int spaces = 0, OutputRange)(in JSON json, OutputRange outputRange);
- Write a JSON value to an output range.
Params:
| spaces |
The number of spaces to indent the output with. |
| json |
A JSON value to write to the outputRange. |
| outputRange |
An output range to write the JSON value to. |
- void writeJSON(int spaces = 0)(in JSON json, File file);
- Write a JSON value to a file.
Params:
| spaces |
The number of spaces to indent the output with. |
| json |
A JSON value to write to the file. |
| file |
A file to write the JSON value to. |
- string toJSON(int spaces = 0)(in JSON json);
- Create a JSON string from a given JSON value.
Params:
| spaces |
The number of spaces to indent the output with. |
| json |
A JSON value to create the string with. |
- JSON parseJSON(InputRange)(InputRange inputRange);
- Params:
| inputRange |
A range of characters to read a JSON string from. |
Returns:
A JSON value.
Throws:
JSONParseException When the range contains invalid JSON.
- JSON parseJSON(size_t chunkSize = 4096)(File file);
- Params:
| chunkSize |
The number of bytes to read at a time. |
| file |
A file object to read the JSON value from. |
Returns:
A JSON value.
Throws:
JSONParseException When the range contains invalid JSON.
Page generated by Ddoc.