Advertisement
Exception_Prototype

Untitled

Aug 10th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.52 KB | None | 0 0
  1. public final class JsonUtils {
  2.  
  3.     public static JsonObject getObjectOrEmpty(JsonElement element) {
  4.         if (isObject(element)) {
  5.             return element.getAsJsonObject();
  6.         }
  7.         return new JsonObject();
  8.     }
  9.  
  10.     public static JsonObject getObject(JsonElement element) {
  11.         if (isObject(element)) {
  12.             return element.getAsJsonObject();
  13.         }
  14.         return null;
  15.     }
  16.  
  17.     public static Integer getIntegerOrDefault(JsonObject object, String path, Integer def) {
  18.         if (isNumber(object, path)) {
  19.             return object.get(path).getAsInt();
  20.         }
  21.         return def;
  22.     }
  23.  
  24.     public static Long getLongOrDefault(JsonObject object, String path, Long def) {
  25.         if (isNumber(object, path)) {
  26.             return object.get(path).getAsLong();
  27.         }
  28.         return def;
  29.     }
  30.  
  31.     public static Double getDoubleOrDefault(JsonObject object, String path, Double def) {
  32.         if (isNumber(object, path)) {
  33.             return object.get(path).getAsDouble();
  34.         }
  35.         return def;
  36.     }
  37.  
  38.     public static Float getFloatOrDefault(JsonObject object, String path, Float def) {
  39.         if (isNumber(object, path)) {
  40.             return object.get(path).getAsFloat();
  41.         }
  42.         return def;
  43.     }
  44.  
  45.     public static String getStringOrDefault(JsonObject object, String path, String def) {
  46.         if (isString(object, path)) {
  47.             return object.get(path).getAsString();
  48.         }
  49.         return def;
  50.     }
  51.  
  52.     public static Boolean getBooleanOrDefault(JsonObject object, String path, Boolean def) {
  53.         if (isBoolean(object, path)) {
  54.             return object.get(path).getAsBoolean();
  55.         }
  56.         return def;
  57.     }
  58.  
  59.     public static Integer getIntegerOrDefault(JsonElement element, Integer def) {
  60.         if (isNumber(element)) {
  61.             return element.getAsInt();
  62.         }
  63.         return def;
  64.     }
  65.  
  66.     public static Long getLongOrDefault(JsonElement element, Long def) {
  67.         if (isNumber(element)) {
  68.             return element.getAsLong();
  69.         }
  70.         return def;
  71.     }
  72.  
  73.     public static Double getDoubleOrDefault(JsonElement element, Double def) {
  74.         if (isNumber(element)) {
  75.             return element.getAsDouble();
  76.         }
  77.         return def;
  78.     }
  79.  
  80.     public static Float getFloatOrDefault(JsonElement element, Float def) {
  81.         if (isNumber(element)) {
  82.             return element.getAsFloat();
  83.         }
  84.         return def;
  85.     }
  86.  
  87.     public static String getStringOrDefault(JsonElement element, String def) {
  88.         if (isString(element)) {
  89.             return element.getAsString();
  90.         }
  91.         return def;
  92.     }
  93.  
  94.     public static Boolean getBooleanOrDefault(JsonElement element, Boolean def) {
  95.         if (isBoolean(element)) {
  96.             return element.getAsBoolean();
  97.         }
  98.         return def;
  99.     }
  100.  
  101.     public static Integer getInteger(JsonObject object, String path) {
  102.         return getIntegerOrDefault(object, path, null);
  103.     }
  104.  
  105.     public static Long getLong(JsonObject object, String path) {
  106.         return getLongOrDefault(object, path, null);
  107.     }
  108.  
  109.     public static Double getDouble(JsonObject object, String path) {
  110.         return getDoubleOrDefault(object, path, null);
  111.     }
  112.  
  113.     public static Float getFloat(JsonObject object, String path) {
  114.         return getFloatOrDefault(object, path, null);
  115.     }
  116.  
  117.     public static String getString(JsonObject object, String path) {
  118.         return getStringOrDefault(object, path, null);
  119.     }
  120.  
  121.     public static Boolean getBoolean(JsonObject object, String path) {
  122.         return getBooleanOrDefault(object, path, null);
  123.     }
  124.  
  125.     public static Integer getInteger(JsonElement element) {
  126.         return getIntegerOrDefault(element, null);
  127.     }
  128.  
  129.     public static Long getLong(JsonElement element) {
  130.         return getLongOrDefault(element, null);
  131.     }
  132.  
  133.     public static Double getDouble(JsonElement element) {
  134.         return getDoubleOrDefault(element, null);
  135.     }
  136.  
  137.     public static Float getFloat(JsonElement element) {
  138.         return getFloatOrDefault(element, null);
  139.     }
  140.  
  141.     public static String getString(JsonElement element) {
  142.         return getStringOrDefault(element, null);
  143.     }
  144.  
  145.     public static Boolean getBoolean(JsonElement element) {
  146.         return getBooleanOrDefault(element, null);
  147.     }
  148.  
  149.     public static JsonArray getArray(JsonElement element) {
  150.         if ((element != null) && (element.isJsonArray())) {
  151.             return element.getAsJsonArray();
  152.         }
  153.         return null;
  154.     }
  155.  
  156.     public static JsonArray getArray(JsonObject object, String path) {
  157.         if (isArray(object, path)) {
  158.             return object.get(path).getAsJsonArray();
  159.         }
  160.         return null;
  161.     }
  162.  
  163.     public static JsonObject getObject(JsonObject object, String path) {
  164.         if (isObject(object, path)) {
  165.             return object.get(path).getAsJsonObject();
  166.         }
  167.         return null;
  168.     }
  169.  
  170.     public static boolean isNumber(JsonObject object, String path) {
  171.         if (containsElement(object, path)) {
  172.             return isNumber(object.get(path));
  173.         }
  174.         return false;
  175.     }
  176.  
  177.     public static boolean isBoolean(JsonObject object, String path) {
  178.         if (containsElement(object, path)) {
  179.             return isBoolean(object.get(path));
  180.         }
  181.         return false;
  182.     }
  183.  
  184.     public static boolean isString(JsonObject object, String path) {
  185.         if (containsElement(object, path)) {
  186.             return isString(object.get(path));
  187.         }
  188.         return false;
  189.     }
  190.  
  191.     public static boolean isArray(JsonObject object, String path) {
  192.         if (containsElement(object, path)) {
  193.             return isArray(object.get(path));
  194.         }
  195.         return false;
  196.     }
  197.  
  198.     public static boolean isObject(JsonObject object, String path) {
  199.         if (containsElement(object, path)) {
  200.             return isObject(object.get(path));
  201.         }
  202.         return false;
  203.     }
  204.  
  205.     public static boolean isNumber(JsonElement element) {
  206.         return (element != null) && (element.isJsonPrimitive()) && (element.getAsJsonPrimitive().isNumber());
  207.     }
  208.  
  209.     public static boolean isBoolean(JsonElement element) {
  210.         return (element != null) && (element.isJsonPrimitive()) && (element.getAsJsonPrimitive().isBoolean());
  211.     }
  212.  
  213.     public static boolean isString(JsonElement element) {
  214.         return (element != null) && (element.isJsonPrimitive()) && (element.getAsJsonPrimitive().isString());
  215.     }
  216.  
  217.     public static boolean isArray(JsonElement element) {
  218.         return (element != null) && (element.isJsonArray());
  219.     }
  220.  
  221.     public static boolean isObject(JsonElement element) {
  222.         return (element != null) && (element.isJsonObject());
  223.     }
  224.  
  225.     public static boolean containsElement(JsonObject object, String path) {
  226.         return (object != null) && (path != null) && (!path.isEmpty()) && (object.has(path));
  227.     }
  228.  
  229.     public static JsonElement parse(File file)
  230.             throws FileNotFoundException, JsonSyntaxException, JsonIOException, UnsupportedEncodingException {
  231.         return parse(new FileInputStream(file));
  232.     }
  233.  
  234.     public static JsonElement parse(InputStream stream)
  235.             throws JsonSyntaxException, JsonIOException, UnsupportedEncodingException {
  236.         return new JsonParser().parse(new InputStreamReader(stream, "Cp1251"));
  237.     }
  238.  
  239.     public static JsonElement parse(String str)
  240.             throws JsonSyntaxException {
  241.         return new JsonParser().parse(str);
  242.     }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement