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.

mir.algebraic_alias.json

Mutable JSON value

This module contains a single alias definition and doesn't provide JSON serialization API.
See Also:
JSON libraries mir-ion and asdf;
License:
Authors:
Ilya Yaroshenko
union JsonAlgebraicUnion;
Definition union for JsonAlgebraic.
typeof(null) null_;
bool boolean;
long integer;
double float_;
string string;
This[] array;
Self alias in array.
StringMap!This object;
Self alias in mir.string_map.
alias JsonAlgebraic = mir.algebraic.Algebraic!(TaggedType!(typeof(null), "null_"), TaggedType!(bool, "boolean"), TaggedType!(StringMap!(This, uint), "object"), TaggedType!(double, "float_"), TaggedType!(long, "integer"), TaggedType!(This[], "array"), TaggedType!(string, "string")).Algebraic;
JSON tagged algebraic alias.
The example below shows only the basic features. Advanced API to work with algebraic types can be found at mir.algebraic. See also mir.string_map - ordered string-value associative array.
Examples:
import mir.ndslice.topology: map;
import mir.array.allocation: array;

JsonAlgebraic value;

StringMap!JsonAlgebraic object;

// Default
assert(value.isNull);
assert(value.kind == JsonAlgebraic.Kind.null_);

// Boolean
value = object["bool"] = true;
assert(!value.isNull);
assert(value == true);
assert(value.kind == JsonAlgebraic.Kind.boolean);
assert(value.get!bool == true);
assert(value.get!(JsonAlgebraic.Kind.boolean) == true);

// Null
value = object["null"] = null;
assert(value.isNull);
assert(value == null);
assert(value.kind == JsonAlgebraic.Kind.null_);
assert(value.get!(typeof(null)) == null);
assert(value.get!(JsonAlgebraic.Kind.null_) == null);

// String
value = object["string"] = "s";
assert(value.kind == JsonAlgebraic.Kind.string);
assert(value == "s");
assert(value.get!string == "s");
assert(value.get!(JsonAlgebraic.Kind.string) == "s");

// Integer
value = object["integer"] = 4;
assert(value.kind == JsonAlgebraic.Kind.integer);
assert(value == 4);
assert(value != 4.0);
assert(value.get!long == 4);
assert(value.get!(JsonAlgebraic.Kind.integer) == 4);

// Float
value = object["float"] = 3.0;
assert(value.kind == JsonAlgebraic.Kind.float_);
assert(value != 3);
assert(value == 3.0);
assert(value.get!double == 3.0);
assert(value.get!(JsonAlgebraic.Kind.float_) == 3.0);

// Array
JsonAlgebraic[] arr = [0, 1, 2, 3, 4].map!JsonAlgebraic.array;

value = object["array"] = arr;
assert(value.kind == JsonAlgebraic.Kind.array);
assert(value == arr);
assert(value.get!(JsonAlgebraic[])[3] == 3);

// Object
assert(object.keys == ["bool", "null", "string", "integer", "float", "array"]);
object.values[0] = "false";
assert(object["bool"] == "false"); // it is a string now
object.remove("bool"); // remove the member

value = object["array"] = object;
assert(value.kind == JsonAlgebraic.Kind.object);
assert(value.get!(StringMap!JsonAlgebraic).keys is object.keys);
assert(value.get!(StringMap!JsonAlgebraic).values is object.values);

JsonAlgebraic[string] aa = object.toAA;
object = StringMap!JsonAlgebraic(aa);

JsonAlgebraic fromAA = ["a" : JsonAlgebraic(3), "b" : JsonAlgebraic("b")];
assert(fromAA.get!(StringMap!JsonAlgebraic)["a"] == 3);
assert(fromAA.get!(StringMap!JsonAlgebraic)["b"] == "b");