true on success, false on error (with err set).bool parseJSON(out var value, const(char)[] input, out JsonError err,
JsonPolicy policy = JsonPolicy.init) @safeParse JSON text into a var value.
This is the primary parsing function that builds a DOM-style tree representation of the JSON data.
value | Output parameter for the parsed value. |
input | JSON text to parse. |
err | Output parameter for error information on failure. |
policy | Policy controlling parser behavior. |
true on success, false on error (with err set).import ddn.data.json : parseJSON, JsonError;
import ddn.var : var;
JsonError err;
var v;
if (parseJSON(v, `{"name": "John", "age": 30}`, err)) {
assert(v["name"].as!string == "John");
assert(v["age"].as!long == 30);
}var parseJSON(const(char)[] input) @safeConvenience overload that parses JSON and returns the value directly.
Returns var.init on parse failure.
input | JSON text to parse. |
var value, or var.init on error.bool parseJSON(out var value, const(char)[] input, JsonReviver reviver, out JsonError err,
JsonPolicy policy = JsonPolicy.init) @safeParse JSON text and apply reviver to each value.
The reviver is called in post-order traversal (children are transformed before their parents). This matches the behavior of JavaScript's JSON.parse(text, reviver).
value | Output parameter for the parsed and transformed value. |
input | JSON text to parse. |
reviver | Delegate to transform each value. May be null (no transformation). |
err | Output parameter for error information on failure. |
policy | Policy controlling parser behavior. |
true on success; false with err populated on parse failure.import ddn.data.json : parseJSON, JsonReviver, JsonError;
import ddn.var : var;
// Double all integer values
JsonReviver rev = (const string key, var value) @safe {
if (value.type == var.Type.LONG) {
return var(value.as!long * 2);
}
return value;
};
JsonError err;
var v;
assert(parseJSON(v, `{"a":1,"b":2}`, rev, err));
assert(v["a"].as!long == 2);
assert(v["b"].as!long == 4);