-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonParser.cs
98 lines (86 loc) · 3.45 KB
/
JsonParser.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
using System.Collections.Generic;
using System.IO;
using WiFiMapDataImporter.DTO;
using Newtonsoft.Json.Linq;
namespace WiFiMapDataImporter;
public static class JsonParser
{
public static Network[] ParseWiFiMapData(string[] filePaths)
{
JObject jsonObject = ReadAndMerge(filePaths);
var networks = new List<Network>();
foreach (var networkEntry in jsonObject)
{
var network = new Network(networkEntry.Key)
{
Bssid = networkEntry.Key,
Ssid = networkEntry.Value!["ssid"]?.ToString(),
Frequency = networkEntry.Value!["frequency"]!.ToObject<int>(),
Capabilities = networkEntry.Value["capabilities"]!.ToString(),
};
var levels = new List<Network.Level>();
if (networkEntry.Value["levels"] is JObject levelsObject)
{
foreach (var levelEntry in levelsObject)
{
var coordinates = levelEntry.Key.Split(',');
if (coordinates.Length == 2 &&
double.TryParse(coordinates[0], out var latitude) &&
double.TryParse(coordinates[1], out var longitude))
{
levels.Add(new Network.Level
{
Latitude = latitude,
Longitude = longitude,
Power = levelEntry.Value.ToObject<int>()
});
}
}
}
network.Levels = levels.ToArray();
networks.Add(network);
}
return networks.ToArray();
}
private static JObject ReadAndMerge(string[] filePaths)
{
string json = File.ReadAllText(filePaths[0]);
JObject jsonObject = JObject.Parse(json);
for (int i = 1; i < filePaths.Length; i++)
{
string nextJson = File.ReadAllText(filePaths[i]);
JObject nextJsonObject = JObject.Parse(nextJson);
foreach (var nextProperty in nextJsonObject.Properties())
{
string key = nextProperty.Name;
if (jsonObject.TryGetValue(key, out var value))
{
var currentValue = value as JObject;
var newValue = nextJsonObject[key] as JObject;
if (currentValue != null && newValue != null)
{
foreach (var newProperty in newValue.Properties())
{
if (newProperty.Value.HasValues || !string.IsNullOrEmpty(newProperty.Value.ToString()))
{
currentValue[newProperty.Name] = newProperty.Value;
}
}
if (currentValue["levels"] is JObject existingLevels && newValue["levels"] is JObject newLevels)
{
foreach (var level in newLevels.Properties())
{
existingLevels[level.Name] = level.Value;
}
}
}
}
else
{
jsonObject[key] = nextProperty.Value;
}
}
}
return jsonObject;
}
}