-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonConfigParser.cs
More file actions
215 lines (176 loc) · 7.27 KB
/
JsonConfigParser.cs
File metadata and controls
215 lines (176 loc) · 7.27 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using ConfigMerger.Parsers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConfigMerger
{
public class JsonConfigParser : IConfigParser
{
public Dictionary<string, string> ParseConfig(string content)
{
var config = new Dictionary<string, string>();
try
{
var jsonObject = JToken.Parse(content);
ProcessJsonToken(jsonObject, "", config);
}
catch (JsonException ex)
{
throw new InvalidOperationException($"Ошибка парсинга JSON: {ex.Message}", ex);
}
return config;
}
private void ProcessJsonToken(JToken token, string prefix, Dictionary<string, string> config)
{
switch (token.Type)
{
case JTokenType.Object:
var obj = (JObject)token;
foreach (var property in obj.Properties())
{
string key = string.IsNullOrEmpty(prefix) ? property.Name : $"{prefix}.{property.Name}";
ProcessJsonToken(property.Value, key, config);
}
break;
case JTokenType.Array:
var array = (JArray)token;
for (int i = 0; i < array.Count; i++)
{
string key = $"{prefix}[{i}]";
ProcessJsonToken(array[i], key, config);
}
break;
case JTokenType.String:
config[prefix] = token.Value<string>() ?? "";
break;
case JTokenType.Integer:
case JTokenType.Float:
config[prefix] = token.ToString();
break;
case JTokenType.Boolean:
config[prefix] = token.Value<bool>().ToString().ToLower();
break;
case JTokenType.Null:
config[prefix] = "null";
break;
case JTokenType.Date:
config[prefix] = token.Value<DateTime>().ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
break;
default:
config[prefix] = token.ToString();
break;
}
}
public string GenerateMergedConfig(Dictionary<string, string> merged, string originalContent)
{
try
{
var jsonObject = BuildJsonObject(merged);
return JsonConvert.SerializeObject(jsonObject, Formatting.Indented);
}
catch (Exception ex)
{
throw new InvalidOperationException($"Ошибка генерации JSON: {ex.Message}", ex);
}
}
private JToken BuildJsonObject(Dictionary<string, string> config)
{
var result = new JObject();
foreach (var kvp in config.OrderBy(x => x.Key))
{
SetNestedValue(result, kvp.Key, ParseValue(kvp.Value));
}
return result;
}
private void SetNestedValue(JObject obj, string path, JToken value)
{
var parts = path.Split('.');
JToken current = obj;
for (int i = 0; i < parts.Length - 1; i++)
{
var part = parts[i];
if (part.Contains("[") && part.Contains("]"))
{
current = ProcessArrayNavigation(current, part, i == parts.Length - 2 ? parts[i + 1] : null, value);
if (i == parts.Length - 2) return;
}
else
{
var currentObj = (JObject)current;
if (currentObj[part] == null)
currentObj[part] = new JObject();
current = currentObj[part];
}
}
var lastKey = parts[parts.Length - 1];
if (lastKey.Contains("[") && lastKey.Contains("]"))
{
ProcessArrayAssignment(current, lastKey, value);
}
else
{
var currentObj = (JObject)current;
currentObj[lastKey] = value;
}
}
private JToken ProcessArrayNavigation(JToken current, string arrayPart, string nextPart, JToken value)
{
var arrayKey = arrayPart.Substring(0, arrayPart.IndexOf('['));
var arrayIndex = int.Parse(arrayPart.Substring(arrayPart.IndexOf('[') + 1, arrayPart.IndexOf(']') - arrayPart.IndexOf('[') - 1));
var currentObj = (JObject)current;
if (currentObj[arrayKey] == null)
currentObj[arrayKey] = new JArray();
var array = (JArray)currentObj[arrayKey];
while (array.Count <= arrayIndex)
array.Add(new JObject());
if (nextPart != null && nextPart.Contains("["))
{
var nestedKey = nextPart.Substring(0, nextPart.IndexOf('['));
var nestedIndex = int.Parse(nextPart.Substring(nextPart.IndexOf('[') + 1, nextPart.IndexOf(']') - nextPart.IndexOf('[') - 1));
var nestedObj = (JObject)array[arrayIndex];
if (nestedObj[nestedKey] == null)
nestedObj[nestedKey] = new JArray();
var nestedArray = (JArray)nestedObj[nestedKey];
while (nestedArray.Count <= nestedIndex)
nestedArray.Add(JValue.CreateNull());
nestedArray[nestedIndex] = value;
}
else if (nextPart != null)
{
var nestedObj = (JObject)array[arrayIndex];
nestedObj[nextPart] = value;
}
return array[arrayIndex];
}
private void ProcessArrayAssignment(JToken current, string arrayKey, JToken value)
{
var keyName = arrayKey.Substring(0, arrayKey.IndexOf('['));
var index = int.Parse(arrayKey.Substring(arrayKey.IndexOf('[') + 1, arrayKey.IndexOf(']') - arrayKey.IndexOf('[') - 1));
var currentObj = (JObject)current;
if (currentObj[keyName] == null)
currentObj[keyName] = new JArray();
var array = (JArray)currentObj[keyName];
while (array.Count <= index)
array.Add(JValue.CreateNull());
array[index] = value;
}
private JToken ParseValue(string value)
{
if (string.IsNullOrEmpty(value))
return JValue.CreateString("");
if (int.TryParse(value, out int intValue))
return new JValue(intValue);
if (double.TryParse(value, out double doubleValue))
return new JValue(doubleValue);
if (bool.TryParse(value, out bool boolValue))
return new JValue(boolValue);
if (value.Equals("null", StringComparison.OrdinalIgnoreCase))
return JValue.CreateNull();
if (DateTime.TryParse(value, out DateTime dateValue))
return new JValue(dateValue);
return new JValue(value);
}
}
}