| 1 | package com.mojang.realmsclient.util;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.google.gson.JsonObject;
|
| 5 | import com.mojang.util.UndashedUuid;
|
| 6 | import java.net.URI;
|
| 7 | import java.time.Instant;
|
| 8 | import java.util.UUID;
|
| 9 | import java.util.function.Function;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import org.jetbrains.annotations.Contract;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class JsonUtils {
|
| 17 | public static <T> T getRequired(final String key, final JsonObject node, final Function<JsonObject, T> parser) {
|
| 18 | JsonElement property = node.get(key);
|
| 19 | if (property == null || property.isJsonNull()) {
|
| 20 | throw new IllegalStateException("Missing required property: " + key);
|
| 21 | } else if (!property.isJsonObject()) {
|
| 22 | throw new IllegalStateException("Required property " + key + " was not a JsonObject as espected");
|
| 23 | } else {
|
| 24 | return parser.apply(property.getAsJsonObject());
|
| 25 | }
|
| 26 | }
|
| 27 |
|
| 28 | public static <T> @Nullable T getOptional(final String key, final JsonObject node, final Function<JsonObject, T> parser) {
|
| 29 | JsonElement property = node.get(key);
|
| 30 | if (property == null || property.isJsonNull()) {
|
| 31 | return null;
|
| 32 | } else if (!property.isJsonObject()) {
|
| 33 | throw new IllegalStateException("Required property " + key + " was not a JsonObject as espected");
|
| 34 | } else {
|
| 35 | return parser.apply(property.getAsJsonObject());
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | public static String getRequiredString(final String key, final JsonObject node) {
|
| 40 | String result = getStringOr(key, node, null);
|
| 41 | if (result == null) {
|
| 42 | throw new IllegalStateException("Missing required property: " + key);
|
| 43 | } else {
|
| 44 | return result;
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | @Contract("_,_,!null->!null;_,_,null->_")
|
| 49 | public static @Nullable String getStringOr(final String key, final JsonObject node, final @Nullable String defaultValue) {
|
| 50 | JsonElement element = node.get(key);
|
| 51 | if (element != null) {
|
| 52 | return element.isJsonNull() ? defaultValue : element.getAsString();
|
| 53 | } else {
|
| 54 | return defaultValue;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | public static URI getRequiredUri(final String key, final JsonObject node) {
|
| 59 | URI uri = getUriNullable(key, node);
|
| 60 | if (uri == null) {
|
| 61 | throw new IllegalStateException("Missing required URI property: " + key);
|
| 62 | } else {
|
| 63 | return uri;
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | public static @Nullable URI getUriNullable(final String key, final JsonObject node) {
|
| 68 | String string = getStringOr(key, node, null);
|
| 69 | return string != null && !string.isEmpty() ? URI.create(string) : null;
|
| 70 | }
|
| 71 |
|
| 72 | @Contract("_,_,!null->!null;_,_,null->_")
|
| 73 | public static @Nullable UUID getUuidOr(final String key, final JsonObject node, final @Nullable UUID defaultValue) {
|
| 74 | String uuidAsString = getStringOr(key, node, null);
|
| 75 | return uuidAsString == null ? defaultValue : UndashedUuid.fromStringLenient(uuidAsString);
|
| 76 | }
|
| 77 |
|
| 78 | public static int getIntOr(final String key, final JsonObject node, final int defaultValue) {
|
| 79 | JsonElement element = node.get(key);
|
| 80 | if (element != null) {
|
| 81 | return element.isJsonNull() ? defaultValue : element.getAsInt();
|
| 82 | } else {
|
| 83 | return defaultValue;
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | public static long getLongOr(final String key, final JsonObject node, final long defaultValue) {
|
| 88 | JsonElement element = node.get(key);
|
| 89 | if (element != null) {
|
| 90 | return element.isJsonNull() ? defaultValue : element.getAsLong();
|
| 91 | } else {
|
| 92 | return defaultValue;
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | public static boolean getBooleanOr(final String key, final JsonObject node, final boolean defaultValue) {
|
| 97 | JsonElement element = node.get(key);
|
| 98 | if (element != null) {
|
| 99 | return element.isJsonNull() ? defaultValue : element.getAsBoolean();
|
| 100 | } else {
|
| 101 | return defaultValue;
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | public static Instant getDateOr(final String key, final JsonObject node) {
|
| 106 | JsonElement element = node.get(key);
|
| 107 | return element != null ? Instant.ofEpochMilli(Long.parseLong(element.getAsString())) : Instant.EPOCH;
|
| 108 | }
|
| 109 |
|
| 110 | @Contract("_,_,!null->!null;_,_,null->_")
|
| 111 | public static @Nullable Instant getEpochMillisOr(final String key, final JsonObject node, final @Nullable Instant defaultValue) {
|
| 112 | JsonElement element = node.get(key);
|
| 113 | return element != null && !element.isJsonNull() ? Instant.ofEpochMilli(Long.parseLong(element.getAsString())) : defaultValue;
|
| 114 | }
|
| 115 | }
|
| 116 |
|