-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
208 lines (183 loc) · 5.98 KB
/
Program.cs
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
using System.Runtime.InteropServices;
using Newtonsoft.Json.Linq;
namespace servecoin
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[DllImport("kernel32.dll")]
private static extern bool AllocConsole();
[DllImport("kernel32.dll")]
private static extern bool FreeConsole();
[STAThread]
static void Main(string[] args)
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
new JsonFileManager();
}
}
public class JsonFileManager
{
private readonly string _filePath;
public JsonFileManager()
{
_filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data.json");
if (!File.Exists(_filePath))
{
var initialContent = new JObject();
File.WriteAllText(_filePath, initialContent.ToString(Newtonsoft.Json.Formatting.Indented));
this.SetNestedValue("piggy.goals", new JArray { });
this.SetNestedValue("language", "en");
}
}
public void AddToArrayByPath(string arrayPath, JToken newValue)
{
var json = ReadJson();
var tokens = arrayPath.Split('.');
JToken current = json;
foreach (var token in tokens)
{
if (current[token] != null)
{
current = current[token];
}
else
{
Console.WriteLine($"Ìàñèâ çà øëÿõîì {arrayPath} íå çíàéäåíî.");
return;
}
}
if (current is JArray array)
{
array.Add(newValue);
WriteJson(json);
}
else
{
Console.WriteLine($"Øëÿõ {arrayPath} íå º ìàñèâîì.");
}
}
public JObject ReadJson()
{
try
{
var jsonContent = File.ReadAllText(_filePath);
return JObject.Parse(jsonContent);
}
catch (Exception ex)
{
Console.WriteLine($"Ïîìèëêà ÷èòàííÿ JSON-ôàéëó: {ex.Message}");
return new JObject();
}
}
public void WriteJson(JObject content)
{
try
{
File.WriteAllText(_filePath, content.ToString(Newtonsoft.Json.Formatting.Indented));
}
catch (Exception ex)
{
Console.WriteLine($"Ïîìèëêà çàïèñó â JSON-ôàéë: {ex.Message}");
}
}
public JToken GetNestedValue(string path)
{
var json = ReadJson();
var tokens = path.Split('.');
JToken current = json;
foreach (var token in tokens)
{
if (current[token] != null)
{
current = current[token];
}
else
{
return null;
}
}
return current;
}
public void SetNestedValue(string path, JToken value)
{
var json = ReadJson();
var tokens = path.Split('.');
JToken current = json;
for (int i = 0; i < tokens.Length; i++)
{
var token = tokens[i];
if (i == tokens.Length - 1) // Îñòàíí³é åëåìåíò
{
if (current is JObject obj)
{
obj[token] = value;
}
else if (current is JArray array) // ßêùî ïîòî÷íèé åëåìåíò ìàñèâ
{
int index = int.Parse(token); // Îòðèìóºìî ³íäåêñ åëåìåíòà ìàñèâó
if (array.Count > index)
{
array[index] = value;
}
else
{
// ßêùî ³íäåêñ á³ëüøå, í³æ ³ñíóþ÷³ åëåìåíòè, äîäàºìî íîâèé åëåìåíò
array.Add(value);
}
}
}
else
{
if (current[token] == null)
{
if (current is JObject obj)
{
obj[token] = new JObject();
}
else if (current is JArray array)
{
array.Add(new JObject()); // ßêùî öå ìàñèâ, äîäàºìî îá'ºêò
}
}
current = current[token];
}
}
WriteJson(json);
}
// Äîäàòêîâ³ ìåòîäè äëÿ ðîáîòè ç ìàñèâàìè
public void AddToArray(string arrayPath, JToken newValue)
{
var json = ReadJson();
var tokens = arrayPath.Split('.');
JToken current = json;
// Ïðîõîäèìî äî ïîòð³áíîãî ìàñèâó
foreach (var token in tokens)
{
if (current[token] != null)
{
current = current[token];
}
else
{
Console.WriteLine($"Ìàñèâ çà øëÿõîì {arrayPath} íå çíàéäåíî.");
return;
}
}
if (current is JArray array)
{
array.Add(newValue); // Äîäàºìî íîâèé åëåìåíò äî ìàñèâó
}
else
{
Console.WriteLine($"Øëÿõ {arrayPath} íå º ìàñèâîì.");
}
WriteJson(json);
}
}
}