1 import mir.ndslice.topology: map; 2 import mir.array.allocation: array; 3 4 JsonAlgebraic value; 5 6 StringMap!JsonAlgebraic object; 7 8 // Default 9 assert(value.isNull); 10 assert(value.kind == JsonAlgebraic.Kind.null_); 11 12 // Boolean 13 value = object["bool"] = true; 14 assert(!value.isNull); 15 assert(value == true); 16 assert(value.kind == JsonAlgebraic.Kind.boolean); 17 assert(value.get!bool == true); 18 assert(value.get!(JsonAlgebraic.Kind.boolean) == true); 19 20 // Null 21 value = object["null"] = null; 22 assert(value.isNull); 23 assert(value == null); 24 assert(value.kind == JsonAlgebraic.Kind.null_); 25 assert(value.get!(typeof(null)) == null); 26 assert(value.get!(JsonAlgebraic.Kind.null_) == null); 27 28 // String 29 value = object["string"] = "s"; 30 assert(value.kind == JsonAlgebraic.Kind.string); 31 assert(value == "s"); 32 assert(value.get!string == "s"); 33 assert(value.get!(JsonAlgebraic.Kind.string) == "s"); 34 35 // Integer 36 value = object["integer"] = 4; 37 assert(value.kind == JsonAlgebraic.Kind.integer); 38 assert(value == 4); 39 assert(value != 4.0); 40 assert(value.get!long == 4); 41 assert(value.get!(JsonAlgebraic.Kind.integer) == 4); 42 43 // Float 44 value = object["float"] = 3.0; 45 assert(value.kind == JsonAlgebraic.Kind.float_); 46 assert(value != 3); 47 assert(value == 3.0); 48 assert(value.get!double == 3.0); 49 assert(value.get!(JsonAlgebraic.Kind.float_) == 3.0); 50 51 // Array 52 JsonAlgebraic[] arr = [0, 1, 2, 3, 4].map!JsonAlgebraic.array; 53 54 value = object["array"] = arr; 55 assert(value.kind == JsonAlgebraic.Kind.array); 56 assert(value == arr); 57 assert(value.get!(JsonAlgebraic[])[3] == 3); 58 59 // Object 60 assert(object.keys == ["bool", "null", "string", "integer", "float", "array"]); 61 object.values[0] = "false"; 62 assert(object["bool"] == "false"); // it is a string now 63 object.remove("bool"); // remove the member 64 65 value = object["array"] = object; 66 assert(value.kind == JsonAlgebraic.Kind.object); 67 assert(value.get!(StringMap!JsonAlgebraic).keys is object.keys); 68 assert(value.get!(StringMap!JsonAlgebraic).values is object.values); 69 70 JsonAlgebraic[string] aa = object.toAA; 71 object = StringMap!JsonAlgebraic(aa); 72 73 JsonAlgebraic fromAA = ["a" : JsonAlgebraic(3), "b" : JsonAlgebraic("b")]; 74 assert(fromAA.get!(StringMap!JsonAlgebraic)["a"] == 3); 75 assert(fromAA.get!(StringMap!JsonAlgebraic)["b"] == "b");
JSON tagged algebraic alias.
The example below shows only the basic features. Advanced API to work with algebraic types can be found at $(GMREF mir-core, mir,algebraic). See also mir.string_map - ordered string-value associative array.