GsonHelper .java

net.minecraft.util.GsonHelper (class, 596 lines): members: convertToBigDecimal, convertToBigInteger, convertToJsonObject, convertToCharacter, getAsItem.

net.minecraft.utilclass596 linescommon20.1 KB

API Surface (65)

boolean isStringValue(final JsonObject node, final String name)
boolean isStringValue(final JsonElement node)
boolean isNumberValue(final JsonObject node, final String name)
boolean isNumberValue(final JsonElement node)
boolean isBooleanValue(final JsonObject node, final String name)
boolean isBooleanValue(final JsonElement node)
boolean isArrayNode(final JsonObject node, final String name)
boolean isObjectNode(final JsonObject node, final String name)
boolean isValidPrimitive(final JsonObject node, final String name)
boolean isValidNode(final @Nullable JsonObject node, final String name)
JsonElement getNonNull(final JsonObject object, final String name)
String convertToString(final JsonElement element, final String name)
String getAsString(final JsonObject object, final String name)
Holder<Item> convertToItem(final JsonElement element, final String name)
Holder<Item> getAsItem(final JsonObject object, final String name)
boolean convertToBoolean(final JsonElement element, final String name)
boolean getAsBoolean(final JsonObject object, final String name)
boolean getAsBoolean(final JsonObject object, final String name, final boolean def)
double convertToDouble(final JsonElement element, final String name)
double getAsDouble(final JsonObject object, final String name)
double getAsDouble(final JsonObject object, final String name, final double def)
float convertToFloat(final JsonElement element, final String name)
float getAsFloat(final JsonObject object, final String name)
float getAsFloat(final JsonObject object, final String name, final float def)
long convertToLong(final JsonElement element, final String name)
long getAsLong(final JsonObject object, final String name)
long getAsLong(final JsonObject object, final String name, final long def)
int convertToInt(final JsonElement element, final String name)
int getAsInt(final JsonObject object, final String name)
int getAsInt(final JsonObject object, final String name, final int def)
byte convertToByte(final JsonElement element, final String name)
byte getAsByte(final JsonObject object, final String name)
byte getAsByte(final JsonObject object, final String name, final byte def)
char convertToCharacter(final JsonElement element, final String name)
char getAsCharacter(final JsonObject object, final String name)
char getAsCharacter(final JsonObject object, final String name, final char def)
BigDecimal convertToBigDecimal(final JsonElement element, final String name)
BigDecimal getAsBigDecimal(final JsonObject object, final String name)
BigDecimal getAsBigDecimal(final JsonObject object, final String name, final BigDecimal def)
BigInteger convertToBigInteger(final JsonElement element, final String name)
BigInteger getAsBigInteger(final JsonObject object, final String name)
BigInteger getAsBigInteger(final JsonObject object, final String name, final BigInteger def)
short convertToShort(final JsonElement element, final String name)
short getAsShort(final JsonObject object, final String name)
short getAsShort(final JsonObject object, final String name, final short def)
JsonObject convertToJsonObject(final JsonElement element, final String name)
JsonObject getAsJsonObject(final JsonObject object, final String name)
JsonArray convertToJsonArray(final JsonElement element, final String name)
JsonArray getAsJsonArray(final JsonObject object, final String name)
<T> T convertToObject(final @Nullable JsonElement element, final String name, final JsonDeserializationContext context, final Class<? extends T> clazz)
<T> T getAsObject(final JsonObject object, final String name, final JsonDeserializationContext context, final Class<? extends T> clazz)
String getType(final @Nullable JsonElement element)
<T> T fromJson(final Gson gson, final Reader reader, final Class<T> type)
<T> T fromJson(final Gson gson, final Reader reader, final TypeToken<T> type)
<T> T fromJson(final Gson gson, final String string, final Class<T> type)
JsonObject parse(final String string)
JsonObject parse(final Reader reader)
JsonArray parseArray(final String string)
JsonArray parseArray(final Reader reader)
String toStableString(final JsonElement jsonElement)
void writeValue(final JsonWriter out, final @Nullable JsonElement value, final @Nullable Comparator<String> keyComparator)
boolean encodesLongerThan(final JsonElement element, final int limit)
Appendable append(final CharSequence csq)
Appendable append(final CharSequence csq, final int start, final int end)
Appendable append(final char c)

Fields & Constants (3)

GSON
context
elements

String literals (43)

"Missing field "
"Expected "
" to be a string, was "
"Missing "
", expected to find a string"
"_,_,!null->!null;_,_,null->_"
" to be an item, was unknown string '"
" to be an item, was "
", expected to find an item"
" to be a Boolean, was "
", expected to find a Boolean"
" to be a Double, was "
", expected to find a Double"
" to be a Float, was "
", expected to find a Float"
" to be a Long, was "
", expected to find a Long"
" to be a Int, was "
", expected to find a Int"
" to be a Byte, was "
", expected to find a Byte"
" to be a Character, was "
", expected to find a Character"
" to be a BigDecimal, was "

Code Graph: Dependencies (4)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

GsonHelper.java596 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import com.google.gson.Gson;
4
import com.google.gson.GsonBuilder;
5
import com.google.gson.JsonArray;
6
import com.google.gson.JsonDeserializationContext;
7
import com.google.gson.JsonElement;
8
import com.google.gson.JsonObject;
9
import com.google.gson.JsonParseException;
10
import com.google.gson.JsonPrimitive;
11
import com.google.gson.JsonSyntaxException;
12
import com.google.gson.Strictness;
13
import com.google.gson.internal.Streams;
14
import com.google.gson.reflect.TypeToken;
15
import com.google.gson.stream.JsonReader;
16
import com.google.gson.stream.JsonWriter;
17
import java.io.IOException;
18
import java.io.Reader;
19
import java.io.StringReader;
20
import java.io.StringWriter;
21
import java.io.UncheckedIOException;
22
import java.math.BigDecimal;
23
import java.math.BigInteger;
24
import java.util.ArrayList;
25
import java.util.Collection;
26
import java.util.Comparator;
27
import java.util.List;
28
import java.util.Map.Entry;
29
import net.minecraft.core.Holder;
30
import net.minecraft.core.registries.BuiltInRegistries;
31
import net.minecraft.resources.Identifier;
32
import net.minecraft.world.item.Item;
33
import org.apache.commons.lang3.StringUtils;
34
import org.jetbrains.annotations.Contract;
35
import org.jspecify.annotations.Nullable;
36
37
public class GsonHelper {
38
	private static final Gson GSON = new GsonBuilder().create();
39
40
	public static boolean isStringValue(final JsonObject node, final String name) {
41
		return !isValidPrimitive(node, name) ? false : node.getAsJsonPrimitive(name).isString();
42
	}
43
44
	public static boolean isStringValue(final JsonElement node) {
45
		return !node.isJsonPrimitive() ? false : node.getAsJsonPrimitive().isString();
46
	}
47
48
	public static boolean isNumberValue(final JsonObject node, final String name) {
49
		return !isValidPrimitive(node, name) ? false : node.getAsJsonPrimitive(name).isNumber();
50
	}
51
52
	public static boolean isNumberValue(final JsonElement node) {
53
		return !node.isJsonPrimitive() ? false : node.getAsJsonPrimitive().isNumber();
54
	}
55
56
	public static boolean isBooleanValue(final JsonObject node, final String name) {
57
		return !isValidPrimitive(node, name) ? false : node.getAsJsonPrimitive(name).isBoolean();
58
	}
59
60
	public static boolean isBooleanValue(final JsonElement node) {
61
		return !node.isJsonPrimitive() ? false : node.getAsJsonPrimitive().isBoolean();
62
	}
63
64
	public static boolean isArrayNode(final JsonObject node, final String name) {
65
		return !isValidNode(node, name) ? false : node.get(name).isJsonArray();
66
	}
67
68
	public static boolean isObjectNode(final JsonObject node, final String name) {
69
		return !isValidNode(node, name) ? false : node.get(name).isJsonObject();
70
	}
71
72
	public static boolean isValidPrimitive(final JsonObject node, final String name) {
73
		return !isValidNode(node, name) ? false : node.get(name).isJsonPrimitive();
74
	}
75
76
	public static boolean isValidNode(final @Nullable JsonObject node, final String name) {
77
		return node == null ? false : node.get(name) != null;
78
	}
79
80
	public static JsonElement getNonNull(final JsonObject object, final String name) {
81
		JsonElement result = object.get(name);
82
		if (result != null && !result.isJsonNull()) {
83
			return result;
84
		} else {
85
			throw new JsonSyntaxException("Missing field " + name);
86
		}
87
	}
88
89
	public static String convertToString(final JsonElement element, final String name) {
90
		if (element.isJsonPrimitive()) {
91
			return element.getAsString();
92
		} else {
93
			throw new JsonSyntaxException("Expected " + name + " to be a string, was " + getType(element));
94
		}
95
	}
96
97
	public static String getAsString(final JsonObject object, final String name) {
98
		if (object.has(name)) {
99
			return convertToString(object.get(name), name);
100
		} else {
101
			throw new JsonSyntaxException("Missing " + name + ", expected to find a string");
102
		}
103
	}
104
105
	@Contract("_,_,!null->!null;_,_,null->_")
106
	public static @Nullable String getAsString(final JsonObject object, final String name, final @Nullable String def) {
107
		return object.has(name) ? convertToString(object.get(name), name) : def;
108
	}
109
110
	public static Holder<Item> convertToItem(final JsonElement element, final String name) {
111
		if (element.isJsonPrimitive()) {
112
			String itemName = element.getAsString();
113
			return BuiltInRegistries.ITEM
114
				.get(Identifier.parse(itemName))
115
				.orElseThrow(() -> new JsonSyntaxException("Expected " + name + " to be an item, was unknown string '" + itemName + "'"));
116
		} else {
117
			throw new JsonSyntaxException("Expected " + name + " to be an item, was " + getType(element));
118
		}
119
	}
120
121
	public static Holder<Item> getAsItem(final JsonObject object, final String name) {
122
		if (object.has(name)) {
123
			return convertToItem(object.get(name), name);
124
		} else {
125
			throw new JsonSyntaxException("Missing " + name + ", expected to find an item");
126
		}
127
	}
128
129
	@Contract("_,_,!null->!null;_,_,null->_")
130
	public static @Nullable Holder<Item> getAsItem(final JsonObject object, final String name, final @Nullable Holder<Item> def) {
131
		return object.has(name) ? convertToItem(object.get(name), name) : def;
132
	}
133
134
	public static boolean convertToBoolean(final JsonElement element, final String name) {
135
		if (element.isJsonPrimitive()) {
136
			return element.getAsBoolean();
137
		} else {
138
			throw new JsonSyntaxException("Expected " + name + " to be a Boolean, was " + getType(element));
139
		}
140
	}
141
142
	public static boolean getAsBoolean(final JsonObject object, final String name) {
143
		if (object.has(name)) {
144
			return convertToBoolean(object.get(name), name);
145
		} else {
146
			throw new JsonSyntaxException("Missing " + name + ", expected to find a Boolean");
147
		}
148
	}
149
150
	public static boolean getAsBoolean(final JsonObject object, final String name, final boolean def) {
151
		return object.has(name) ? convertToBoolean(object.get(name), name) : def;
152
	}
153
154
	public static double convertToDouble(final JsonElement element, final String name) {
155
		if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
156
			return element.getAsDouble();
157
		} else {
158
			throw new JsonSyntaxException("Expected " + name + " to be a Double, was " + getType(element));
159
		}
160
	}
