Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

asdf.transform

Mutable ASDF data structure. The representation can be used to compute a difference between JSON object-trees.
Authors:
Ilya Yaroshenko
License:
BSL-1.0
struct AsdfNode;
Object-tree structure for mutable Asdf representation.
AsdfNode can be used to construct and manipulate JSON objects. Each AsdfNode can represent either a dynamic JSON object (associative array of AsdfNode nodes) or a ASDF JSON value. JSON arrays can be represented only as JSON values.
AsdfNode[const(char)[]] children;
Children nodes.
Asdf data;
Leaf data.
const pure nothrow @nogc @safe bool isLeaf();
Returns true if the node is leaf.
pure this(Asdf data);
Construct AsdfNode recursively.
pure ref scope AsdfNode opIndex(scope const(char)[][] keys...) return;
Examples:
import asdf;
auto text = `{"foo":"bar","inner":{"a":true,"b":false,"c":"32323","d":null,"e":{}}}`;
auto root = AsdfNode(text.parseJson);
assert(root["inner", "a"].data == `true`.parseJson);
pure void opIndexAssign(AsdfNode value, scope const(char)[][] keys...);
Examples:
import asdf;
auto text = `{"foo":"bar","inner":{"a":true,"b":false,"c":"32323","d":null,"e":{}}}`;
auto root = AsdfNode(text.parseJson);
auto value = AsdfNode(`true`.parseJson);
root["inner", "g", "u"] = value;
assert(root["inner", "g", "u"].data == true);
pure AsdfNode get(AsdfNode value, in char[][] keys...);
Parameters:
AsdfNode value default value
char[][] keys list of keys
Returns:
[keys] if any and value othervise.
Examples:
import asdf;
auto text = `{"foo":"bar","inner":{"a":true,"b":false,"c":"32323","d":null,"e":{}}}`;
auto root = AsdfNode(text.parseJson);
auto value = AsdfNode(`false`.parseJson);
assert(root.get(value, "inner", "a").data == true);
assert(root.get(value, "inner", "f").data == false);
pure void serialize(ref AsdfSerializer serializer);
Serialization primitive
Asdf opCast(T : Asdf)();
Examples:
import asdf;
auto text = `{"foo":"bar","inner":{"a":true,"b":false,"c":"32323","d":null,"e":{}}}`;
auto root = AsdfNode(text.parseJson);
import std.stdio;
Asdf flat = cast(Asdf) root;
assert(flat["inner", "a"] == true);
const pure nothrow @nogc @safe bool opEquals(in AsdfNode rhs);
Examples:
import asdf;
auto text = `{"foo":"bar","inner":{"a":true,"b":false,"c":"32323","d":null,"e":{}}}`;
auto root1 = AsdfNode(text.parseJson);
auto root2= AsdfNode(text.parseJson);
assert(root1 == root2);
assert(root1["inner"].children.remove("b"));
assert(root1 != root2);
pure void add(Asdf data);
Adds data to the object-tree recursively.
Examples:
import asdf;
auto text = `{"foo":"bar","inner":{"a":true,"b":false,"c":"32323","d":null,"e":{}}}`;
auto addition = `{"do":"re","inner":{"a":false,"u":2}}`;
auto root = AsdfNode(text.parseJson);
root.add(addition.parseJson);
auto result = `{"do":"re","foo":"bar","inner":{"a":false,"u":2,"b":false,"c":"32323","d":null,"e":{}}}`;
assert(root == AsdfNode(result.parseJson));
pure void remove(Asdf data);
Removes keys from the object-tree recursively.
Examples:
import asdf;
auto text = `{"foo":"bar","inner":{"a":true,"b":false,"c":"32323","d":null,"e":{}}}`;
auto rem = `{"do":null,"foo":null,"inner":{"c":null,"e":null}}`;
auto root = AsdfNode(text.parseJson);
root.remove(rem.parseJson);
auto result = `{"inner":{"a":true,"b":false,"d":null}}`;
assert(root == AsdfNode(result.parseJson));
pure Asdf removed(AsdfNode node);
Returns the subset of the object-tree which is not represented in node. If a leaf is represented but has a different value then it will be included in the return value. Returned value has ASDF format and its leaves are set to null.
Examples:
import asdf;
auto text1 = `{"inner":{"a":true,"b":false,"d":null}}`;
auto text2 = `{"foo":"bar","inner":{"a":false,"b":false,"c":"32323","d":null,"e":{}}}`;
auto node1 = AsdfNode(text1.parseJson);
auto node2 = AsdfNode(text2.parseJson);
auto diff = AsdfNode(node2.removed(node1));
assert(diff == AsdfNode(`{"foo":null,"inner":{"a":null,"c":null,"e":null}}`.parseJson));
pure Asdf added(AsdfNode node);
Returns the subset of the node which is not represented in the object-tree. If a leaf is represented but has a different value then it will be included in the return value. Returned value has ASDF format.
Examples:
import asdf;
auto text1 = `{"foo":"bar","inner":{"a":false,"b":false,"c":"32323","d":null,"e":{}}}`;
auto text2 = `{"inner":{"a":true,"b":false,"d":null}}`;
auto node1 = AsdfNode(text1.parseJson);
auto node2 = AsdfNode(text2.parseJson);
auto diff = AsdfNode(node2.added(node1));
assert(diff == AsdfNode(`{"foo":"bar","inner":{"a":false,"c":"32323","e":{}}}`.parseJson));