toJSON

Converts the given JSON document(s) to its string representation.

The input can be a JSONValue, or an input range of either JSONToken or JSONParserNode elements. By default, the generator will use newlines and tabs to pretty-print the result. Use the options template parameter to customize this.

Parameters

nodes Input

A set of JSON documents encoded as single parser nodes. The nodes must be in valid document order, or the parser result will be undefined.

Return Value

Type: string

Returns a JSON formatted string.

Examples

JSONValue value = true;
assert(value.toJSON() == "true");
auto a = toJSONValue(`{"a": [], "b": [1, {}]}`);

// pretty print:
// {
//     "a": [],
//     "b": [
//         1,
//         {},
//     ]
// }
assert(
    a.toJSON() == "{\n\t\"a\": [],\n\t\"b\": [\n\t\t1,\n\t\t{}\n\t]\n}" ||
    a.toJSON() == "{\n\t\"b\": [\n\t\t1,\n\t\t{}\n\t],\n\t\"a\": []\n}"
);

// write compact JSON (order of object fields is undefined)
assert(
    a.toJSON!(GeneratorOptions.compact)() == `{"a":[],"b":[1,{}]}` ||
    a.toJSON!(GeneratorOptions.compact)() == `{"b":[1,{}],"a":[]}`
);

See Also

writeJSON, toPrettyJSON

Meta