Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate remaining text-serializer-gson serializers to use TypeAdapters #373

Merged
merged 22 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
eaf22a7
Convert ShowItemSerializer to TypeAdapter
emilyy-dev May 18, 2021
7faa7da
Write kashike-compliant code
emilyy-dev May 18, 2021
48da0be
Use a TypeAdapterFactory for better handling dependencies between ser…
emilyy-dev May 19, 2021
4349c4d
Write more kashike-compliant code
emilyy-dev May 19, 2021
352b221
Convert StyleSerializer to TypeAdapter
emilyy-dev May 20, 2021
6ea403e
Convert ShowEntitySerializer to TypeAdapter
emilyy-dev May 20, 2021
d4d1f5d
Remove unnecessary imports
emilyy-dev May 20, 2021
60353a9
Move indexed serializers to static instances
emilyy-dev May 20, 2021
e1d72d0
Use static types in SerializerFactory when retrieving Gson adapters
emilyy-dev May 20, 2021
0bfe5c2
Move indexed serializers to static instances once again into dummy cl…
emilyy-dev May 20, 2021
4cef6bf
Prefer direct references to serializers over Gson#getAdapter
emilyy-dev May 21, 2021
0978383
Prevent infinite recursion with hideous patterns
emilyy-dev May 21, 2021
0223077
Make the style checker happy
emilyy-dev Jun 1, 2021
6d71308
Make the way of making the CSI & SES less hideous
emilyy-dev Jun 1, 2021
3db8bce
Add spaces and use jetbrains annotations to comply with current check…
emilyy-dev Jul 23, 2021
06ec1df
What's your favorite thing about space? Mine is space.
emilyy-dev Jul 24, 2021
066ec51
Don't hard-code the serializer instances used throughout the serializers
emilyy-dev Aug 3, 2021
9f3edca
Add gson (de)serialization benchmarks
emilyy-dev Aug 9, 2021
38e1ee1
Merge branch 'master' into feature/gson-type-adapters
emilyy-dev Aug 9, 2021
cd8afb8
Add license headers
emilyy-dev Aug 11, 2021
7b57876
Minor changes in de/serialization benchmarks
emilyy-dev Aug 24, 2021
8cc2511
Actually migrate `ComponentSerializerImpl` to `TypeAdapter`
emilyy-dev Aug 25, 2021
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
1 change: 1 addition & 0 deletions text-serializer-gson/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("adventure.common-conventions")
id("me.champeau.jmh")
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2021 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 java.util.UUID;
import java.util.concurrent.TimeUnit;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.api.BinaryTagHolder;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;

import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.event.ClickEvent.openUrl;
import static net.kyori.adventure.text.event.HoverEvent.showEntity;
import static net.kyori.adventure.text.event.HoverEvent.showItem;
import static net.kyori.adventure.text.event.HoverEvent.showText;
import static net.kyori.adventure.text.format.Style.style;
import static net.kyori.adventure.text.format.TextColor.color;
import static net.kyori.adventure.text.serializer.gson.GsonComponentSerializer.gson;

@State(Scope.Benchmark)
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class ComponentDeserializationBenchmark {
private String simpleComponent;
private String componentTreeWithStyle;
private String componentTreeWithEvents;

@Setup(Level.Trial)
public void prepare() {
this.simpleComponent = gson().serialize(text("Hello, World!", style(TextDecoration.UNDERLINED)));
this.componentTreeWithStyle = gson().serialize(text()
.decorate(TextDecoration.UNDERLINED, TextDecoration.ITALIC)
.append(text("Component ", color(0x8cfbde)))
.append(text("with ", color(0x0fff95), TextDecoration.BOLD))
.append(text("hex ", color(0x06ba63)))
.append(text("colors", color(0x103900)))
.build());
this.componentTreeWithEvents = gson().serialize(text()
.decorate(TextDecoration.UNDERLINED, TextDecoration.ITALIC)
.append(text("Component ", style(color(0x8cfbde), openUrl("https://kyori.net/"))))
.append(text("with ", style(color(0x0fff95), TextDecoration.BOLD, showItem(Key.key("iron_sword"), 1, BinaryTagHolder.of("{Damage: 30, RepairCost: 4, Enchantments: [{id: 'minecraft:sharpness', lvl: 3s}, {id: 'minecraft:unbreaking', lvl: 1s}]}")))))
.append(text("hex ", style(color(0x06ba63), showEntity(Key.key("pig"), UUID.randomUUID(), text("Piggy", NamedTextColor.YELLOW)))))
.append(text("colors", style(color(0x103900), showText(text("Text hover!")))))
.build());
}

@Benchmark
public Component simpleComponent() {
return gson().deserialize(this.simpleComponent);
}

@Benchmark
public Component componentTreeWithStyle() {
return gson().deserialize(this.componentTreeWithStyle);
}

@Benchmark
public Component componentTreeWithEvents() {
return gson().deserialize(this.componentTreeWithEvents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2021 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 java.util.UUID;
import java.util.concurrent.TimeUnit;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.api.BinaryTagHolder;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;

import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.event.ClickEvent.openUrl;
import static net.kyori.adventure.text.event.HoverEvent.showEntity;
import static net.kyori.adventure.text.event.HoverEvent.showItem;
import static net.kyori.adventure.text.event.HoverEvent.showText;
import static net.kyori.adventure.text.format.Style.style;
import static net.kyori.adventure.text.format.TextColor.color;
import static net.kyori.adventure.text.serializer.gson.GsonComponentSerializer.gson;

@State(Scope.Benchmark)
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class ComponentSerializationBenchmark {
private Component simpleComponent;
private Component componentTreeWithStyle;
private Component componentTreeWithEvents;

@Setup(Level.Trial)
public void prepare() {
this.simpleComponent = text("Hello, World!", style(TextDecoration.UNDERLINED));
this.componentTreeWithStyle = text()
.decorate(TextDecoration.UNDERLINED, TextDecoration.ITALIC)
.append(text("Component ", color(0x8cfbde)))
.append(text("with ", color(0x0fff95), TextDecoration.BOLD))
.append(text("hex ", color(0x06ba63)))
.append(text("colors", color(0x103900)))
.build();
this.componentTreeWithEvents = text()
.decorate(TextDecoration.UNDERLINED, TextDecoration.ITALIC)
.append(text("Component ", style(color(0x8cfbde), openUrl("https://kyori.net/"))))
.append(text("with ", style(color(0x0fff95), TextDecoration.BOLD, showItem(Key.key("iron_sword"), 1, BinaryTagHolder.of("{Damage: 30, RepairCost: 4, Enchantments: [{id: 'minecraft:sharpness', lvl: 3s}, {id: 'minecraft:unbreaking', lvl: 1s}]}")))))
.append(text("hex ", style(color(0x06ba63), showEntity(Key.key("pig"), UUID.randomUUID(), text("Piggy", NamedTextColor.YELLOW)))))
.append(text("colors", style(color(0x103900), showText(text("Text hover!")))))
.build();
}

emilyy-dev marked this conversation as resolved.
Show resolved Hide resolved
@Benchmark
public String simpleComponent() {
return gson().serialize(this.simpleComponent);
}

@Benchmark
public String componentTreeWithStyle() {
return gson().serialize(this.componentTreeWithStyle);
}

@Benchmark
public String componentTreeWithEvents() {
return gson().serialize(this.componentTreeWithEvents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2021 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 com.google.gson.TypeAdapter;
import net.kyori.adventure.text.event.ClickEvent;

final class ClickEventActionSerializer {
static final TypeAdapter<ClickEvent.Action> INSTANCE = IndexedSerializer.of("click action", ClickEvent.Action.NAMES);

private ClickEventActionSerializer() {
}
}
Loading