Advertisement
Dori_mon

Untitled

Jul 1st, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.54 KB | None | 0 0
  1. package me.dori_mon.json;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.Writer;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11.  
  12. import com.google.gson.Gson;
  13. import com.google.gson.GsonBuilder;
  14. import com.google.gson.JsonArray;
  15. import com.google.gson.JsonElement;
  16. import com.google.gson.JsonIOException;
  17. import com.google.gson.JsonObject;
  18. import com.google.gson.JsonSyntaxException;
  19.  
  20. public class JsonFile {
  21.    
  22.     private File file;
  23.     private JsonObject json;
  24.     private Map<String, JsonElement> defaults = new HashMap<>();
  25.     Gson gson;
  26.    
  27.     /**
  28.      * Creates a new JsonFile and sets it up (see {@link JsonFile#setup()}.
  29.      * @param fileName the name of the file.
  30.      */
  31.     public JsonFile(final String fileName) {
  32.         this("", fileName);
  33.     }
  34.    
  35.     /**
  36.      * Creates a new JsonFile and sets it up (see {@link JsonFile#setup()}.
  37.      * @param path the path of the file.
  38.      * @param fileName the name of the file.
  39.      */
  40.     public JsonFile(final String path, final String fileName) {
  41.         this(path, fileName, new GsonBuilder().disableHtmlEscaping().setPrettyPrinting());
  42.     }
  43.    
  44.     /**
  45.      * Creates a new JsonFile and sets it up (see {@link JsonFile#setup()}.
  46.      * @param path the path of the file.
  47.      * @param fileName the name of the file.
  48.      * @param gsonBuilder a gsonBuilder to use for formatting.
  49.      */
  50.     public JsonFile(final String path, final String fileName, final GsonBuilder gsonBuilder) {
  51.         this.gson = gsonBuilder.create();
  52.         if (!path.equals("")) {
  53.             File pathFile = new File(path);
  54.             if (!pathFile.exists())
  55.                 pathFile.mkdirs();
  56.         }
  57.        
  58.         String finalFileName = new String(fileName);
  59.         if (!fileName.endsWith(".json"))
  60.             finalFileName = finalFileName + ".json";
  61.        
  62.         if (!path.equals(""))
  63.             file = new File(path, finalFileName);
  64.         else
  65.             file = new File(finalFileName);
  66.        
  67.         if (!file.exists())
  68.             try {
  69.                 file.createNewFile();
  70.             } catch (IOException e) {
  71.                 e.printStackTrace();
  72.                 return;
  73.             }
  74.         setup();
  75.     }
  76.    
  77.     /**
  78.      * Saves the edited JsonObject to the File.
  79.      */
  80.     public void save() {
  81.         try {
  82.             Writer writer = new FileWriter(file);
  83.             String parsedJson = gson.toJson(json);
  84.             writer.write(parsedJson);
  85.             writer.flush();
  86.             writer.close();
  87.         } catch (IOException e) {
  88.             System.err.println("Could not save JsonFile named " + file.getName() + "!");
  89.             e.printStackTrace();
  90.             return;
  91.         }
  92.     }
  93.    
  94.     /**
  95.      * Sets up the JsonFile, parses and handles defaults.
  96.      */
  97.     public void setup() {
  98.         try {
  99.             JsonElement element = gson.fromJson(new FileReader(file), JsonElement.class);
  100.             if (element == null)
  101.                 json = new JsonObject();
  102.             else
  103.                 json = element.getAsJsonObject();
  104.         } catch (JsonSyntaxException | JsonIOException | FileNotFoundException e) {
  105.             json = new JsonObject();
  106.         }
  107.         if (json == null)
  108.             json = new JsonObject();
  109.         copyDefaults();
  110.     }
  111.    
  112.     /**
  113.      * Sets a key to a value in the JsonFile.
  114.      * @param key the key.
  115.      * @param value the value.
  116.      */
  117.     public void set(String key, Object value) {
  118.         if (containsKey(key))
  119.             json.remove(key);
  120.         json.add(key, gson.toJsonTree(value));
  121.         save();
  122.     }
  123.    
  124.     /**
  125.      * Gets a Value as JsonElement from the JsonFile.
  126.      * @param key the key.
  127.      * @return the value.
  128.      */
  129.     public JsonElement get(String key) {
  130.         return json.get(key);
  131.     }
  132.    
  133.     /**
  134.      * Gets a Value as String from the JsonFile.
  135.      * @param key the key.
  136.      * @return the value.
  137.      */
  138.     public String getAsString(String key) {
  139.         return get(key).getAsString();
  140.     }
  141.    
  142.     /**
  143.      * Gets a Value as Boolean from the JsonFile.
  144.      * @param key the key.
  145.      * @return the value.
  146.      */
  147.     public boolean getAsBoolean(String key) {
  148.         return get(key).getAsBoolean();
  149.     }
  150.    
  151.     /**
  152.      * Gets a Value as Integer from the JsonFile.
  153.      * @param key the key.
  154.      * @return the value.
  155.      */
  156.     public int getAsInt(String key) {
  157.         return get(key).getAsInt();
  158.     }
  159.    
  160.     /**
  161.      * Gets a Value as Double from the JsonFile.
  162.      * @param key the key.
  163.      * @return the value.
  164.      */
  165.     public double getAsDouble(String key) {
  166.         return get(key).getAsDouble();
  167.     }
  168.    
  169.     /**
  170.      * Gets a Value as Long from the JsonFile.
  171.      * @param key the key.
  172.      * @return the value.
  173.      */
  174.     public long getAsLong(String key) {
  175.         return get(key).getAsLong();
  176.     }
  177.    
  178.     /**
  179.      * Gets a Value as JsonObject from the JsonFile.
  180.      * @param key the key.
  181.      * @return the value.
  182.      */
  183.     public JsonObject getAsJsonObject(String key) {
  184.         return get(key).getAsJsonObject();
  185.     }
  186.    
  187.     /**
  188.      * Gets a Value as JsonArray from the JsonFile.
  189.      * @param key the key.
  190.      * @return the value.
  191.      */
  192.     public JsonArray getAsJsonArray(String key) {
  193.         return get(key).getAsJsonArray();
  194.     }
  195.    
  196.     /**
  197.      * Checks if the file exists.
  198.      * @return true if it exists, false if not.
  199.      */
  200.     public boolean exists() {
  201.         return file.exists();
  202.     }
  203.    
  204.     /**
  205.      * Checks if the JsonFile contains a key.
  206.      * @param key the key.
  207.      * @return true if it exists, false if not.
  208.      */
  209.     public boolean containsKey(String key) {
  210.         return json.has(key);
  211.     }
  212.    
  213.     /**
  214.      * Adds a default to the defaults Map.
  215.      * @param key the key of the default.
  216.      * @param value the value of the default.
  217.      */
  218.     public void addDefault(String key, Object value) {
  219.         defaults.put(key, gson.toJsonTree(value));
  220.     }
  221.    
  222.     /**
  223.      * Copies all defaults from the defaults Map to the JsonFile.
  224.      */
  225.     public void copyDefaults() {
  226.         for (String key : defaults.keySet())
  227.             if (!json.has(key))
  228.                 json.add(key, defaults.get(key));
  229.         save();
  230.     }
  231.    
  232.     /**
  233.      * deletes the JsonFile.
  234.      */
  235.     public void delete() {
  236.         file.delete();
  237.     }
  238.    
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement