|
1 | 1 | package net.anweisen.utilities.common.config.document; |
2 | 2 |
|
3 | 3 | import com.google.gson.*; |
| 4 | +import com.google.gson.internal.LazilyParsedNumber; |
4 | 5 | import com.google.gson.internal.bind.TypeAdapters; |
| 6 | +import com.google.gson.stream.JsonReader; |
| 7 | +import com.google.gson.stream.JsonToken; |
| 8 | +import com.google.gson.stream.JsonWriter; |
5 | 9 | import net.anweisen.utilities.common.collection.pair.Pair; |
6 | 10 | import net.anweisen.utilities.common.config.Document; |
7 | 11 | import net.anweisen.utilities.common.config.document.gson.*; |
@@ -40,6 +44,28 @@ public class GsonDocument extends AbstractDocument { |
40 | 44 | GSON_PRETTY_PRINT = builder.setPrettyPrinting().create(); |
41 | 45 | } |
42 | 46 |
|
| 47 | + // Not implemented in some versions of gson |
| 48 | + TypeAdapter<Number> NUMBER = new TypeAdapter<Number>() { |
| 49 | + @Override |
| 50 | + public void write(JsonWriter jsonWriter, Number number) throws IOException { |
| 51 | + TypeAdapters.STRING.write(jsonWriter, String.valueOf(number)); |
| 52 | + } |
| 53 | + public Number read(JsonReader in) throws IOException { |
| 54 | + JsonToken jsonToken = in.peek(); |
| 55 | + switch (jsonToken) { |
| 56 | + case NUMBER: |
| 57 | + case STRING: |
| 58 | + return new LazilyParsedNumber(in.nextString()); |
| 59 | + case BOOLEAN: |
| 60 | + default: |
| 61 | + throw new JsonSyntaxException("Expecting number, got: " + jsonToken); |
| 62 | + case NULL: |
| 63 | + in.nextNull(); |
| 64 | + return null; |
| 65 | + } |
| 66 | + } |
| 67 | + }; |
| 68 | + |
43 | 69 | public static final Gson GSON, GSON_PRETTY_PRINT; |
44 | 70 |
|
45 | 71 | private static boolean cleanupEmptyObjects = false; |
@@ -395,7 +421,7 @@ private void setElement(@Nonnull String path, @Nullable Object value) { |
395 | 421 | JsonElement jsonValue = |
396 | 422 | value instanceof JsonElement ? (JsonElement) value |
397 | 423 | : value == null ? JsonNull.INSTANCE |
398 | | - : value instanceof Number ? TypeAdapters.NUMBER.toJsonTree((Number) value) |
| 424 | + : value instanceof Number ? NUMBER.toJsonTree((Number) value) |
399 | 425 | : value instanceof Boolean ? TypeAdapters.BOOLEAN.toJsonTree((boolean) value) |
400 | 426 | : value instanceof Character ? TypeAdapters.CHARACTER.toJsonTree((char) value) |
401 | 427 | : GSON.toJsonTree(value); |
|
0 commit comments