-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathNormalizeUtils.cs
More file actions
175 lines (152 loc) · 5.88 KB
/
NormalizeUtils.cs
File metadata and controls
175 lines (152 loc) · 5.88 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System.Collections.Generic;
using System.Linq.Dynamic.Core.SystemTextJson.Config;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace System.Linq.Dynamic.Core.SystemTextJson.Utils;
internal static class NormalizeUtils
{
/// <summary>
/// Normalizes a document so that each object contains all properties found in the array, including nested objects.
/// </summary>
internal static JsonDocument NormalizeJsonDocument(JsonDocument jsonDocument, NormalizationNonExistingPropertyBehavior normalizationBehavior)
{
if (jsonDocument.RootElement.ValueKind != JsonValueKind.Array)
{
throw new NotSupportedException("The source is not a JSON array.");
}
var jsonArray = JsonNode.Parse(jsonDocument.RootElement.GetRawText())!.AsArray();
var normalizedArray = NormalizeJsonArray(jsonArray, normalizationBehavior);
return JsonDocument.Parse(normalizedArray.ToJsonString());
}
/// <summary>
/// Normalizes an array of JSON objects so that each object contains all properties found in the array, including nested objects.
/// </summary>
internal static JsonArray NormalizeJsonArray(JsonArray jsonArray, NormalizationNonExistingPropertyBehavior normalizationBehavior)
{
if (jsonArray.Any(item => item != null && item.GetValueKind() != JsonValueKind.Object))
{
return jsonArray;
}
var schema = BuildSchema(jsonArray);
var normalizedArray = new JsonArray();
foreach (var item in jsonArray)
{
if (item is JsonObject obj)
{
var normalizedObj = NormalizeObject(obj, schema, normalizationBehavior);
normalizedArray.Add(normalizedObj);
}
}
return normalizedArray;
}
private static Dictionary<string, JsonValueInfo> BuildSchema(JsonArray array)
{
var schema = new Dictionary<string, JsonValueInfo>();
foreach (var item in array)
{
if (item is JsonObject obj)
{
MergeSchema(schema, obj);
}
}
return schema;
}
private static void MergeSchema(Dictionary<string, JsonValueInfo> schema, JsonObject obj)
{
foreach (var prop in obj)
{
if (prop.Value is JsonObject nested)
{
if (!schema.TryGetValue(prop.Key, out var jsonValueInfo))
{
jsonValueInfo = new JsonValueInfo(JsonValueKind.Object, new Dictionary<string, JsonValueInfo>());
schema[prop.Key] = jsonValueInfo;
}
MergeSchema((Dictionary<string, JsonValueInfo>)jsonValueInfo.Value!, nested);
}
else
{
if (!schema.ContainsKey(prop.Key))
{
schema[prop.Key] = new JsonValueInfo(prop.Value?.GetValueKind() ?? JsonValueKind.Null, null);
}
}
}
}
private static JsonObject NormalizeObject(JsonObject source, Dictionary<string, JsonValueInfo> schema, NormalizationNonExistingPropertyBehavior normalizationBehavior)
{
var result = new JsonObject();
foreach (var kvp in schema)
{
var key = kvp.Key;
var jType = kvp.Value;
if (jType.Value is Dictionary<string, JsonValueInfo> nestedSchema)
{
result[key] = source.ContainsKey(key) && source[key] is JsonObject jo ? NormalizeObject(jo, nestedSchema, normalizationBehavior) : CreateEmptyObject(nestedSchema, normalizationBehavior);
}
else
{
if (source.ContainsKey(key))
{
var value = source[key];
#if NET8_0_OR_GREATER
result[key] = value?.DeepClone();
#else
result[key] = value != null ? JsonNode.Parse(value.ToJsonString()) : null;
#endif
}
else
{
result[key] = GetDefaultOrNullValue(normalizationBehavior, jType);
}
}
}
return result;
}
private static JsonObject CreateEmptyObject(Dictionary<string, JsonValueInfo> schema, NormalizationNonExistingPropertyBehavior normalizationBehavior)
{
var obj = new JsonObject();
foreach (var kvp in schema)
{
var key = kvp.Key;
var jType = kvp.Value;
if (jType.Value is Dictionary<string, JsonValueInfo> nestedSchema)
{
obj[key] = CreateEmptyObject(nestedSchema, normalizationBehavior);
}
else
{
obj[key] = GetDefaultOrNullValue(normalizationBehavior, jType);
}
}
return obj;
}
private static JsonNode? GetDefaultValue(JsonValueInfo jType)
{
return jType.Type switch
{
JsonValueKind.Array => new JsonArray(),
JsonValueKind.False => false,
JsonValueKind.Number => default(int),
JsonValueKind.String => string.Empty,
JsonValueKind.True => false,
_ => GetNullValue(jType),
};
}
private static JsonValue? GetNullValue(JsonValueInfo jType)
{
return jType.Type switch
{
JsonValueKind.Array => null,
JsonValueKind.False => JsonValue.Create<bool?>(false),
JsonValueKind.Number => JsonValue.Create<int?>(null),
JsonValueKind.String => JsonValue.Create<string?>(null),
JsonValueKind.True => JsonValue.Create<bool?>(true),
_ => null,
};
}
private static JsonNode? GetDefaultOrNullValue(NormalizationNonExistingPropertyBehavior behavior, JsonValueInfo jType)
{
return behavior == NormalizationNonExistingPropertyBehavior.UseDefaultValue ? GetDefaultValue(jType) : GetNullValue(jType);
}
}