-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
GsonComponentSerializer.java
183 lines (169 loc) · 5.7 KB
/
GsonComponentSerializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* 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.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import java.util.function.UnaryOperator;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.ComponentSerializer;
import net.kyori.adventure.util.Buildable;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.ApiStatus;
/**
* A gson component serializer.
*
* <p>Use {@link Builder#downsampleColors()} to support platforms
* that do not understand hex colors that were introduced in Minecraft 1.16.</p>
*
* @since 4.0.0
*/
public interface GsonComponentSerializer extends ComponentSerializer<Component, Component, String>, Buildable<GsonComponentSerializer, GsonComponentSerializer.Builder> {
/**
* Gets a component serializer for gson serialization and deserialization.
*
* @return a gson component serializer
* @since 4.0.0
*/
static @NonNull GsonComponentSerializer gson() {
return GsonComponentSerializerImpl.INSTANCE;
}
/**
* Gets a component serializer for gson serialization and deserialization.
*
* <p>Hex colors are coerced to the nearest named color, and legacy hover events are
* emitted for action {@link net.kyori.adventure.text.event.HoverEvent.Action#SHOW_TEXT}.</p>
*
* @return a gson component serializer
* @since 4.0.0
*/
static @NonNull GsonComponentSerializer colorDownsamplingGson() {
return GsonComponentSerializerImpl.LEGACY_INSTANCE;
}
/**
* Creates a new {@link GsonComponentSerializer.Builder}.
*
* @return a builder
* @since 4.0.0
*/
static Builder builder() {
return new GsonComponentSerializerImpl.BuilderImpl();
}
/**
* Gets the underlying gson serializer.
*
* @return a gson serializer
* @since 4.0.0
*/
@NonNull Gson serializer();
/**
* Gets the underlying gson populator.
*
* @return a gson populator
* @since 4.0.0
*/
@NonNull UnaryOperator<GsonBuilder> populator();
/**
* Deserialize a component from input of type {@link JsonElement}.
*
* @param input the input
* @return the component
* @since 4.7.0
*/
@NonNull Component deserializeFromTree(final @NonNull JsonElement input);
/**
* Deserialize a component to output of type {@link JsonElement}.
*
* @param component the component
* @return the json element
* @since 4.7.0
*/
@NonNull JsonElement serializeToTree(final @NonNull Component component);
/**
* A builder for {@link GsonComponentSerializer}.
*
* @since 4.0.0
*/
interface Builder extends Buildable.Builder<GsonComponentSerializer> {
/**
* Sets that the serializer should downsample hex colors to named colors.
*
* @return this builder
* @since 4.0.0
*/
@NonNull Builder downsampleColors();
/**
* Sets a serializer that will be used to interpret legacy hover event {@code value} payloads.
* If the serializer is {@code null}, then only {@link net.kyori.adventure.text.event.HoverEvent.Action#SHOW_TEXT}
* legacy hover events can be deserialized.
*
* @param serializer serializer
* @return this builder
* @since 4.0.0
*/
@NonNull Builder legacyHoverEventSerializer(final @Nullable LegacyHoverEventSerializer serializer);
/**
* Output a legacy hover event {@code value} in addition to the modern {@code contents}.
*
* <p>A {@link #legacyHoverEventSerializer(LegacyHoverEventSerializer) legacy hover serializer} must also be set
* to serialize any hover events beyond those with action {@link net.kyori.adventure.text.event.HoverEvent.Action#SHOW_TEXT}</p>
*
* @return this builder
* @since 4.0.0
*/
@NonNull Builder emitLegacyHoverEvent();
/**
* Builds the serializer.
*
* @return the built serializer
*/
@Override
@NonNull GsonComponentSerializer build();
}
/**
* A {@link GsonComponentSerializer} service provider.
*
* @since 4.8.0
*/
@ApiStatus.Internal
interface Provider {
/**
* Provides a standard {@link GsonComponentSerializer}.
*
* @return a {@link GsonComponentSerializer}
* @since 4.8.0
*/
@ApiStatus.Internal
@NonNull GsonComponentSerializer gson();
/**
* Provides a legacy {@link GsonComponentSerializer}.
*
* @return a {@link GsonComponentSerializer}
* @since 4.8.0
*/
@ApiStatus.Internal
@NonNull GsonComponentSerializer gsonLegacy();
}
}