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

tokens Input

List of JSON tokens to be converted to strings. The tokens may occur in any order and are simply appended in order to the final string.

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