Reworked the JSON serialisation

This commit is contained in:
2023-05-11 14:23:44 +02:00
parent 57028f1fc9
commit 025c2f8d61
6 changed files with 219 additions and 191 deletions
+54 -13
View File
@@ -12,6 +12,8 @@ using System.Text;
using System.Threading.Tasks;
using Tesseract;
using System.IO;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace Test_Merge
{
@@ -50,25 +52,64 @@ namespace Test_Merge
{
string JSON = "";
JSON += "{" + Environment.NewLine;
JSON += MainZone.ToJSON() + "," + Environment.NewLine;
JSON += "\"Drivers\":[" + Environment.NewLine;
JsonObject jsonFileObject = new JsonObject();
for (int i = 0; i < drivers.Count; i++)
//Creating the mainZone object
JsonObject mainZoneObject = new JsonObject();
mainZoneObject.Add("x",MainZone.Bounds.X);
mainZoneObject.Add("y",MainZone.Bounds.Y);
mainZoneObject.Add("width",MainZone.Bounds.Width);
mainZoneObject.Add("height",MainZone.Bounds.Height);
JsonArray driverZonesArray = new JsonArray();
int DriverID = 0;
foreach (Zone driverZone in MainZone.Zones)
{
JSON += "\"" + drivers[i] + "\"";
if (i < drivers.Count - 1)
JSON += ",";
JSON += Environment.NewLine;
DriverID++;
JsonObject driverZoneObject = new JsonObject();
driverZoneObject.Add("name","Driver"+DriverID);
driverZoneObject.Add("x", driverZone.Bounds.X);
driverZoneObject.Add("y", driverZone.Bounds.Y);
driverZoneObject.Add("width", driverZone.Bounds.Width);
driverZoneObject.Add("height", driverZone.Bounds.Height);
JsonArray windowsArray = new JsonArray();
JsonObject windowObject = new JsonObject();
foreach (Window window in driverZone.Windows)
{
windowObject.Add(window.Name, new JsonObject {
{ "x", window.Bounds.X },
{ "y", window.Bounds.Y },
{ "width", window.Bounds.Width },
{ "height", window.Bounds.Height }
});
}
windowsArray.Add(windowObject);
driverZoneObject.Add("Windows",windowsArray);
driverZonesArray.Add(driverZoneObject);
}
JSON += "]" + Environment.NewLine;
mainZoneObject.Add("DriverZones",driverZonesArray);
JSON += "}";
JsonArray driversArray = new JsonArray();
if (!Directory.Exists(CONFIGS_FOLDER_NAME))
Directory.CreateDirectory(CONFIGS_FOLDER_NAME);
foreach (string driver in drivers)
{
driversArray.Add(driver);
}
mainZoneObject.Add("Drivers",driversArray);
jsonFileObject.Add("Main",mainZoneObject);
JSON = jsonFileObject.ToString();
//Saving the file
string path = CONFIGS_FOLDER_NAME + configName;
if (File.Exists(path + ".json"))
@@ -204,7 +245,7 @@ namespace Test_Merge
Zone driverZone = new Zone(MainZone.ZoneImage, windowRectangle, "DriverZone");
MainZone.AddZone(driverZone);
driverZone.ZoneImage.Save("Driver" + i+".png");
//driverZone.ZoneImage.Save("Driver" + i+".png");
i++;
}
}