Advertisement
NeutronStars_

JSONReader

Feb 15th, 2019
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. package your.package;
  2.  
  3. import org.json.JSONArray;
  4. import org.json.JSONObject;
  5.  
  6. import java.io.*;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11.  
  12. /**
  13.  * Created by NeutronStars on 14/07/2017
  14.  *
  15.  */
  16. public final class JSONReader
  17. {
  18.     private final String json;
  19.  
  20.     public JSONReader(String path) throws IOException
  21.     {
  22.         this(new File(path));
  23.     }
  24.  
  25.     public JSONReader(File file) throws IOException
  26.     {
  27.         this(new InputStreamReader(new FileInputStream(file)));
  28.     }
  29.  
  30.     public JSONReader(Reader reader) throws IOException
  31.     {
  32.         this(new BufferedReader(reader));
  33.     }
  34.  
  35.     public JSONReader(BufferedReader reader) throws IOException
  36.     {
  37.         json = load(reader);
  38.     }
  39.  
  40.     private String load(BufferedReader reader) throws IOException
  41.     {
  42.         StringBuilder builder = new StringBuilder();
  43.  
  44.         while(reader.ready()) builder.append(reader.readLine());
  45.  
  46.         reader.close();
  47.  
  48.         return builder.length() == 0 ? "[]" : builder.toString();
  49.     }
  50.  
  51.     public static <E> List<E> toList(String path)
  52.     {
  53.         return toList(new File(path));
  54.     }
  55.  
  56.     public static <E> List<E> toList(File file)
  57.     {
  58.         try
  59.         {
  60.             return toList(new InputStreamReader(new FileInputStream(file)));
  61.         }
  62.         catch(IOException e)
  63.         {
  64.             e.printStackTrace();
  65.         }
  66.         return new ArrayList<>();
  67.     }
  68.  
  69.     public static <E> List<E> toList(Reader reader)
  70.     {
  71.         return toList(new BufferedReader(reader));
  72.     }
  73.  
  74.     public static <E> List<E> toList(BufferedReader bufferedReader)
  75.     {
  76.         List<E> list= new ArrayList<>();
  77.  
  78.         try
  79.         {
  80.             JSONReader reader = new JSONReader(bufferedReader);
  81.             JSONArray array = reader.toJSONArray();
  82.             for(int i = 0; i < array.length(); i++)
  83.             {
  84.                 try
  85.                 {
  86.                     list.add((E) array.get(i));
  87.                 }catch(ClassCastException e){}
  88.             }
  89.         }
  90.         catch(IOException e)
  91.         {
  92.             e.printStackTrace();
  93.         }
  94.  
  95.         return list;
  96.     }
  97.  
  98.     public static <V> Map<String, V> toMap(String path)
  99.     {
  100.         return toMap(new File(path));
  101.     }
  102.  
  103.     public static <V> Map<String, V> toMap(File file)
  104.     {
  105.         try
  106.         {
  107.             return toMap(new InputStreamReader(new FileInputStream(file)));
  108.         }
  109.         catch(IOException e)
  110.         {
  111.             e.printStackTrace();
  112.         }
  113.         return new HashMap<>();
  114.     }
  115.  
  116.     public static <V> Map<String, V> toMap(Reader reader)
  117.     {
  118.         return toMap(new BufferedReader(reader));
  119.     }
  120.  
  121.     public static <V> Map<String, V> toMap(BufferedReader bufferedReader)
  122.     {
  123.         Map<String, V> map = new HashMap<>();
  124.  
  125.         try
  126.         {
  127.             JSONReader reader = new JSONReader(bufferedReader);
  128.             JSONObject object = reader.toJSONObject();
  129.             for(String key : object.keySet())
  130.             {
  131.                 Object obj = object.get(key);
  132.                 try
  133.                 {
  134.                     map.put(key, (V) object.get(key));
  135.                 }
  136.                 catch(ClassCastException e) {}
  137.             }
  138.         }
  139.         catch(IOException e)
  140.         {
  141.             e.printStackTrace();
  142.         }
  143.  
  144.         return map;
  145.     }
  146.  
  147.     public JSONArray toJSONArray()
  148.     {
  149.         return new JSONArray(json);
  150.     }
  151.  
  152.     public JSONObject toJSONObject()
  153.     {
  154.         return new JSONObject(json);
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement