diff --git a/http-api/src/main/java/net/runelite/http/api/gson/ColorTypeAdapter.java b/http-api/src/main/java/net/runelite/http/api/gson/ColorTypeAdapter.java index 6f4b003df0..feec0f6d66 100644 --- a/http-api/src/main/java/net/runelite/http/api/gson/ColorTypeAdapter.java +++ b/http-api/src/main/java/net/runelite/http/api/gson/ColorTypeAdapter.java @@ -43,10 +43,7 @@ public class ColorTypeAdapter extends TypeAdapter } int rgba = value.getRGB(); - out.beginObject() - .name("value") - .value(rgba) - .endObject(); + out.value(String.format("#%08X", rgba)); } @Override @@ -57,7 +54,18 @@ public class ColorTypeAdapter extends TypeAdapter case NULL: in.nextNull(); return null; + case STRING: + { + String value = in.nextString(); + if (value.charAt(0) == '#') + { + value = value.substring(1); + } + int intValue = Integer.parseUnsignedInt(value, 16); + return new Color(intValue, true); + } case BEGIN_OBJECT: + { in.beginObject(); double value = 0; while (in.peek() != JsonToken.END_OBJECT) @@ -74,6 +82,7 @@ public class ColorTypeAdapter extends TypeAdapter } in.endObject(); return new Color((int) value, true); + } } return null; // throws } diff --git a/http-api/src/test/java/net/runelite/http/api/gson/ColorTypeAdapterTest.java b/http-api/src/test/java/net/runelite/http/api/gson/ColorTypeAdapterTest.java index d7f5e6fd4c..0fe0b2226a 100644 --- a/http-api/src/test/java/net/runelite/http/api/gson/ColorTypeAdapterTest.java +++ b/http-api/src/test/java/net/runelite/http/api/gson/ColorTypeAdapterTest.java @@ -34,17 +34,24 @@ public class ColorTypeAdapterTest @Test public void test() { - test("null", null); - test("{\"value\":-13347208,\"falpha\":0.0}", new Color(0x12345678, false)); - test("{\"value\":305419896,\"falpha\":0.0}", new Color(0x12345678, true)); - test("{\"value\":-1.4221317E7,\"falpha\":0.0}", new Color(0xFF26FFFB, true)); + test("null", null, true); + test("{\"value\":-13347208,\"falpha\":0.0}", new Color(0x12345678, false), false); + test("{\"value\":305419896,\"falpha\":0.0}", new Color(0x12345678, true), false); + test("{\"value\":-1.4221317E7,\"falpha\":0.0}", new Color(0xFF26FFFB, true), false); + test("\"#FF345678\"", new Color(0x12345678, false), true); + test("\"#12345678\"", new Color(0x12345678, true), true); + test("\"#FF26FFFB\"", new Color(0xFF26FFFB, true), true); } - private void test(String json, Color object) + private void test(String json, Color object, boolean exactEncoding) { Color parsed = RuneLiteAPI.GSON.fromJson(json, Color.class); Assert.assertEquals(object, parsed); String serialized = RuneLiteAPI.GSON.toJson(object); + if (exactEncoding) + { + Assert.assertEquals(json, serialized); + } Color roundTripped = RuneLiteAPI.GSON.fromJson(serialized, Color.class); Assert.assertEquals(object, roundTripped); }