@@ -99,6 +99,30 @@ struct JSONValue
9999 * Use `.hasType!T` or `.typeID` for that purpose.
100100 */
101101 ref inout (T) get (T)() inout { return .get ! T(payload); }
102+
103+ /**
104+ * Enables equality comparisons.
105+ *
106+ * Note that the location is considered token metadata and thus does not
107+ * affect the comparison.
108+ */
109+ bool opEquals (T)(auto ref inout (T) other) inout
110+ {
111+ import std.traits : Unqual;
112+
113+ static if (is (Unqual! T == typeof (null )))
114+ {
115+ return this .isNull;
116+ }
117+ else static if (is (Unqual! T == JSONValue))
118+ {
119+ return this .payload == other.payload;
120+ }
121+ else
122+ {
123+ return this .payload == other;
124+ }
125+ }
102126}
103127
104128// / Shows the basic construction and operations on JSON values.
@@ -124,6 +148,75 @@ unittest
124148 assert (d[" b" ] == b);
125149}
126150
151+ // Unittests for JSONValue equality comparisons
152+ unittest
153+ {
154+ JSONValue nullval = null ;
155+ assert (nullval.hasType! (typeof (null ))());
156+ assert (nullval == null );
157+ assert (nullval == nullval);
158+
159+ JSONValue boolval = true ;
160+ assert (boolval.hasType! bool ());
161+ assert (boolval == true );
162+ assert (boolval == boolval);
163+
164+ JSONValue intval = 22 ;
165+ assert (intval.hasType! long ());
166+ assert (intval == 22 );
167+ assert (intval == 22.0 );
168+ assert (intval == intval);
169+
170+ JSONValue longval = 56L ;
171+ assert (longval.hasType! long ());
172+ assert (longval == 56 );
173+ assert (longval == 56.0 );
174+ assert (longval == longval);
175+
176+ assert (intval + longval == 78 );
177+ assert (intval + longval == intval + longval);
178+
179+ JSONValue floatval = 32.0f ;
180+ assert (floatval.hasType! double ());
181+ assert (floatval == 32 );
182+ assert (floatval == 32.0 );
183+ assert (floatval == floatval);
184+
185+ JSONValue doubleval = 63.5 ;
186+ assert (doubleval.hasType! double ());
187+ assert (doubleval == 63.5 );
188+ assert (doubleval == doubleval);
189+
190+ assert (floatval + doubleval == 95.5 );
191+ assert (floatval + doubleval == floatval + doubleval);
192+ assert (intval + longval + floatval + doubleval == 173.5 );
193+ assert (intval + longval + floatval + doubleval ==
194+ intval + longval + floatval + doubleval);
195+
196+ JSONValue strval = " Hello!" ;
197+ assert (strval.hasType! string ());
198+ assert (strval == " Hello!" );
199+ assert (strval == strval);
200+
201+ auto arrval = JSONValue([floatval, doubleval]);
202+ assert (arrval.hasType! (JSONValue[])());
203+ assert (arrval == [floatval, doubleval]);
204+ assert (arrval == [32.0 , 63.5 ]);
205+ assert (arrval[0 ] == floatval);
206+ assert (arrval[0 ] == 32.0 );
207+ assert (arrval[1 ] == doubleval);
208+ assert (arrval[1 ] == 63.5 );
209+ assert (arrval == arrval);
210+
211+ auto objval = JSONValue([" f" : floatval, " d" : doubleval]);
212+ assert (objval.hasType! (JSONValue[string ])());
213+ assert (objval[" f" ] == floatval);
214+ assert (objval[" f" ] == 32.0 );
215+ assert (objval[" d" ] == doubleval);
216+ assert (objval[" d" ] == 63.5 );
217+ assert (objval == objval);
218+ }
219+
127220
128221// / Proxy structure that stores BigInt as a pointer to save space in JSONValue
129222static struct WrappedBigInt {
0 commit comments