RuneHive-Game
Loading...
Searching...
No Matches
JsonSaver.java
Go to the documentation of this file.
1package com.runehive.util.parser;
2
3import com.google.common.base.Preconditions;
4import com.google.gson.Gson;
5import com.google.gson.GsonBuilder;
6import com.google.gson.JsonArray;
7import com.google.gson.JsonObject;
8
9import java.io.FileWriter;
10
11/**
12 * A util class used for constructing and writing {@code JSON} files.
13 * <p>
14 * <p>
15 * And an example of usage:
16 *
17 * <pre>
18 * JsonSaver json = new JsonSaver();
19 *
20 * for (Player player : players) {
21 * json.current().addProperty(&quot;name&quot;, player.getUsername());
22 * json.current().addProperty(&quot;value1&quot;, 1);
23 * json.current().addProperty(&quot;value2&quot;, true);
24 * json.split();
25 * }
26 *
27 * json.publish(&quot;./data/some_player_database.json&quot;);
28 * </pre>
29 *
30 * @author lare96 <http://github.org/lare96>
31 */
32public final class JsonSaver {
33
34 /**
35 * A gson builder, allows us to turn {@code Object}s into {@code JSON}
36 * format and vice-versa.
37 */
38 private final Gson serializer = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
39
40 /**
41 * An array that will hold all of our sub-tables.
42 */
43 private final JsonArray array = new JsonArray();
44
45 /**
46 * The flag that determines if only one table can exist.
47 */
48 private final boolean singletonTable;
49
50 /**
51 * A writer that acts as a sub-table, instantiated after each
52 * {@code split()}.
53 */
54 private JsonObject currentWriter = new JsonObject();
55
56 /**
57 * Creates a new {@code JsonSaver} that can have an infinite amount of
58 * tables.
59 */
60 public JsonSaver() {
61 this(false);
62 }
63
64 /**
65 * Creates a new {@code JsonSaver}.
66 *
67 * @param singletonTable
68 * determines if only one table can exist.
69 */
70 public JsonSaver(boolean singletonTable) {
71 this.singletonTable = singletonTable;
72 }
73
74 /***
75 * Gets the current {@code JsonObject} that is writing data.
76 *
77 * @return the current writer.
78 */
79 public JsonObject current() {
80 return currentWriter;
81 }
82
83 /**
84 * Publishes the contents of this {@code JsonSaver} to the file at
85 * {@code path}.
86 *
87 * @param path
88 * the path to publish the contents.
89 */
90 public void publish(String path) {
91 try (FileWriter fw = new FileWriter(path)) {
92 fw.write(toString());
93 } catch (final Exception e) {
94 e.printStackTrace();
95 }
96 }
97
98 /**
99 * Gets the internal gson that allows for serialization.
100 *
101 * @return the internal gson.
102 */
103 public Gson serializer() {
104 return serializer;
105 }
106
107 /**
108 * Adds the data within {@code currentWriter} to the internal
109 * {@code JsonArray} then instantiates a new writer, effectively splitting
110 * the data up into tables. If this instance is a {@code singletonTable},
111 * throws an {@code IllegalStateException}.
112 *
113 * @throws IllegalStateException
114 * if this instance is only allowed one internal table.
115 */
116 public void split() {
117 Preconditions.checkState(!singletonTable, "JsonSaver instance is a singleton table!");
118 array.add(currentWriter);
119 currentWriter = new JsonObject();
120 }
121
122 /**
123 * <strong>Invocation of this function is expensive and should be cached or
124 * avoided whenever possible.</strong> This function will call
125 * {@code split()} if the {@code currentWriter} has unsplit elements added
126 * to it.
127 * <p>
128 * <p>
129 * This function returns the contents of this class in pretty printed
130 * {@code JSON} format.
131 */
132 @Override
133 public String toString() {
134 if (singletonTable) {
135 return serializer.toJson(currentWriter);
136 }
137 if (currentWriter.entrySet().size() > 0) {
138 split();
139 }
140 return serializer.toJson(array);
141 }
142}
void split()
Adds the data within currentWriter to the internal JsonArray then instantiates a new writer,...
JsonObject currentWriter
A writer that acts as a sub-table, instantiated after each split().
void publish(String path)
Publishes the contents of this JsonSaver to the file at path.
String toString()
Invocation of this function is expensive and should be cached or avoided whenever possible.
JsonSaver(boolean singletonTable)
Creates a new JsonSaver.
final Gson serializer
A gson builder, allows us to turn Objects into JSON format and vice-versa.
final boolean singletonTable
The flag that determines if only one table can exist.
final JsonArray array
An array that will hold all of our sub-tables.
Gson serializer()
Gets the internal gson that allows for serialization.
JsonSaver()
Creates a new JsonSaver that can have an infinite amount of tables.