Open MoonBow-1 opened 6 months ago
This was started yesterday, will be done soon, only 2 more files to go
Actually there is 2 more files to change, that being Rule
and Omrelp
.
Rules are used in a map and a List, requiring their own Json class
Marshalling has been removed from these files, and tests added for their methods.
Had to test the general flow multiple times because the first run had one HostGroup missing, but that didn't happen on the subsequent ones had all of them.
public void fromJson(final JsonObject jsonObject) {
if (jsonObject.has("discrepancies")) {
final GsonUtility gsonUtility = new GsonUtility();
final DiscrepancyReport discrepancyReport =
gsonUtility.gson.fromJson(
jsonObject,
DiscrepancyReport.class
);
discrepancies.addAll(discrepancyReport.discrepancies);
}
}
with its usage:
final GsonUtility gsonUtility = new GsonUtility();
final JsonElement oldDataAsJSON =
JsonParser.parseReader(
Files.newBufferedReader(discrepancyReportFile.file.toPath())
);
final DiscrepancyReport oldDiscrepancyReport = new DiscrepancyReport();
if (oldDataAsJSON != null && !oldDataAsJSON.isJsonNull()) {
oldDiscrepancyReport.fromJson(oldDataAsJSON.getAsJsonObject());
}
New JsonDiscrepancyReport class:
public final class JsonDiscrepancyReport {
public final JsonObject jsonObject;
public JsonDiscrepancyReport(JsonObject jsonObject) {
this.jsonObject = jsonObject;
}
public DiscrepancyReport toDiscrepancyReport() {
if (jsonObject.has("discrepancies")) {
final JsonArray discrepancyArray = jsonObject.get("discrepancies").getAsJsonArray();
final Set<DiscrepancyRecord> discrepancyRecords = deserializeDiscrepancyRecords(
discrepancyArray
);
return new DiscrepancyReport(discrepancyRecords);
} else {
return new DiscrepancyReport();
}
}
public Set<DiscrepancyRecord> deserializeDiscrepancyRecords(final JsonArray jsonArray) {
final Set<DiscrepancyRecord> discrepancyRecords = new HashSet<>();
jsonArray
.forEach(
entry -> {
final JsonObject discrepancyRecordAsJson = entry.getAsJsonObject();
discrepancyRecords.add(
new DiscrepancyRecord(
discrepancyRecordAsJson.get("masterPath").getAsString(),
discrepancyRecordAsJson.get("archivePath").getAsString()
)
);
}
);
return discrepancyRecords;
}
}
With its usage:
final DiscrepancyReport oldDiscrepancyReport;
if (oldDataAsJSON != null && !oldDataAsJSON.isJsonNull()) {
final JsonDiscrepancyReport jsonDiscrepancyReport = new JsonDiscrepancyReport(
oldDataAsJSON.getAsJsonObject()
);
oldDiscrepancyReport = jsonDiscrepancyReport.toDiscrepancyReport();
} else {
oldDiscrepancyReport = new DiscrepancyReport();
}
~A new class, JsonIntegerStringObject, was created to deserialize Json maps with Interger keys and String values, used in InputTypes
and Templates
~
Going to move away from the JsonIntegerStringObject
Description
Related to #22
Just going to remove all usages of
gson.fromJson()
method.