@@ -257,8 +257,7 @@ pub fn isEqualJson(a: anytype, b: anytype) !bool {
257
257
const aa = arena .allocator ();
258
258
const a_value = try convertToJson (aa , a );
259
259
const b_value = try convertToJson (aa , b );
260
- expectJsonValue (a_value , b_value ) catch return false ;
261
- return true ;
260
+ return isJsonValue (a_value , b_value );
262
261
}
263
262
264
263
fn convertToJson (arena : Allocator , value : anytype ) ! std.json.Value {
@@ -308,3 +307,46 @@ fn expectJsonValue(a: std.json.Value, b: std.json.Value) !void {
308
307
},
309
308
}
310
309
}
310
+
311
+ fn isJsonValue (a : std.json.Value , b : std.json.Value ) bool {
312
+ if (std .mem .eql (u8 , @tagName (a ), @tagName (b )) == false ) {
313
+ return false ;
314
+ }
315
+
316
+ // at this point, we know that if a is an int, b must also be an int
317
+ switch (a ) {
318
+ .null = > return true ,
319
+ .bool = > return a .bool == b .bool ,
320
+ .integer = > return a .integer == b .integer ,
321
+ .float = > return a .float == b .float ,
322
+ .number_string = > return std .mem .eql (u8 , a .number_string , b .number_string ),
323
+ .string = > return std .mem .eql (u8 , a .string , b .string ),
324
+ .array = > {
325
+ const a_len = a .array .items .len ;
326
+ const b_len = b .array .items .len ;
327
+ if (a_len != b_len ) {
328
+ return false ;
329
+ }
330
+ for (a .array .items , b .array .items ) | a_item , b_item | {
331
+ if (isJsonValue (a_item , b_item ) == false ) {
332
+ return false ;
333
+ }
334
+ }
335
+ return true ;
336
+ },
337
+ .object = > {
338
+ var it = a .object .iterator ();
339
+ while (it .next ()) | entry | {
340
+ const key = entry .key_ptr .* ;
341
+ if (b .object .get (key )) | b_item | {
342
+ if (isJsonValue (entry .value_ptr .* , b_item ) == false ) {
343
+ return false ;
344
+ }
345
+ } else {
346
+ return false ;
347
+ }
348
+ }
349
+ return true ;
350
+ },
351
+ }
352
+ }
0 commit comments