161
162
	public static double getAsDouble(final JsonObject object, final String name) {
163
		if (object.has(name)) {
164
			return convertToDouble(object.get(name), name);
165
		} else {
166
			throw new JsonSyntaxException("Missing " + name + ", expected to find a Double");
167
		}
168
	}
169
170
	public static double getAsDouble(final JsonObject object, final String name, final double def) {
171
		return object.has(name) ? convertToDouble(object.get(name), name) : def;
172
	}
173
174
	public static float convertToFloat(final JsonElement element, final String name) {
175
		if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
176
			return element.getAsFloat();
177
		} else {
178
			throw new JsonSyntaxException("Expected " + name + " to be a Float, was " + getType(element));
179
		}
180
	}
181
182
	public static float getAsFloat(final JsonObject object, final String name) {
183
		if (object.has(name)) {
184
			return convertToFloat(object.get(name), name);
185
		} else {
186
			throw new JsonSyntaxException("Missing " + name + ", expected to find a Float");
187
		}
188
	}
189
190
	public static float getAsFloat(final JsonObject object, final String name, final float def) {
191
		return object.has(name) ? convertToFloat(object.get(name), name) : def;
192
	}
193
194
	public static long convertToLong(final JsonElement element, final String name) {
195
		if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
196
			return element.getAsLong();
197
		} else {
198
			throw new JsonSyntaxException("Expected " + name + " to be a Long, was " + getType(element));
199
		}
200
	}
201
202
	public static long getAsLong(final JsonObject object, final String name) {
203
		if (object.has(name)) {
204
			return convertToLong(object.get(name), name);
205
		} else {
206
			throw new JsonSyntaxException("Missing " + name + ", expected to find a Long");
207
		}
208
	}
209
210
	public static long getAsLong(final JsonObject object, final String name, final long def) {
211
		return object.has(name) ? convertToLong(object.get(name), name) : def;
212
	}
213
214
	public static int convertToInt(final JsonElement element, final String name) {
215
		if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
216
			return element.getAsInt();
217
		} else {
218
			throw new JsonSyntaxException("Expected " + name + " to be a Int, was " + getType(element));
219
		}
220
	}
221
222
	public static int getAsInt(final JsonObject object, final String name) {
223
		if (object.has(name)) {
224
			return convertToInt(object.get(name), name);
225
		} else {
226
			throw new JsonSyntaxException("Missing " + name + ", expected to find a Int");
227
		}
228
	}
229
230
	public static int getAsInt(final JsonObject object, final String name, final int def) {
231
		return object.has(name) ? convertToInt(object.get(name), name) : def;
232
	}
233
234
	public static byte convertToByte(final JsonElement element, final String name) {
235
		if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
236
			return element.getAsByte();
237
		} else {
238
			throw new JsonSyntaxException("Expected " + name + " to be a Byte, was " + getType(element));
239
		}
240
	}
241
242
	public static byte getAsByte(final JsonObject object, final String name) {
243
		if (object.has(name)) {
244
			return convertToByte(object.get(name), name);
245
		} else {
246
			throw new JsonSyntaxException("Missing " + name + ", expected to find a Byte");
247
		}
248
	}
249
250
	public static byte getAsByte(final JsonObject object, final String name, final byte def) {
251
		return object.has(name) ? convertToByte(object.get(name), name) : def;
252
	}
253
254
	public static char convertToCharacter(final JsonElement element, final String name) {
255
		if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
256
			return element.getAsCharacter();
257
		} else {
258
			throw new JsonSyntaxException("Expected " + name + " to be a Character, was " + getType(element));
259
		}
260
	}
261
262
	public static char getAsCharacter(final JsonObject object, final String name) {
263
		if (object.has(name)) {
264
			return convertToCharacter(object.get(name), name);
265
		} else {
266
			throw new JsonSyntaxException("Missing " + name + ", expected to find a Character");
267
		}
268
	}
269
270
	public static char getAsCharacter(final JsonObject object, final String name, final char def) {
271
		return object.has(name) ? convertToCharacter(object.get(name), name) : def;
272
	}
273
274
	public static BigDecimal convertToBigDecimal(final JsonElement element, final String name) {
275
		if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
276
			return element.getAsBigDecimal();
277
		} else {
278
			throw new JsonSyntaxException("Expected " + name + " to be a BigDecimal, was " + getType(element));
279
		}
280
	}
281
282
	public static BigDecimal getAsBigDecimal(final JsonObject object, final String name) {
283
		if (object.has(name)) {
284
			return convertToBigDecimal(object.get(name), name);
285
		} else {
286
			throw new JsonSyntaxException("Missing " + name + ", expected to find a BigDecimal");
287
		}
288
	}
289
290
	public static BigDecimal getAsBigDecimal(final JsonObject object, final String name, final BigDecimal def) {
291
		return object.has(name) ? convertToBigDecimal(object.get(name), name) : def;
292
	}
293
294
	public static BigInteger convertToBigInteger(final JsonElement element, final String name) {
295
		if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
296
			return element.getAsBigInteger();
297
		} else {
298
			throw new JsonSyntaxException("Expected " + name + " to be a BigInteger, was " + getType(element));
299
		}
300
	}
301
302
	public static BigInteger getAsBigInteger(final JsonObject object, final String name) {
303
		if (object.has(name)) {
304
			return convertToBigInteger(object.get(name), name);
305
		} else {
306
			throw new JsonSyntaxException("Missing " + name + ", expected to find a BigInteger");
307
		}
308
	}
309
310
	public static BigInteger getAsBigInteger(final JsonObject object, final String name, final BigInteger def) {
311
		return object.has(name) ? convertToBigInteger(object.get(name), name) : def;
312
	}
313
314
	public static short convertToShort(final JsonElement element, final String name) {
315
		if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
316
			return element.getAsShort();
317
		} else {
318
			throw new JsonSyntaxException("Expected " + name + " to be a Short, was " + getType(element));
319
		}
320
	}
321
322
	public static short getAsShort(final JsonObject object, final String name) {
323
		if (object.has(name)) {
324
			return convertToShort(object.get(name), name);
325
		} else {
326
			throw new JsonSyntaxException("Missing " + name + ", expected to find a Short");
327
		}
328
	}
329
330
	public static short getAsShort(final JsonObject object, final String name, final short def) {
331
		return object.has(name) ? convertToShort(object.get(name), name) : def;
332
	}
333
334
	public static JsonObject convertToJsonObject(final JsonElement element, final String name) {
335
		if (element.isJsonObject()) {
336
			return element.getAsJsonObject();
337
		} else {
338
			throw new JsonSyntaxException("Expected " + name + " to be a JsonObject, was " + getType(element));
339
		}
340
	}
341
342
	public static JsonObject getAsJsonObject(final JsonObject object, final String name) {
343
		if (object.has(name)) {
344
			return convertToJsonObject(object.get(name), name);
345
		} else {
346
			throw new JsonSyntaxException("Missing " + name + ", expected to find a JsonObject");
347
		}
348
	}
349
350
	@Contract("_,_,!null->!null;_,_,null->_")
351
	public static @Nullable JsonObject getAsJsonObject(final JsonObject object, final String name, final @Nullable JsonObject def) {
352
		return object.has(name) ? convertToJsonObject(object.get(name), name) : def;
353
	}
354
355
	public static JsonArray convertToJsonArray(final JsonElement element, final String name) {
356
		if (element.isJsonArray()) {
357
			return element.getAsJsonArray();
358
		} else {
359
			throw new JsonSyntaxException("Expected " + name + " to be a JsonArray, was " + getType(element));
360
		}
361
	}
362
363
	public static JsonArray getAsJsonArray(final JsonObject object, final String name) {
364
		if (object.has(name)) {
365
			return convertToJsonArray(object.get(name), name);
366
		} else {
367
			throw new JsonSyntaxException("Missing " + name + ", expected to find a JsonArray");
368
		}
369
	}
370
371
	@Contract("_,_,!null->!null;_,_,null->_")
372
	public static @Nullable JsonArray getAsJsonArray(final JsonObject object, final String name, final @Nullable JsonArray def) {
373
		return object.has(name) ? convertToJsonArray(object.get(name), name) : def;
374
	}
