Skip to content

Commit

Permalink
Validate numeric values when serializing in loadables
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed May 17, 2024
1 parent aeb31a9 commit ead3b12
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ public static FloatLoadable min(float min) {
return new FloatLoadable(min, Float.POSITIVE_INFINITY);
}

@Override
public Float convert(JsonElement element, String key) {
float value = GsonHelper.convertToFloat(element, key);
// note to prevent NaN we want a condition where true passes rather than false passes, as NaN is always false on these compares
protected float validate(float value, String key) {
if (min <= value && value <= max) {
return value;
}
Expand All @@ -46,14 +43,19 @@ public Float convert(JsonElement element, String key) {
throw new JsonSyntaxException(key + " must be between " + min + " and " + max);
}

@Override
public Float convert(JsonElement element, String key) {
return validate(GsonHelper.convertToFloat(element, key), key);
}

@Override
public Float decode(FriendlyByteBuf buffer) {
return buffer.readFloat();
}

@Override
public JsonElement serialize(Float object) {
return new JsonPrimitive(object);
return new JsonPrimitive(validate(object, "Value"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Integer convert(JsonElement element, String key) {

@Override
public JsonElement serialize(Integer value) {
return new JsonPrimitive(value);
return new JsonPrimitive(validate(value, "Value"));
}


Expand Down

0 comments on commit ead3b12

Please sign in to comment.