Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,17 @@ static <T> GsonConfigSerializerBuilder<T> create(ConfigClassHandler<T> config) {
*/
GsonConfigSerializerBuilder<T> setJson5(boolean json5);


/**
* If disabled, writes colors to the json in decimal ({@code -1610612736}).
* If enabled with JSON5 ({@link #setJson5(boolean)}), writes colors to the json as hexadecimal literals ({@code 0xA0000000}).
* If enabled without JSON5, writes colors to the json as hexadecimal strings ({@code "0xA0000000"}).
* Colors are saved in ARGB.
*
* @param hexColor whether to store color in hexadecimal instead of decimal
* @return this builder
*/
GsonConfigSerializerBuilder<T> setStoreColorsAsHex(boolean hexColor);

ConfigSerializer<T> build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ public class GsonConfigSerializer<T> extends ConfigSerializer<T> {
private final Gson gson;
private final Path path;
private final boolean json5;
private final boolean hexColor;

private GsonConfigSerializer(ConfigClassHandler<T> config, Path path, Gson gson, boolean json5) {
private GsonConfigSerializer(ConfigClassHandler<T> config, Path path, Gson gson, boolean json5, boolean hexColor) {
super(config);
this.gson = gson;
this.path = path;
this.json5 = json5;
this.hexColor = hexColor;
}

@Override
Expand All @@ -52,7 +54,13 @@ public void save() {

try (StringWriter stringWriter = new StringWriter()) {
JsonWriter jsonWriter = json5 ? JsonWriter.json5(stringWriter) : JsonWriter.json(stringWriter);
GsonWriter gsonWriter = new GsonWriter(jsonWriter);
GsonWriter gsonWriter;
if (!hexColor)
gsonWriter = new GsonWriter(jsonWriter);
else if (json5)
gsonWriter = new HexColorLiteralGsonWriter(jsonWriter);
else
gsonWriter = new HexColorStringGsonWriter(jsonWriter);

jsonWriter.beginObject();

Expand Down Expand Up @@ -192,12 +200,21 @@ public JsonElement serialize(Style src, Type typeOfSrc, JsonSerializationContext
public static class ColorTypeAdapter implements JsonSerializer<Color>, JsonDeserializer<Color> {
@Override
public Color deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
return new Color(jsonElement.getAsInt(), true);
String value = jsonElement.getAsString();

int argb;
if (value.startsWith("0x") || value.startsWith("0X"))
// Parse JSON5 hex literal or hex string
argb = (int) Long.parseLong(value.substring(2), 16);
else
argb = jsonElement.getAsInt();

return new Color(argb, true);
}

@Override
public JsonElement serialize(Color color, Type type, JsonSerializationContext jsonSerializationContext) {
return new JsonPrimitive(color.getRGB());
return new JsonPrimitive(new HexColorNumber(color.getRGB()));
}
}

Expand All @@ -218,6 +235,7 @@ public static class Builder<T> implements GsonConfigSerializerBuilder<T> {
private final ConfigClassHandler<T> config;
private Path path;
private boolean json5;
private boolean hexColor;
private UnaryOperator<GsonBuilder> gsonBuilder = builder -> builder
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.serializeNulls()
Expand Down Expand Up @@ -261,9 +279,15 @@ public Builder<T> setJson5(boolean json5) {
return this;
}

@Override
public GsonConfigSerializerBuilder<T> setStoreColorsAsHex(boolean hexColor) {
this.hexColor = hexColor;
return this;
}

@Override
public GsonConfigSerializer<T> build() {
return new GsonConfigSerializer<>(config, path, gsonBuilder.apply(new GsonBuilder()).create(), json5);
return new GsonConfigSerializer<>(config, path, gsonBuilder.apply(new GsonBuilder()).create(), json5, hexColor);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.isxander.yacl3.config.v2.impl.serializer;

import org.quiltmc.parsers.json.JsonWriter;
import org.quiltmc.parsers.json.gson.GsonWriter;

import java.io.IOException;

public class HexColorLiteralGsonWriter extends GsonWriter {
public HexColorLiteralGsonWriter(JsonWriter writer) {
super(writer);
}

@Override
public com.google.gson.stream.JsonWriter value(Number value) throws IOException {
return value instanceof HexColorNumber hexColorNumber
? jsonValue(hexColorNumber.hex())
: super.value(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dev.isxander.yacl3.config.v2.impl.serializer;

import java.util.HexFormat;

public class HexColorNumber extends Number {
private static final HexFormat HEX_FORMAT = HexFormat.of().withUpperCase();
protected final int value;

public HexColorNumber(int value) {
this.value = value;
}

public String hex() {
return "0x" + HEX_FORMAT.toHexDigits(value);
}

@Override
public int intValue() {
return value;
}

@Override
public long longValue() {
return value;
}

@Override
public float floatValue() {
return value;
}

@Override
public double doubleValue() {
return value;
}

@Override
public int hashCode() {
return Integer.hashCode(value);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof HexColorNumber other)) return false;
return value == other.value;
}

@Override
public String toString() {
return String.valueOf(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.isxander.yacl3.config.v2.impl.serializer;

import org.quiltmc.parsers.json.JsonWriter;
import org.quiltmc.parsers.json.gson.GsonWriter;

import java.io.IOException;

public class HexColorStringGsonWriter extends GsonWriter {
public HexColorStringGsonWriter(JsonWriter writer) {
super(writer);
}

@Override
public com.google.gson.stream.JsonWriter value(Number value) throws IOException {
return value instanceof HexColorNumber hexColorNumber
? value(hexColorNumber.hex())
: super.value(value);
}
}
1 change: 1 addition & 0 deletions src/main/java/dev/isxander/yacl3/platform/YACLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class YACLConfig {
.serializer(config -> GsonConfigSerializerBuilder.create(config)
.setPath(YACLPlatform.getConfigDir().resolve("yacl.json5"))
.setJson5(true)
.setStoreColorsAsHex(true)
.build())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class AutogenConfigTest {
.serializer(config -> GsonConfigSerializerBuilder.create(config)
.setPath(YACLPlatform.getConfigDir().resolve("yacl-test-v2.json5"))
.setJson5(true)
.setStoreColorsAsHex(true)
.build())
.build();

Expand Down
1 change: 1 addition & 0 deletions src/testmod/java/dev/isxander/yacl3/test/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ConfigTest {
public static final ConfigClassHandler<ConfigTest> GSON = ConfigClassHandler.createBuilder(ConfigTest.class)
.serializer(config -> GsonConfigSerializerBuilder.create(config)
.setPath(YACLPlatform.getConfigDir().resolve("yacl-test.json"))
.setStoreColorsAsHex(true)
.build())
.build();

Expand Down