-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathJsonComparer.cs
More file actions
134 lines (118 loc) · 5.05 KB
/
JsonComparer.cs
File metadata and controls
134 lines (118 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System.Collections.Generic;
using System.Text.Json;
namespace Azure.Sdk.Tools.TestProxy.Common
{
public class JsonComparer
{
public static List<string> CompareJson(byte[] json1, byte[] json2)
{
// Deserialize the byte arrays to JsonDocument
JsonDocument doc1 = JsonDocument.Parse(json1);
JsonDocument doc2 = JsonDocument.Parse(json2);
// Compare the JSON objects
var differences = new List<string>();
CompareElements(doc1.RootElement, doc2.RootElement, differences, "");
return differences;
}
private static void CompareElements(JsonElement element1, JsonElement element2, List<string> differences, string path)
{
if (element1.ValueKind != element2.ValueKind)
{
differences.Add($"{path}: Request and record have different types.");
return;
}
switch (element1.ValueKind)
{
case JsonValueKind.Object:
{
var properties1 = element1.EnumerateObject();
var properties2 = element2.EnumerateObject();
var propDict1 = new Dictionary<string, JsonElement>();
var propDict2 = new Dictionary<string, JsonElement>();
foreach (var prop in properties1)
propDict1[prop.Name] = prop.Value;
foreach (var prop in properties2)
propDict2[prop.Name] = prop.Value;
foreach (var key in propDict1.Keys)
{
if (propDict2.ContainsKey(key))
{
CompareElements(propDict1[key], propDict2[key], differences, $"{path}.{key}");
}
else
{
differences.Add($"{path}.{key}: Missing in request JSON");
}
}
foreach (var key in propDict2.Keys)
{
if (!propDict1.ContainsKey(key))
{
differences.Add($"{path}.{key}: Missing in record JSON");
}
}
break;
}
case JsonValueKind.Array:
{
var array1 = element1.EnumerateArray();
var array2 = element2.EnumerateArray();
int index = 0;
var enum1 = array1.GetEnumerator();
var enum2 = array2.GetEnumerator();
while (enum1.MoveNext() && enum2.MoveNext())
{
CompareElements(enum1.Current, enum2.Current, differences, $"{path}[{index}]");
index++;
}
while (enum1.MoveNext())
{
differences.Add($"{path}[{index}]: Extra element in request JSON");
index++;
}
while (enum2.MoveNext())
{
differences.Add($"{path}[{index}]: Extra element in record JSON");
index++;
}
break;
}
case JsonValueKind.String:
{
if (element1.GetString() != element2.GetString())
{
differences.Add($"{path}: \"{element1.GetString()}\" != \"{element2.GetString()}\"");
}
break;
}
case JsonValueKind.Number:
{
if (element1.GetDecimal() != element2.GetDecimal())
{
differences.Add($"{path}: {element1.GetDecimal()} != {element2.GetDecimal()}");
}
break;
}
case JsonValueKind.True:
case JsonValueKind.False:
{
if (element1.GetBoolean() != element2.GetBoolean())
{
differences.Add($"{path}: {element1.GetBoolean()} != {element2.GetBoolean()}");
}
break;
}
case JsonValueKind.Null:
{
// Both are null, nothing to compare
break;
}
default:
{
differences.Add($"{path}: Unhandled value kind {element1.ValueKind}");
break;
}
}
}
}
}