Skip to content

Commit

Permalink
Merge pull request #257 from KyoriPowered/fix/gson-ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 authored Jan 11, 2021
2 parents b7c62e8 + 5d8c8ac commit 9e959f0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void testSerializeToString() {
.build()
.serialize(Component.text("Hello", Style.style(TextDecoration.BOLD)));

assertEquals("{\"text\":\"Hello\",\"bold\":true}", serialized.getString());
assertEquals("{\"bold\":true,\"text\":\"Hello\"}", serialized.getString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void testSerializeToString() {
.build()
.serialize(Component.text("Hello", Style.style(TextDecoration.BOLD)));

assertEquals("{\"text\":\"Hello\",\"bold\":true}", serialized.getString());
assertEquals("{\"bold\":true,\"text\":\"Hello\"}", serialized.getString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,25 @@ private static <C extends NBTComponent<C, B>, B extends NBTComponentBuilder<C, B
@Override
public JsonElement serialize(final Component src, final Type typeOfSrc, final JsonSerializationContext context) {
final JsonObject object = new JsonObject();

if(src.hasStyling()) {
final JsonElement style = context.serialize(src.style());
if(style.isJsonObject()) {
for(final Map.Entry<String, JsonElement> entry : ((JsonObject) style).entrySet()) {
object.add(entry.getKey(), entry.getValue());
}
}
}

final List<Component> children = src.children();
if(!children.isEmpty()) {
final JsonArray extra = new JsonArray();
for(final Component child : children) {
extra.add(context.serialize(child));
}
object.add(EXTRA, extra);
}

if(src instanceof TextComponent) {
object.addProperty(TEXT, ((TextComponent) src).content());
} else if(src instanceof TranslatableComponent) {
Expand Down Expand Up @@ -215,24 +234,6 @@ public JsonElement serialize(final Component src, final Type typeOfSrc, final Js
throw notSureHowToSerialize(src);
}

final List<Component> children = src.children();
if(!children.isEmpty()) {
final JsonArray extra = new JsonArray();
for(final Component child : children) {
extra.add(context.serialize(child));
}
object.add(EXTRA, extra);
}

if(src.hasStyling()) {
final JsonElement style = context.serialize(src.style());
if(style.isJsonObject()) {
for(final Map.Entry<String, JsonElement> entry : ((JsonObject) style).entrySet()) {
object.add(entry.getKey(), entry.getValue());
}
}
}

return object;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.util.EnumSet;
import java.util.Set;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
Expand All @@ -49,7 +51,27 @@
import org.checkerframework.checker.nullness.qual.Nullable;

final class StyleSerializer implements JsonDeserializer<Style>, JsonSerializer<Style> {
private static final TextDecoration[] DECORATIONS = TextDecoration.values();
@SuppressWarnings("checkstyle:NoWhitespaceAfter")
private static final TextDecoration[] DECORATIONS = {
// The order here is important -- Minecraft does string comparisons of some
// serialized components so we have to make sure our order matches Vanilla
TextDecoration.BOLD,
TextDecoration.ITALIC,
TextDecoration.UNDERLINED,
TextDecoration.STRIKETHROUGH,
TextDecoration.OBFUSCATED
};

static {
// Ensure coverage of decorations
final Set<TextDecoration> knownDecorations = EnumSet.allOf(TextDecoration.class);
for(final TextDecoration decoration : DECORATIONS) {
knownDecorations.remove(decoration);
}
if(!knownDecorations.isEmpty()) {
throw new IllegalStateException("GSON serializer is missing some text decorations: " + knownDecorations);
}
}

static final String FONT = "font";
static final String COLOR = "color";
Expand Down Expand Up @@ -183,16 +205,6 @@ private Codec.Decoder<Component, String, JsonParseException> decoder(final JsonD
public JsonElement serialize(final Style src, final Type typeOfSrc, final JsonSerializationContext context) {
final JsonObject json = new JsonObject();

final @Nullable Key font = src.font();
if(font != null) {
json.add(FONT, context.serialize(font));
}

final @Nullable TextColor color = src.color();
if(color != null) {
json.add(COLOR, context.serialize(color));
}

for(int i = 0, length = DECORATIONS.length; i < length; i++) {
final TextDecoration decoration = DECORATIONS[i];
final TextDecoration.State state = src.decoration(decoration);
Expand All @@ -203,6 +215,11 @@ public JsonElement serialize(final Style src, final Type typeOfSrc, final JsonSe
}
}

final @Nullable TextColor color = src.color();
if(color != null) {
json.add(COLOR, context.serialize(color));
}

final @Nullable String insertion = src.insertion();
if(insertion != null) {
json.addProperty(INSERTION, insertion);
Expand All @@ -228,6 +245,11 @@ public JsonElement serialize(final Style src, final Type typeOfSrc, final JsonSe
json.add(HOVER_EVENT, eventJson);
}

final @Nullable Key font = src.font();
if(font != null) {
json.add(FONT, context.serialize(font));
}

return json;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void testPre116Downsamples() {
final TextColor original = TextColor.color(0xAB2211);
final NamedTextColor downsampled = NamedTextColor.nearestTo(original);
final Component test = Component.text("meow", original);
assertEquals("{\"text\":\"meow\",\"color\":\"" + name(downsampled) + "\"}", GsonComponentSerializer.colorDownsamplingGson().serializer().toJson(test));
assertEquals("{\"color\":\"" + name(downsampled) + "\",\"text\":\"meow\"}", GsonComponentSerializer.colorDownsamplingGson().serializer().toJson(test));
}

@Test
Expand All @@ -76,7 +76,7 @@ public void testPre116DownsamplesInChildren() {
final NamedTextColor downsampled = NamedTextColor.nearestTo(original);
final Component test = Component.text(builder -> builder.content("hey").append(Component.text("there", original)));

assertEquals("{\"text\":\"hey\",\"extra\":[{\"text\":\"there\",\"color\":\"" + name(downsampled) + "\"}]}", GsonComponentSerializer.colorDownsamplingGson().serializer().toJson(test));
assertEquals("{\"extra\":[{\"color\":\"" + name(downsampled) + "\",\"text\":\"there\"}],\"text\":\"hey\"}", GsonComponentSerializer.colorDownsamplingGson().serializer().toJson(test));
}

private static String name(final NamedTextColor color) {
Expand Down

0 comments on commit 9e959f0

Please sign in to comment.