teragrep / cfe_31

0 stars 0 forks source link

Remove Gson marshalling completely #32

Open MoonBow-1 opened 6 months ago

MoonBow-1 commented 6 months ago

Description

Related to #22

Just going to remove all usages of gson.fromJson() method.

MoonBow-1 commented 6 months ago

This was started yesterday, will be done soon, only 2 more files to go

MoonBow-1 commented 6 months ago

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

MoonBow-1 commented 6 months ago

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.

MoonBow-1 commented 6 months ago

DiscrepancyReport.fromJson() before:

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 way:

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();
}
MoonBow-1 commented 6 months ago

~A new class, JsonIntegerStringObject, was created to deserialize Json maps with Interger keys and String values, used in InputTypes and Templates~

spoiler ```java public final class JsonIntegerStringObject { /** * JSON map as a JsonObject, should not include the actual field, only the values */ public final JsonObject jsonObject; public JsonIntegerStringObject(JsonObject jsonObject) { this.jsonObject = jsonObject; } public Map toMap() { return this.jsonObject .entrySet() .stream() .filter(entry -> entry.getValue().isJsonPrimitive()) .filter(entry -> entry.getValue().getAsJsonPrimitive().isString()) .collect( Collectors.toMap( entry -> Integer.parseInt(entry.getKey()), entry -> entry.getValue().getAsString() ) ); } @Override public boolean equals(Object o) { if (this == o) {return true;} if (!(o instanceof JsonIntegerStringObject)) {return false;} JsonIntegerStringObject that = (JsonIntegerStringObject) o; return Objects.equals(jsonObject, that.jsonObject); } @Override public int hashCode() { return jsonObject != null ? jsonObject.hashCode() : 0; } @Override public String toString() { return "JsonIntegerStringObject{" + "jsonObject=" + jsonObject + '}'; } } ``` Used in Templates: ```java final JsonObject templates = this.generateFileAsJson.get("templates").getAsJsonObject(); final JsonIntegerStringObject mapObject = new JsonIntegerStringObject(templates); final Map templateMap = mapObject.toMap(); ```
MoonBow-1 commented 6 months ago

Going to move away from the JsonIntegerStringObject