375
376
	public static <T> T convertToObject(
377
		final @Nullable JsonElement element, final String name, final JsonDeserializationContext context, final Class<? extends T> clazz
378
	) {
379
		if (element != null) {
380
			return context.deserialize(element, clazz);
381
		} else {
382
			throw new JsonSyntaxException("Missing " + name);
383
		}
384
	}
385
386
	public static <T> T getAsObject(final JsonObject object, final String name, final JsonDeserializationContext context, final Class<? extends T> clazz) {
387
		if (object.has(name)) {
388
			return convertToObject(object.get(name), name, context, clazz);
389
		} else {
390
			throw new JsonSyntaxException("Missing " + name);
391
		}
392
	}
393
394
	@Contract("_,_,!null,_,_->!null;_,_,null,_,_->_")
395
	public static <T> @Nullable T getAsObject(
396
		final JsonObject object, final String name, final @Nullable T def, final JsonDeserializationContext context, final Class<? extends T> clazz
397
	) {
398
		return object.has(name) ? convertToObject(object.get(name), name, context, clazz) : def;
399
	}
400
401
	public static String getType(final @Nullable JsonElement element) {
402
		String value = StringUtils.abbreviateMiddle(String.valueOf(element), "...", 10);
403
		if (element == null) {
404
			return "null (missing)";
405
		}
406
407
		if (element.isJsonNull()) {
408
			return "null (json)";
409
		}
410
411
		if (element.isJsonArray()) {
412
			return "an array (" + value + ")";
413
		}
414
415
		if (element.isJsonObject()) {
416
			return "an object (" + value + ")";
417
		}
418
419
		if (element.isJsonPrimitive()) {
420
			JsonPrimitive primitive = element.getAsJsonPrimitive();
421
			if (primitive.isNumber()) {
422
				return "a number (" + value + ")";
423
			}
424
425
			if (primitive.isBoolean()) {
426
				return "a boolean (" + value + ")";
427
			}
428
		}
429
430
		return value;
431
	}
432
433
	public static <T> T fromJson(final Gson gson, final Reader reader, final Class<T> type) {
434
		try {
435
			JsonReader jsonReader = new JsonReader(reader);
436
			jsonReader.setStrictness(Strictness.STRICT);
437
			T result = gson.getAdapter(type).read(jsonReader);
438
			if (result == null) {
439
				throw new JsonParseException("JSON data was null or empty");
440
			} else {
441
				return result;
442
			}
443
		} catch (IOException e) {
444
			throw new JsonParseException(e);
445
		}
446
	}
447
448
	public static <T> @Nullable T fromNullableJson(final Gson gson, final Reader reader, final TypeToken<T> type) {
449
		try {
450
			JsonReader jsonReader = new JsonReader(reader);
451
			jsonReader.setStrictness(Strictness.STRICT);
452
			return gson.getAdapter(type).read(jsonReader);
453
		} catch (IOException e) {
454
			throw new JsonParseException(e);
455
		}
456
	}
457
458
	public static <T> T fromJson(final Gson gson, final Reader reader, final TypeToken<T> type) {
459
		T result = fromNullableJson(gson, reader, type);
460
		if (result == null) {
461
			throw new JsonParseException("JSON data was null or empty");
462
		} else {
463
			return result;
464
		}
465
	}
466
467
	public static <T> @Nullable T fromNullableJson(final Gson gson, final String string, final TypeToken<T> type) {
468
		return fromNullableJson(gson, new StringReader(string), type);
469
	}
470
471
	public static <T> T fromJson(final Gson gson, final String string, final Class<T> type) {
472
		return fromJson(gson, new StringReader(string), type);
473
	}
474
475
	public static JsonObject parse(final String string) {
476
		return parse(new StringReader(string));
477
	}
478
479
	public static JsonObject parse(final Reader reader) {
480
		return fromJson(GSON, reader, JsonObject.class);
481
	}
