teragrep / cfe_31

0 stars 0 forks source link

Remove Gson dependency for Jakarta JSON API #47

Closed MoonBow-1 closed 3 months ago

MoonBow-1 commented 4 months ago

Description

Going to switch from using Gson to the Jakarta JSON API (previously known as javax.json API)

MoonBow-1 commented 3 months ago

So the Jakarta JSON API doesn't allow for adding null values to an object or an array with the add() method, but requires the use of the addNull() method.

This currently results in either if-else statements for each value that is needed to be added to an Object-/ArrayBuilder, or in an additional class that handles calls to the Builder methods and decides which method to use:

public final class NullSafeJsonObjectBuilder implements JsonObjectBuilder {
  private final JsonObjectBuilder builder;

  public JsonObjectBuilder add(String name, JsonValue value) {
      builder.add(name, (value == null) ? JsonValue.NULL : value);
      return this;
  }

  @Override
  public JsonObjectBuilder add(String name, String value) {
      if (value == null) {
          builder.addNull(name);
      } else {
          builder.add(name, value);
      }
      return this;
  }
...
}

allowing the methods to be called as with the original implementation of the Jakarta JSON API:

public JsonObject toJsonObject() {
    NullSafeJsonObjectBuilder nullSafeJsonObjectBuilder = new NullSafeJsonObjectBuilder();
    return nullSafeJsonObjectBuilder
        .add("os", this.os)
        .add("arch", this.arch)
        .add("release_version", this.release)
        .add("flavor", this.flavor)
        .add("hostname", this.hostName)
        .add("host_id", this.hostID)
        .build();
}
MoonBow-1 commented 3 months ago

Currently approx. 20 tests are failing still, mostly due to some String representation mismatches between Jakarta and Gson.

MoonBow-1 commented 3 months ago

All tests are now passing, but there is a need to write new ones for the Jakarta JSON API behavior

MoonBow-1 commented 3 months ago

The JSON API doesn't allow the unpacking of Integer variables without a NullPointerException, so the toJsonObject() methods need to have if-else statements to check for null Integers manually.

if (this.inputValue == null) {
    nullSafeJsonObjectBuilder.addNull("inputvalue");
} else {
    nullSafeJsonObjectBuilder.add("inputvalue", this.inputValue);
}
MoonBow-1 commented 3 months ago

All Exceptions have now been fixed, the application runs without any problems, but found a problem with Hosts' IP addresses, investigating why the field is reported as null when it gets to CFE-18

MoonBow-1 commented 3 months ago

All Exceptions have now been fixed, the application runs without any problems, but found a problem with Hosts' IP addresses, investigating why the field is reported as null when it gets to CFE-18

Turned out to be a wrongly named field for the ipAddress

MoonBow-1 commented 3 months ago

The weird linkage behavior is now diagnosed, CFE-18 returns an ID lower than the "max" for the last linkage that is added when running the program. (Example, largest ID would be 5, last linkage returns 4 as a valid ID)

This is ok behavior according to @Nefarious46, so I'm going to verify insertions completely compared to the main branch version.