Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class JsonUtils {
- public static JsonObject getObjectOrEmpty(JsonElement element) {
- if (isObject(element)) {
- return element.getAsJsonObject();
- }
- return new JsonObject();
- }
- public static JsonObject getObject(JsonElement element) {
- if (isObject(element)) {
- return element.getAsJsonObject();
- }
- return null;
- }
- public static Integer getIntegerOrDefault(JsonObject object, String path, Integer def) {
- if (isNumber(object, path)) {
- return object.get(path).getAsInt();
- }
- return def;
- }
- public static Long getLongOrDefault(JsonObject object, String path, Long def) {
- if (isNumber(object, path)) {
- return object.get(path).getAsLong();
- }
- return def;
- }
- public static Double getDoubleOrDefault(JsonObject object, String path, Double def) {
- if (isNumber(object, path)) {
- return object.get(path).getAsDouble();
- }
- return def;
- }
- public static Float getFloatOrDefault(JsonObject object, String path, Float def) {
- if (isNumber(object, path)) {
- return object.get(path).getAsFloat();
- }
- return def;
- }
- public static String getStringOrDefault(JsonObject object, String path, String def) {
- if (isString(object, path)) {
- return object.get(path).getAsString();
- }
- return def;
- }
- public static Boolean getBooleanOrDefault(JsonObject object, String path, Boolean def) {
- if (isBoolean(object, path)) {
- return object.get(path).getAsBoolean();
- }
- return def;
- }
- public static Integer getIntegerOrDefault(JsonElement element, Integer def) {
- if (isNumber(element)) {
- return element.getAsInt();
- }
- return def;
- }
- public static Long getLongOrDefault(JsonElement element, Long def) {
- if (isNumber(element)) {
- return element.getAsLong();
- }
- return def;
- }
- public static Double getDoubleOrDefault(JsonElement element, Double def) {
- if (isNumber(element)) {
- return element.getAsDouble();
- }
- return def;
- }
- public static Float getFloatOrDefault(JsonElement element, Float def) {
- if (isNumber(element)) {
- return element.getAsFloat();
- }
- return def;
- }
- public static String getStringOrDefault(JsonElement element, String def) {
- if (isString(element)) {
- return element.getAsString();
- }
- return def;
- }
- public static Boolean getBooleanOrDefault(JsonElement element, Boolean def) {
- if (isBoolean(element)) {
- return element.getAsBoolean();
- }
- return def;
- }
- public static Integer getInteger(JsonObject object, String path) {
- return getIntegerOrDefault(object, path, null);
- }
- public static Long getLong(JsonObject object, String path) {
- return getLongOrDefault(object, path, null);
- }
- public static Double getDouble(JsonObject object, String path) {
- return getDoubleOrDefault(object, path, null);
- }
- public static Float getFloat(JsonObject object, String path) {
- return getFloatOrDefault(object, path, null);
- }
- public static String getString(JsonObject object, String path) {
- return getStringOrDefault(object, path, null);
- }
- public static Boolean getBoolean(JsonObject object, String path) {
- return getBooleanOrDefault(object, path, null);
- }
- public static Integer getInteger(JsonElement element) {
- return getIntegerOrDefault(element, null);
- }
- public static Long getLong(JsonElement element) {
- return getLongOrDefault(element, null);
- }
- public static Double getDouble(JsonElement element) {
- return getDoubleOrDefault(element, null);
- }
- public static Float getFloat(JsonElement element) {
- return getFloatOrDefault(element, null);
- }
- public static String getString(JsonElement element) {
- return getStringOrDefault(element, null);
- }
- public static Boolean getBoolean(JsonElement element) {
- return getBooleanOrDefault(element, null);
- }
- public static JsonArray getArray(JsonElement element) {
- if ((element != null) && (element.isJsonArray())) {
- return element.getAsJsonArray();
- }
- return null;
- }
- public static JsonArray getArray(JsonObject object, String path) {
- if (isArray(object, path)) {
- return object.get(path).getAsJsonArray();
- }
- return null;
- }
- public static JsonObject getObject(JsonObject object, String path) {
- if (isObject(object, path)) {
- return object.get(path).getAsJsonObject();
- }
- return null;
- }
- public static boolean isNumber(JsonObject object, String path) {
- if (containsElement(object, path)) {
- return isNumber(object.get(path));
- }
- return false;
- }
- public static boolean isBoolean(JsonObject object, String path) {
- if (containsElement(object, path)) {
- return isBoolean(object.get(path));
- }
- return false;
- }
- public static boolean isString(JsonObject object, String path) {
- if (containsElement(object, path)) {
- return isString(object.get(path));
- }
- return false;
- }
- public static boolean isArray(JsonObject object, String path) {
- if (containsElement(object, path)) {
- return isArray(object.get(path));
- }
- return false;
- }
- public static boolean isObject(JsonObject object, String path) {
- if (containsElement(object, path)) {
- return isObject(object.get(path));
- }
- return false;
- }
- public static boolean isNumber(JsonElement element) {
- return (element != null) && (element.isJsonPrimitive()) && (element.getAsJsonPrimitive().isNumber());
- }
- public static boolean isBoolean(JsonElement element) {
- return (element != null) && (element.isJsonPrimitive()) && (element.getAsJsonPrimitive().isBoolean());
- }
- public static boolean isString(JsonElement element) {
- return (element != null) && (element.isJsonPrimitive()) && (element.getAsJsonPrimitive().isString());
- }
- public static boolean isArray(JsonElement element) {
- return (element != null) && (element.isJsonArray());
- }
- public static boolean isObject(JsonElement element) {
- return (element != null) && (element.isJsonObject());
- }
- public static boolean containsElement(JsonObject object, String path) {
- return (object != null) && (path != null) && (!path.isEmpty()) && (object.has(path));
- }
- public static JsonElement parse(File file)
- throws FileNotFoundException, JsonSyntaxException, JsonIOException, UnsupportedEncodingException {
- return parse(new FileInputStream(file));
- }
- public static JsonElement parse(InputStream stream)
- throws JsonSyntaxException, JsonIOException, UnsupportedEncodingException {
- return new JsonParser().parse(new InputStreamReader(stream, "Cp1251"));
- }
- public static JsonElement parse(String str)
- throws JsonSyntaxException {
- return new JsonParser().parse(str);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement