Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ConvertJson {
- public static <T> String convertToJsonList(String key, List<T> arrayList) {
- Map<String, List<String>> maps = new HashMap<>();
- Gson gson = new Gson();
- List<String> serilizedList = new ArrayList<>();
- if (arrayList != null)
- for (T list : arrayList) {
- if (list instanceof Location)
- serilizedList.add(SerializeUtil.serializeLoc((Location) list));
- if (list instanceof UUID)
- serilizedList.add(String.valueOf(list));
- }
- maps.put(key, serilizedList);
- return gson.toJson(maps);
- }
- public static <T> ArrayList<T> convertFromJsonList(Class<T> classof, String Inputmap) {
- ArrayList<T> arrayList = new ArrayList<>();
- Gson gson = new Gson();
- final Map<String, List<Object>> map = gson.fromJson(Inputmap, (Type) Map.class);
- if (map != null) {
- Map<String, List<Object>> mapList = new HashMap<>(map);
- for (Map.Entry<String, List<Object>> entry : mapList.entrySet())
- for (Object deserilizedList : entry.getValue()) {
- if (classof == Location.class && isLocation(deserilizedList) != null)
- arrayList.add((T) isLocation(deserilizedList));
- if (classof == UUID.class)
- arrayList.add((T) UUID.fromString(deserilizedList.toString()));
- }
- }
- return arrayList;
- }
- public static Location isLocation(Object raw) {
- String[] parts;
- if (!raw.toString().contains(" "))
- return null;
- else {
- int length = (parts = raw.toString().split(" ")).length;
- if (length == 4) {
- final String world = parts[0];
- final World bukkitWorld = Bukkit.getWorld(world);
- if (bukkitWorld == null)
- return null;
- if (!parts[1].matches("[-+]?\\d+") && !parts[2].matches("[-+]?\\d+") && !parts[3].matches("[-+]?\\d+"))
- return null;
- else {
- int x = Integer.parseInt(parts[1]), y = Integer.parseInt(parts[2]), z = Integer.parseInt(parts[3]);
- return new Location(bukkitWorld, x, y, z);
- }
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement