
io.github.imsejin.common.util.JsonUtils Maven / Gradle / Ivy
package io.github.imsejin.common.util;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
/**
* JSON utilities
*
*
* Utilities that process the JSON.
*
*/
public final class JsonUtils {
private static final Gson gson = new Gson();
private JsonUtils() {
}
/**
* Reads the JSON format string returned by the URL and converts it to {@link JsonObject}.
*
* {@code
* String uriText = "http://cdn.lezhin.com/episodes/snail/1.json?access_token=5be30a25-a044-410c-88b0-19a1da968a64";
* URL url = URI.create(uriText).toURL();
* JsonObject json = JsonUtils.readJsonFromUrl(url);
* }
*
* @param url URL
* @return JSON object
*/
public static JsonObject readJsonFromUrl(URL url) {
BufferedReader reader;
String jsonText;
try {
reader = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
jsonText = readAllJson(reader);
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return JsonParser.parseString(jsonText).getAsJsonObject();
}
/**
* Converts all characters read by {@link java.io.Reader} to string.
*
* @param reader reader
* @return JSON string
*/
private static String readAllJson(BufferedReader reader) {
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
}
/**
* Converts the JSON format string to the specified object.
*
* {@code
* String jsonText = "{\"id\":1011,\"list\":[{\"id\":10,\"name\":\"foo\"},{\"id\":11,\"name\":\"bar\"}]}";
* T t = JsonUtils.toObject(jsonText, T.class);
* }
*
* @param jsonText JSON string
* @param clazz type
* @param type parameter
* @return type casted instance
*/
public static T toObject(String jsonText, Class clazz) {
return gson.fromJson(jsonText, clazz);
}
/**
* Converts {@link JsonArray} to a list of the specified objects.
*
* {@code
* String jsonText = "{\"id\":1011,\"list\":[{\"id\":10,\"name\":\"foo\"},{\"id\":11,\"name\":\"bar\"}]}";
* JsonObject jsonObject = JsonParser.parseString(jsonText).getAsJsonObject();
* JsonArray jsonArray = jsonObject.get("list").getAsJsonArray();
*
* List list = JsonUtils.toList(jsonArray, T.class);
* }
*
* @param jsonArray JSON array
* @param clazz type
* @param type parameter
* @return list that has type casted instances
*/
public static List toList(JsonArray jsonArray, Class clazz) {
return StreamUtils.toStream(jsonArray.iterator())
.map(jsonElement -> gson.fromJson(jsonElement, clazz))
.collect(Collectors.toList());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy