Add 'Json' suffix to method names in JsonUtils

This commit is contained in:
yushijinhun 2018-07-08 13:43:27 +08:00
parent f2b95f147c
commit 6fe53b6993
No known key found for this signature in database
GPG key ID: 5BC167F73EA558E4
3 changed files with 25 additions and 25 deletions

View file

@ -8,8 +8,8 @@ import static java.util.Collections.unmodifiableMap;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toList;
import static moe.yushi.authlibinjector.util.JsonUtils.asArray;
import static moe.yushi.authlibinjector.util.JsonUtils.asObject;
import static moe.yushi.authlibinjector.util.JsonUtils.asJsonArray;
import static moe.yushi.authlibinjector.util.JsonUtils.asJsonObject;
import static moe.yushi.authlibinjector.util.JsonUtils.parseJson;
import java.io.UncheckedIOException;
import java.security.PublicKey;
@ -26,23 +26,23 @@ public class YggdrasilConfiguration {
public static YggdrasilConfiguration parse(String apiRoot, String metadataResponse) throws UncheckedIOException {
if (!apiRoot.endsWith("/")) apiRoot += "/";
JSONObject response = asObject(parseJson(metadataResponse));
JSONObject response = asJsonObject(parseJson(metadataResponse));
List<String> skinDomains =
ofNullable(response.get("skinDomains"))
.map(it -> asArray(it).stream()
.map(JsonUtils::asString)
.map(it -> asJsonArray(it).stream()
.map(JsonUtils::asJsonString)
.collect(toList()))
.orElse(emptyList());
Optional<PublicKey> decodedPublickey =
ofNullable(response.get("signaturePublickey"))
.map(JsonUtils::asString)
.map(JsonUtils::asJsonString)
.map(KeyUtils::parseSignaturePublicKey);
Map<String, Object> meta =
ofNullable(response.get("meta"))
.map(it -> (Map<String, Object>) new TreeMap<>(asObject(it)))
.map(it -> (Map<String, Object>) new TreeMap<>(asJsonObject(it)))
.orElse(emptyMap());
return new YggdrasilConfiguration(apiRoot, unmodifiableList(skinDomains), unmodifiableMap(meta), decodedPublickey);

View file

@ -9,9 +9,9 @@ import static moe.yushi.authlibinjector.util.IOUtils.asString;
import static moe.yushi.authlibinjector.util.IOUtils.getURL;
import static moe.yushi.authlibinjector.util.IOUtils.newUncheckedIOException;
import static moe.yushi.authlibinjector.util.IOUtils.postURL;
import static moe.yushi.authlibinjector.util.JsonUtils.asArray;
import static moe.yushi.authlibinjector.util.JsonUtils.asObject;
import static moe.yushi.authlibinjector.util.JsonUtils.asString;
import static moe.yushi.authlibinjector.util.JsonUtils.asJsonArray;
import static moe.yushi.authlibinjector.util.JsonUtils.asJsonObject;
import static moe.yushi.authlibinjector.util.JsonUtils.asJsonString;
import static moe.yushi.authlibinjector.util.JsonUtils.parseJson;
import static moe.yushi.authlibinjector.util.LoggingUtils.debug;
import static moe.yushi.authlibinjector.util.LoggingUtils.info;
@ -96,12 +96,12 @@ public class DeprecatedApiHttpd extends NanoHTTPD {
}
debug("[httpd] query uuid of username {0}, response: {1}", username, responseText);
JSONArray response = asArray(parseJson(responseText));
JSONArray response = asJsonArray(parseJson(responseText));
if (response.size() == 0) {
return empty();
} else if (response.size() == 1) {
JSONObject profile = asObject(response.get(0));
return of(asString(profile.get("id")));
JSONObject profile = asJsonObject(response.get(0));
return of(asJsonString(profile.get("id")));
} else {
throw newUncheckedIOException("Invalid JSON: Unexpected response length");
}
@ -121,22 +121,22 @@ public class DeprecatedApiHttpd extends NanoHTTPD {
}
debug("[httpd] query profile of {0}, response: {1}", uuid, responseText);
JSONObject response = asObject(parseJson(responseText));
return asArray(response.get("properties")).stream()
.map(JsonUtils::asObject)
.filter(property -> asString(property.get("name")).equals(propertyName))
JSONObject response = asJsonObject(parseJson(responseText));
return asJsonArray(response.get("properties")).stream()
.map(JsonUtils::asJsonObject)
.filter(property -> asJsonString(property.get("name")).equals(propertyName))
.findFirst()
.map(property -> asString(property.get("value")));
.map(property -> asJsonString(property.get("value")));
}
private Optional<String> obtainTextureUrl(String texturesPayload, String textureType) throws UncheckedIOException {
JSONObject payload = asObject(parseJson(texturesPayload));
JSONObject textures = asObject(payload.get("textures"));
JSONObject payload = asJsonObject(parseJson(texturesPayload));
JSONObject textures = asJsonObject(payload.get("textures"));
return ofNullable(textures.get(textureType))
.map(JsonUtils::asObject)
.map(JsonUtils::asJsonObject)
.map(it -> ofNullable(it.get("url"))
.map(JsonUtils::asString)
.map(JsonUtils::asJsonString)
.orElseThrow(() -> newUncheckedIOException("Invalid JSON: missing texture url")));
}

View file

@ -17,15 +17,15 @@ public final class JsonUtils {
}
}
public static JSONObject asObject(Object json) throws UncheckedIOException {
public static JSONObject asJsonObject(Object json) throws UncheckedIOException {
return assertJson(json, JSONObject.class, "an object");
}
public static JSONArray asArray(Object json) throws UncheckedIOException {
public static JSONArray asJsonArray(Object json) throws UncheckedIOException {
return assertJson(json, JSONArray.class, "an array");
}
public static String asString(Object json) throws UncheckedIOException {
public static String asJsonString(Object json) throws UncheckedIOException {
return assertJson(json, String.class, "a string");
}