diff --git a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/ClickEventActionSerializer.java b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/ClickEventActionSerializer.java index 3132914c0..271bf4129 100644 --- a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/ClickEventActionSerializer.java +++ b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/ClickEventActionSerializer.java @@ -27,7 +27,7 @@ import net.kyori.adventure.text.event.ClickEvent; final class ClickEventActionSerializer { - static final TypeAdapter INSTANCE = IndexedSerializer.of("click action", ClickEvent.Action.NAMES); + static final TypeAdapter INSTANCE = IndexedSerializer.lenient("click action", ClickEvent.Action.NAMES); private ClickEventActionSerializer() { } diff --git a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/HoverEventActionSerializer.java b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/HoverEventActionSerializer.java index 829223471..f94bdce35 100644 --- a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/HoverEventActionSerializer.java +++ b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/HoverEventActionSerializer.java @@ -27,7 +27,7 @@ import net.kyori.adventure.text.event.HoverEvent; final class HoverEventActionSerializer { - static final TypeAdapter> INSTANCE = IndexedSerializer.of("hover action", HoverEvent.Action.NAMES); + static final TypeAdapter> INSTANCE = IndexedSerializer.lenient("hover action", HoverEvent.Action.NAMES); private HoverEventActionSerializer() { } diff --git a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/IndexedSerializer.java b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/IndexedSerializer.java index c9e895a72..232b75652 100644 --- a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/IndexedSerializer.java +++ b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/IndexedSerializer.java @@ -33,14 +33,20 @@ final class IndexedSerializer extends TypeAdapter { private final String name; private final Index map; + private final boolean throwOnUnknownKey; - public static TypeAdapter of(final String name, final Index map) { - return new IndexedSerializer<>(name, map).nullSafe(); + public static TypeAdapter strict(final String name, final Index map) { + return new IndexedSerializer<>(name, map, true).nullSafe(); } - private IndexedSerializer(final String name, final Index map) { + public static TypeAdapter lenient(final String name, final Index map) { + return new IndexedSerializer<>(name, map, false).nullSafe(); + } + + private IndexedSerializer(final String name, final Index map, final boolean throwOnUnknownKey) { this.name = name; this.map = map; + this.throwOnUnknownKey = throwOnUnknownKey; } @Override @@ -54,8 +60,10 @@ public E read(final JsonReader in) throws IOException { final E value = this.map.value(string); if (value != null) { return value; - } else { + } else if (this.throwOnUnknownKey) { throw new JsonParseException("invalid " + this.name + ": " + string); + } else { + return null; } } } diff --git a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/TextDecorationSerializer.java b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/TextDecorationSerializer.java index adf9de36a..b8a9f72b2 100644 --- a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/TextDecorationSerializer.java +++ b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/TextDecorationSerializer.java @@ -27,7 +27,7 @@ import net.kyori.adventure.text.format.TextDecoration; final class TextDecorationSerializer { - static final TypeAdapter INSTANCE = IndexedSerializer.of("text decoration", TextDecoration.NAMES); + static final TypeAdapter INSTANCE = IndexedSerializer.strict("text decoration", TextDecoration.NAMES); private TextDecorationSerializer() { } diff --git a/text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue788Test.java b/text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue788Test.java index 42ed4436d..a9b51296d 100644 --- a/text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue788Test.java +++ b/text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue788Test.java @@ -28,6 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +// https://github.com/KyoriPowered/adventure/issues/788 class Issue788Test { @Test void test() { diff --git a/text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue792Test.java b/text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue792Test.java new file mode 100644 index 000000000..55c6d5884 --- /dev/null +++ b/text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue792Test.java @@ -0,0 +1,39 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.serializer.gson; + +import net.kyori.adventure.text.Component; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +// https://github.com/KyoriPowered/adventure/issues/792 +class Issue792Test { + @Test + void test() { + final String input = "[\"\",{\"text\":\"\",\"extra\":[{\"text\":\"\",\"color\":\"white\"},{\"text\":\"{marriagemaster_heart}\",\"color\":\"white\",\"clickEvent\":{\"action\":\"\",\"value\":\"\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"\"}]}}},{\"text\":\" \",\"color\":\"white\"},{\"text\":\"{vault_prefix}\",\"color\":\"white\",\"clickEvent\":{\"action\":\"\",\"value\":\"\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"\"}]}}},{\"text\":\"\",\"color\":\"white\"},{\"text\":\"{player_displayname}\",\"color\":\"white\",\"clickEvent\":{\"action\":\"\",\"value\":\"\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"\"}]}}},{\"text\":\"\",\"color\":\"white\"},{\"text\":\"{vault_suffix}\",\"color\":\"white\",\"clickEvent\":{\"action\":\"\",\"value\":\"\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"\"}]}}},{\"text\":\" \",\"color\":\"white\"},{\"text\":\"»\",\"color\":\"gray\"}]},{\"text\":\"\",\"color\":\"white\"},{\"text\":\" f\",\"color\":\"white\"},{\"text\":\" [✓]\",\"color\":\"red\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/vchatgui .DoctorMad9952 Global 421783436\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Open Moderation GUI\",\"color\":\"red\"}]}}}]"; + final Component component = GsonComponentSerializer.gson().deserialize(input); + assertNotNull(component); + } +}