Json serialization improved

This commit is contained in:
2026-03-20 20:39:29 +01:00
parent 61accce977
commit a5885e1828
9 changed files with 134 additions and 119 deletions
@@ -1,5 +1,8 @@
package org.saturnclient.config.property; package org.saturnclient.config.property;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
public class BoolProperty extends Property { public class BoolProperty extends Property {
public boolean value; public boolean value;
public final boolean defaultValue; public final boolean defaultValue;
@@ -8,4 +11,15 @@ public class BoolProperty extends Property {
this.value = defaultValue; this.value = defaultValue;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
@Override
public JsonNode toJson() {
return JsonNodeFactory.instance.booleanNode(this.value);
}
@Override
public void loadFromJson(JsonNode element) {
if (element.isBoolean())
this.value = element.booleanValue();
}
} }
@@ -1,5 +1,8 @@
package org.saturnclient.config.property; package org.saturnclient.config.property;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
public class ColorProperty extends Property { public class ColorProperty extends Property {
public int value; public int value;
public final int defaultValue; public final int defaultValue;
@@ -9,6 +12,18 @@ public class ColorProperty extends Property {
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
@Override
public JsonNode toJson() {
return JsonNodeFactory.instance.textNode(
String.format("#%08X", this.value));
}
@Override
public void loadFromJson(JsonNode element) {
if (element.isTextual())
this.value = ColorProperty.parseHexToInt(element.textValue());
}
public static int parseHexToInt(String hex) { public static int parseHexToInt(String hex) {
hex = hex.replace("#", ""); hex = hex.replace("#", "");
@@ -1,5 +1,8 @@
package org.saturnclient.config.property; package org.saturnclient.config.property;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
public class FloatProperty extends Property { public class FloatProperty extends Property {
public float value; public float value;
public final float defaultValue; public final float defaultValue;
@@ -8,4 +11,15 @@ public class FloatProperty extends Property {
this.value = defaultValue; this.value = defaultValue;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
@Override
public JsonNode toJson() {
return JsonNodeFactory.instance.numberNode(this.value);
}
@Override
public void loadFromJson(JsonNode element) {
if (element.isNumber())
this.value = element.floatValue();
}
} }
@@ -1,5 +1,8 @@
package org.saturnclient.config.property; package org.saturnclient.config.property;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
public class IntProperty extends Property { public class IntProperty extends Property {
public int value; public int value;
public final int defaultValue; public final int defaultValue;
@@ -8,4 +11,17 @@ public class IntProperty extends Property {
this.value = defaultValue; this.value = defaultValue;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
@Override
public JsonNode toJson() {
return JsonNodeFactory.instance.numberNode(this.value);
}
@Override
public void loadFromJson(JsonNode element) {
if (element.isInt())
this.value = element.intValue();
else if (element.isTextual())
this.value = ColorProperty.parseHexToInt(element.textValue());
}
} }
@@ -2,6 +2,9 @@ package org.saturnclient.config.property;
import org.saturnclient.common.provider.Providers; import org.saturnclient.common.provider.Providers;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
public class KeybindingProperty extends Property { public class KeybindingProperty extends Property {
public int value; public int value;
public final int defaultValue; public final int defaultValue;
@@ -12,6 +15,17 @@ public class KeybindingProperty extends Property {
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
@Override
public JsonNode toJson() {
return JsonNodeFactory.instance.numberNode(this.value);
}
@Override
public void loadFromJson(JsonNode element) {
if (element.isInt())
this.value = element.intValue();
}
public boolean isKeyPressed() { public boolean isKeyPressed() {
return (Integer) value != -1 && return (Integer) value != -1 &&
@@ -2,6 +2,10 @@ package org.saturnclient.config.property;
import java.util.Map; import java.util.Map;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class NamespaceProperty extends Property { public class NamespaceProperty extends Property {
public final Map<String, Property> value; public final Map<String, Property> value;
@@ -9,6 +13,33 @@ public class NamespaceProperty extends Property {
this.value = value; this.value = value;
} }
@Override
public JsonNode toJson() {
ObjectNode nested = JsonNodeFactory.instance.objectNode();
this.value.forEach((k, v) -> {
nested.set(k, v.toJson());
});
return nested;
}
@Override
public void loadFromJson(JsonNode element) {
if (element.isObject()) {
ObjectNode obj = (ObjectNode) element;
this.value.forEach((k, v) -> {
JsonNode e = obj.get(k);
if (e == null)
return;
v.loadFromJson(e);
});
}
}
public Map<String, Property> getNamespaceMap() { public Map<String, Property> getNamespaceMap() {
return this.value; return this.value;
} }
@@ -1,26 +1,10 @@
package org.saturnclient.config.property; package org.saturnclient.config.property;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.Map; import java.util.Map;
public abstract class Property { public abstract class Property {
public enum PropertyType {
BOOLEAN,
INTEGER,
FLOAT,
STRING,
HEX,
NAMESPACE,
SELECT,
KEYBINDING,
}
public boolean isReset = false;
// ---------- Factory Methods ---------- // ---------- Factory Methods ----------
public static NamespaceProperty namespace(Map<String, Property> value) { public static NamespaceProperty namespace(Map<String, Property> value) {
@@ -66,112 +50,11 @@ public abstract class Property {
} }
public void reset() { public void reset() {
isReset = true;
} }
// ---------- JSON Serialization ---------- // ---------- JSON Serialization ----------
public JsonNode toJson() { public abstract JsonNode toJson();
if (this instanceof BoolProperty p) { public abstract void loadFromJson(JsonNode element);
return JsonNodeFactory.instance.booleanNode(p.value);
}
if (this instanceof IntProperty p) {
return JsonNodeFactory.instance.numberNode(p.value);
}
if (this instanceof FloatProperty p) {
return JsonNodeFactory.instance.numberNode(p.value);
}
if (this instanceof StringProperty p) {
return JsonNodeFactory.instance.textNode(p.value);
}
if (this instanceof ColorProperty p) {
return JsonNodeFactory.instance.textNode(
String.format("#%08X", p.value));
}
if (this instanceof KeybindingProperty p) {
return JsonNodeFactory.instance.numberNode(p.value);
}
if (this instanceof SelectProperty p) {
return JsonNodeFactory.instance.numberNode(p.value);
}
if (this instanceof NamespaceProperty p) {
ObjectNode nested = JsonNodeFactory.instance.objectNode();
p.value.forEach((k, v) -> {
nested.set(k, v.toJson());
});
return nested;
}
return null;
}
public void loadFromJson(JsonNode element) {
if (element == null)
return;
if (this instanceof BoolProperty p) {
if (element.isBoolean())
p.value = element.booleanValue();
return;
}
if (this instanceof IntProperty p) {
if (element.isInt())
p.value = element.intValue();
else if (element.isTextual())
p.value = ColorProperty.parseHexToInt(element.textValue());
return;
}
if (this instanceof FloatProperty p) {
if (element.isNumber())
p.value = element.floatValue();
return;
}
if (this instanceof StringProperty p) {
if (element.isTextual())
p.value = element.textValue();
return;
}
if (this instanceof ColorProperty p) {
if (element.isTextual())
p.value = ColorProperty.parseHexToInt(element.textValue());
return;
}
if (this instanceof NamespaceProperty p) {
if (element.isObject()) {
ObjectNode obj = (ObjectNode) element;
p.value.forEach((k, v) -> {
v.loadFromJson(obj.get(k));
});
}
return;
}
if (this instanceof SelectProperty p) {
if (element.isInt())
p.setSelection(element.intValue());
return;
}
if (this instanceof KeybindingProperty p) {
if (element.isInt())
p.value = element.intValue();
}
}
} }
@@ -1,5 +1,8 @@
package org.saturnclient.config.property; package org.saturnclient.config.property;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
public class SelectProperty extends Property { public class SelectProperty extends Property {
public int value; public int value;
public final int defaultValue; public final int defaultValue;
@@ -11,6 +14,17 @@ public class SelectProperty extends Property {
this.availableValues = availableValues; this.availableValues = availableValues;
} }
@Override
public JsonNode toJson() {
return JsonNodeFactory.instance.numberNode(this.value);
}
@Override
public void loadFromJson(JsonNode element) {
if (element.isInt())
this.setSelection(element.intValue());
}
public void next() { public void next() {
int i = (Integer) value; int i = (Integer) value;
this.value = (i < availableValues.length - 1) ? i + 1 : 0; this.value = (i < availableValues.length - 1) ? i + 1 : 0;
@@ -1,5 +1,8 @@
package org.saturnclient.config.property; package org.saturnclient.config.property;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
public class StringProperty extends Property { public class StringProperty extends Property {
public String value; public String value;
public final String defaultValue; public final String defaultValue;
@@ -8,4 +11,15 @@ public class StringProperty extends Property {
this.value = defaultValue; this.value = defaultValue;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
@Override
public JsonNode toJson() {
return JsonNodeFactory.instance.textNode(this.value);
}
@Override
public void loadFromJson(JsonNode element) {
if (element.isTextual())
this.value = element.textValue();
}
} }