482
483
	public static JsonArray parseArray(final String string) {
484
		return parseArray(new StringReader(string));
485
	}
486
487
	public static JsonArray parseArray(final Reader reader) {
488
		return fromJson(GSON, reader, JsonArray.class);
489
	}
490
491
	public static String toStableString(final JsonElement jsonElement) {
492
		StringWriter stringWriter = new StringWriter();
493
		JsonWriter jsonWriter = new JsonWriter(stringWriter);
494
495
		try {
496
			writeValue(jsonWriter, jsonElement, Comparator.naturalOrder());
497
		} catch (IOException e) {
498
			throw new AssertionError(e);
499
		}
500
501
		return stringWriter.toString();
502
	}
503
504
	public static void writeValue(final JsonWriter out, final @Nullable JsonElement value, final @Nullable Comparator<String> keyComparator) throws IOException {
505
		if (value == null || value.isJsonNull()) {
506
			out.nullValue();
507
		} else if (value.isJsonPrimitive()) {
508
			JsonPrimitive primitive = value.getAsJsonPrimitive();
509
			if (primitive.isNumber()) {
510
				out.value(primitive.getAsNumber());
511
			} else if (primitive.isBoolean()) {
512
				out.value(primitive.getAsBoolean());
513
			} else {
514
				out.value(primitive.getAsString());
515
			}
516
		} else if (value.isJsonArray()) {
517
			out.beginArray();
518
519
			for (JsonElement e : value.getAsJsonArray()) {
520
				writeValue(out, e, keyComparator);
521
			}
522
523
			out.endArray();
524
		} else {
525
			if (!value.isJsonObject()) {
526
				throw new IllegalArgumentException("Couldn't write " + value.getClass());
527
			}
528
529
			out.beginObject();
530
531
			for (Entry<String, JsonElement> e : sortByKeyIfNeeded(value.getAsJsonObject().entrySet(), keyComparator)) {
532
				out.name(e.getKey());
533
				writeValue(out, e.getValue(), keyComparator);
534
			}
535
536
			out.endObject();
537
		}
538
	}
539
540
	private static Collection<Entry<String, JsonElement>> sortByKeyIfNeeded(
541
		final Collection<Entry<String, JsonElement>> elements, final @Nullable Comparator<String> keyComparator
542
	) {
543
		if (keyComparator == null) {
544
			return elements;
545
		}
546
547
		List<Entry<String, JsonElement>> sorted = new ArrayList<>(elements);
548
		sorted.sort(Entry.comparingByKey(keyComparator));
549
		return sorted;
550
	}
551
552
	public static boolean encodesLongerThan(final JsonElement element, final int limit) {
553
		try {
554
			Streams.write(element, new JsonWriter(Streams.writerForAppendable(new GsonHelper.CountedAppendable(limit))));
555
			return false;
556
		} catch (IllegalStateException e) {
557
			return true;
558
		} catch (IOException e) {
559
			throw new UncheckedIOException(e);
560
		}
561
	}
562
563
	private static class CountedAppendable implements Appendable {
564
		private int totalCount;
565
		private final int limit;
566
567
		public CountedAppendable(final int limit) {
568
			this.limit = limit;
569
		}
570
571
		private Appendable accountChars(final int count) {
572
			this.totalCount += count;
573
			if (this.totalCount > this.limit) {
574
				throw new IllegalStateException("Character count over limit: " + this.totalCount + " > " + this.limit);
575
			} else {
576
				return this;
577
			}
578
		}
579
580
		@Override
581
		public Appendable append(final CharSequence csq) {
582
			return this.accountChars(csq.length());
583
		}
584
585
		@Override
586
		public Appendable append(final CharSequence csq, final int start, final int end) {
587
			return this.accountChars(end - start);
588
		}
589
590
		@Override
591
		public Appendable append(final char c) {
592
			return this.accountChars(1);
593
		}
594
	}
595
}
596

Decompiled Minecraft 26.3 source of GsonHelper (net/minecraft/util/GsonHelper.java) with Mojang official mappings, stored verbatim on the site.