Advertisement
broken-arrow

Untitled

Feb 11th, 2022
1,225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. public class ConvertJson {
  2.  
  3.  
  4.     public static <T> String convertToJsonList(String key, List<T> arrayList) {
  5.         Map<String, List<String>> maps = new HashMap<>();
  6.         Gson gson = new Gson();
  7.         List<String> serilizedList = new ArrayList<>();
  8.         if (arrayList != null)
  9.             for (T list : arrayList) {
  10.                 if (list instanceof Location)
  11.                     serilizedList.add(SerializeUtil.serializeLoc((Location) list));
  12.                 if (list instanceof UUID)
  13.                     serilizedList.add(String.valueOf(list));
  14.  
  15.             }
  16.         maps.put(key, serilizedList);
  17.  
  18.         return gson.toJson(maps);
  19.     }
  20.  
  21.     public static <T> ArrayList<T> convertFromJsonList(Class<T> classof, String Inputmap) {
  22.         ArrayList<T> arrayList = new ArrayList<>();
  23.         Gson gson = new Gson();
  24.         final Map<String, List<Object>> map = gson.fromJson(Inputmap, (Type) Map.class);
  25.         if (map != null) {
  26.             Map<String, List<Object>> mapList = new HashMap<>(map);
  27.             for (Map.Entry<String, List<Object>> entry : mapList.entrySet())
  28.                 for (Object deserilizedList : entry.getValue()) {
  29.  
  30.                     if (classof == Location.class && isLocation(deserilizedList) != null)
  31.                         arrayList.add((T) isLocation(deserilizedList));
  32.                     if (classof == UUID.class)
  33.                         arrayList.add((T) UUID.fromString(deserilizedList.toString()));
  34.                 }
  35.         }
  36.         return arrayList;
  37.     }
  38.  
  39.     public static Location isLocation(Object raw) {
  40.         String[] parts;
  41.         if (!raw.toString().contains(" "))
  42.             return null;
  43.         else {
  44.             int length = (parts = raw.toString().split(" ")).length;
  45.             if (length == 4) {
  46.                 final String world = parts[0];
  47.                 final World bukkitWorld = Bukkit.getWorld(world);
  48.                 if (bukkitWorld == null)
  49.                     return null;
  50.                 if (!parts[1].matches("[-+]?\\d+") && !parts[2].matches("[-+]?\\d+") && !parts[3].matches("[-+]?\\d+"))
  51.                     return null;
  52.                 else {
  53.                     int x = Integer.parseInt(parts[1]), y = Integer.parseInt(parts[2]), z = Integer.parseInt(parts[3]);
  54.                     return new Location(bukkitWorld, x, y, z);
  55.                 }
  56.             }
  57.         }
  58.         return null;
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement