1 import mir.test: should; 2 import mir.ndslice.topology: map; 3 import mir.array.allocation: array; 4 5 JsonAlgebraic value; 6 7 StringMap!JsonAlgebraic object; 8 9 // Default 10 assert(value.isNull); 11 assert(value.kind == JsonAlgebraic.Kind.null_); 12 13 // Boolean 14 value = object["bool"] = true; 15 assert(!value.isNull); 16 assert(value == true); 17 assert(value.kind == JsonAlgebraic.Kind.boolean); 18 // access 19 assert(value.boolean == true); 20 assert(value.get!bool == true); 21 assert(value.get!"boolean" == true); 22 assert(value.get!(JsonAlgebraic.Kind.boolean) == true); 23 // nothrow access 24 assert(value.trustedGet!bool == true); 25 assert(value.trustedGet!"boolean" == true); 26 assert(value.trustedGet!(JsonAlgebraic.Kind.boolean) == true); 27 // checks 28 assert(!value._is!string); 29 assert(value._is!bool); 30 assert(value._is!"boolean"); 31 assert(value._is!(JsonAlgebraic.Kind.boolean)); 32 33 // Null 34 value = object["null"] = null; 35 assert(value.isNull); 36 assert(value == null); 37 assert(value.kind == JsonAlgebraic.Kind.null_); 38 // access 39 assert(value.null_ == null); 40 assert(value.get!(typeof(null)) == null); 41 assert(value.get!(JsonAlgebraic.Kind.null_) == null); 42 43 // String 44 value = object["string"] = "s"; 45 assert(value.kind == JsonAlgebraic.Kind.string); 46 assert(value == "s"); 47 // access 48 // Yep, `string` here is an alias to `get!(immutable(char)[])` method 49 assert(value.string == "s"); 50 // `string` here is an alias of type `immutable(char)[]` 51 assert(value.get!string == "s"); 52 assert(value.get!"string" == "s"); 53 // finally, `string` here is an enum meber 54 assert(value.get!(JsonAlgebraic.Kind.string) == "s"); 55 56 // Integer 57 value = object["integer"] = 4; 58 assert(value.kind == JsonAlgebraic.Kind.integer); 59 assert(value == 4); 60 assert(value != 4.0); 61 assert(value.integer == 4); 62 63 // Float 64 value = object["float"] = 3.0; 65 assert(value.kind == JsonAlgebraic.Kind.float_); 66 assert(value != 3); 67 assert(value == 3.0); 68 assert(value.float_ == 3.0); 69 70 // Array 71 JsonAlgebraic[] arr = [0, 1, 2, 3, 4].map!JsonAlgebraic.array; 72 73 value = object["array"] = arr; 74 assert(value.kind == JsonAlgebraic.Kind.array); 75 assert(value == arr); 76 assert(value.array[3] == 3); 77 78 // Object 79 assert(object.keys == ["bool", "null", "string", "integer", "float", "array"]); 80 object.values[0] = "false"; 81 assert(object["bool"] == "false"); // it is a string now 82 object.remove("bool"); // remove the member 83 84 value = object["array"] = object; 85 assert(value.kind == JsonAlgebraic.Kind.object); 86 assert(value.object.keys is object.keys); 87 88 JsonAlgebraic[string] aa = object.toAA; 89 object = aa.StringMap!JsonAlgebraic; 90 91 JsonAlgebraic fromAA = ["a" : JsonAlgebraic(3), "b" : JsonAlgebraic("b")]; 92 assert(fromAA.object["a"] == 3); 93 assert(fromAA.object["b"] == "b"); 94 95 // object foreach iteration 96 long sum; 97 foreach (ref key, ref val; fromAA.object) 98 if (key == "a") 99 sum += val.get!long; 100 sum.should == 3;
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.