@@ -1070,6 +1070,41 @@ struct JSONValue
1070
1070
}
1071
1071
}
1072
1072
1073
+ // / Calculate a numerical hash value for this value,
1074
+ // / allowing `JSONValue` to be used in associative arrays.
1075
+ hash_t toHash () const @nogc nothrow pure @trusted
1076
+ {
1077
+ final switch (type_tag)
1078
+ {
1079
+ case JSONType.integer:
1080
+ return hashOf (store.integer);
1081
+ case JSONType.uinteger:
1082
+ return hashOf (store.uinteger);
1083
+ case JSONType.float_:
1084
+ return hashOf (store.floating);
1085
+ case JSONType.string :
1086
+ return hashOf (store.str);
1087
+ case JSONType.object:
1088
+ hash_t result = hashOf(type_tag);
1089
+ if (! store.object.isOrdered)
1090
+ foreach (ref pair; store.object.unordered.byKeyValue )
1091
+ result ^= hashOf(pair.key, pair.value.toHash());
1092
+ else
1093
+ foreach (ref pair; store.object.ordered)
1094
+ result ^= hashOf(pair.key, pair.value.toHash());
1095
+ return result;
1096
+ case JSONType.array:
1097
+ hash_t result = hashOf(type_tag);
1098
+ foreach (ref v; store.array)
1099
+ result = hashOf(v, result);
1100
+ return result;
1101
+ case JSONType.true_:
1102
+ case JSONType.false_:
1103
+ case JSONType.null_:
1104
+ return hashOf (type_tag);
1105
+ }
1106
+ }
1107
+
1073
1108
// /
1074
1109
@safe unittest
1075
1110
{
@@ -2748,3 +2783,15 @@ pure nothrow @safe unittest
2748
2783
2749
2784
assert (app.data == s, app.data);
2750
2785
}
2786
+
2787
+ @safe unittest
2788
+ {
2789
+ bool [JSONValue] aa;
2790
+ aa[JSONValue(" test" )] = true ;
2791
+ assert (parseJSON(` "test"` ) in aa);
2792
+ assert (parseJSON(` "boo"` ) ! in aa);
2793
+
2794
+ aa[JSONValue(int (5 ))] = true ;
2795
+ assert (JSONValue(int (5 )) in aa);
2796
+ assert (JSONValue(uint (5 )) in aa);
2797
+ }
0 commit comments