TerrificTable55

Untitled

Nov 13th, 2022
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. public class JsonUtils {
  2.  
  3.     /**
  4.      * Read File contents into JsonObject
  5.      * @param path path to a file that should be read
  6.      * @return JsonObject with contents of file
  7.      */
  8.     public static JsonObject readJsonFile(String path) {
  9.         String json = FileIOUtils.readFile(path);
  10.         return convertStringToJsonObject(json);
  11.     }
  12.  
  13.     /**
  14.      * Converts String to JsonObject
  15.      * @param json_str the Json String to be returned as JsonObject
  16.      * @return JsonObject
  17.      */
  18.     public static JsonObject convertStringToJsonObject(String json_str) {
  19.         return new Gson().fromJson(json_str, JsonObject.class);
  20.     }
  21.  
  22.     public static ArrayList<String> jsonObjectToStringArrayList(JsonObject in) {
  23.         ArrayList<String> res = new ArrayList<>();
  24.  
  25.         for (JsonElement jsonElm : in.getAsJsonArray()) {
  26.             System.out.println(jsonElm);
  27.             res.add(jsonElm.toString());
  28.         }
  29.  
  30.         return res;
  31.     }
  32.  
  33.  
  34.     /**
  35.      * prints out a test json object to test if json functions work
  36.      * @param json the JsonObject to print out
  37.      */
  38.     public static void debug(JsonObject json) {
  39.         if (NoWay.dev) { // only print if dev version
  40.             System.out.println("Non Formatted: " + new Gson().toJson(json));
  41.             System.out.println("Formatted: " + formatJsonObjectToString(json));
  42.             System.out.println("Conv to JsonObj: " + convertStringToJsonObject(formatJsonObjectToString(json)).getClass() + " -> " + convertStringToJsonObject(formatJsonObjectToString(json)));
  43.         }
  44.     }
  45.  
  46.  
  47.     /**
  48.      * Format JsonObject
  49.      * Input: {"a":"b","c":"d"}
  50.      * Output: {
  51.      *     "a": "b",
  52.      *     "c": "d"
  53.      * }
  54.      *
  55.      * @param jsonObject JsonObject input
  56.      * @return JsonObject formatted and converted to String
  57.      */
  58.     public static String formatJsonObjectToString(JsonObject jsonObject) {
  59.         if (jsonObject.isJsonNull() || jsonObject.equals(new JsonObject())) return "{ }";
  60.  
  61.         String gson_obj = new Gson().toJson(jsonObject);
  62.         StringBuilder sb = new StringBuilder();
  63.         int indent = 0;
  64.         for (int i = 0; i < gson_obj.length(); i++) {
  65.             char c = gson_obj.charAt(i);
  66.             if (c == '{') {
  67.                 sb.append(c);
  68.                 if (gson_obj.charAt(i + 1) == '}')
  69.                     sb.append(" ");
  70.                 else {
  71.                     sb.append("\n");
  72.                     indent++;
  73.                     for (int j=0; j < indent; j++)
  74.                         sb.append("\t");
  75.                 }
  76.             } else if (c == '}') {
  77.                 if (gson_obj.charAt(i - 1) != '{') {
  78.                     sb.append("\n");
  79.                     indent--;
  80.                     for (int j=0; j < indent; j++)
  81.                         sb.append("\t");
  82.                 }
  83.                 sb.append(c);
  84.             } else if (c == ',') {
  85.                 sb.append(c);
  86.                 sb.append("\n");
  87.                 for (int j=0; j < indent; j++)
  88.                     sb.append("\t");
  89.             } else if (c == ':') {
  90.                 sb.append(c);
  91.                 sb.append(" ");
  92.             } else {
  93.                 sb.append(c);
  94.             }
  95.         }
  96.         return sb.toString();
  97.     }
  98.  
  99.     /**
  100.      * String to json to formatJsonObjectToString(JsonObject)
  101.      * @param jsonString String input
  102.      * @return String output
  103.      */
  104.     public static String formatJsonString(String jsonString) {
  105.         return formatJsonObjectToString(convertStringToJsonObject(jsonString));
  106.     }
  107.  
  108.  
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment