|
RuneHive-Game
|
A util class used for constructing and writing JSON files.
More...
Public Member Functions | |
| JsonObject | current () |
| JsonSaver () | |
Creates a new JsonSaver that can have an infinite amount of tables. | |
| JsonSaver (boolean singletonTable) | |
Creates a new JsonSaver. | |
| void | publish (String path) |
Publishes the contents of this JsonSaver to the file at path. | |
| Gson | serializer () |
| Gets the internal gson that allows for serialization. | |
| void | split () |
Adds the data within currentWriter to the internal JsonArray then instantiates a new writer, effectively splitting the data up into tables. | |
| String | toString () |
| Invocation of this function is expensive and should be cached or avoided whenever possible. | |
Private Attributes | |
| final JsonArray | array = new JsonArray() |
| An array that will hold all of our sub-tables. | |
| JsonObject | currentWriter = new JsonObject() |
A writer that acts as a sub-table, instantiated after each split(). | |
| final Gson | serializer = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create() |
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. | |
A util class used for constructing and writing JSON files.
And an example of usage:
JsonSaver json = new JsonSaver(); for (Player player : players) { json.current().addProperty("name", player.getUsername()); json.current().addProperty("value1", 1); json.current().addProperty("value2", true); json.split(); } json.publish("./data/some_player_database.json");
Definition at line 32 of file JsonSaver.java.
| com.runehive.util.parser.JsonSaver.JsonSaver | ( | ) |
Creates a new JsonSaver that can have an infinite amount of tables.
Definition at line 60 of file JsonSaver.java.
| com.runehive.util.parser.JsonSaver.JsonSaver | ( | boolean | singletonTable | ) |
Creates a new JsonSaver.
| singletonTable | determines if only one table can exist. |
Definition at line 70 of file JsonSaver.java.
References singletonTable.
| JsonObject com.runehive.util.parser.JsonSaver.current | ( | ) |
Definition at line 79 of file JsonSaver.java.
References currentWriter.
Referenced by com.runehive.game.world.items.ItemDefinition.dump(), and com.runehive.game.world.items.ItemDefinition.dump().
| void com.runehive.util.parser.JsonSaver.publish | ( | String | path | ) |
Publishes the contents of this JsonSaver to the file at path.
| path | the path to publish the contents. |
Definition at line 90 of file JsonSaver.java.
References toString().
Referenced by com.runehive.game.world.items.ItemDefinition.dump(), and com.runehive.game.world.items.ItemDefinition.dump().
| Gson com.runehive.util.parser.JsonSaver.serializer | ( | ) |
Gets the internal gson that allows for serialization.
Definition at line 103 of file JsonSaver.java.
References serializer.
| void com.runehive.util.parser.JsonSaver.split | ( | ) |
Adds the data within currentWriter to the internal JsonArray then instantiates a new writer, effectively splitting the data up into tables.
If this instance is a singletonTable, throws an IllegalStateException.
| IllegalStateException | if this instance is only allowed one internal table. |
Definition at line 116 of file JsonSaver.java.
References array, currentWriter, and singletonTable.
Referenced by com.runehive.game.world.items.ItemDefinition.dump(), com.runehive.game.world.items.ItemDefinition.dump(), and toString().
| String com.runehive.util.parser.JsonSaver.toString | ( | ) |
Invocation of this function is expensive and should be cached or avoided whenever possible.
This function will call split() if the currentWriter has unsplit elements added to it.
This function returns the contents of this class in pretty printed JSON format.
Definition at line 133 of file JsonSaver.java.
References array, currentWriter, serializer, singletonTable, and split().
Referenced by publish().
|
private |
An array that will hold all of our sub-tables.
Definition at line 43 of file JsonSaver.java.
Referenced by split(), and toString().
|
private |
A writer that acts as a sub-table, instantiated after each split().
Definition at line 54 of file JsonSaver.java.
Referenced by current(), split(), and toString().
|
private |
A gson builder, allows us to turn Objects into JSON format and vice-versa.
Definition at line 38 of file JsonSaver.java.
Referenced by com.runehive.game.world.items.ItemDefinition.dump(), com.runehive.game.world.items.ItemDefinition.dump(), serializer(), and toString().
|
private |
The flag that determines if only one table can exist.
Definition at line 48 of file JsonSaver.java.
Referenced by JsonSaver(), split(), and toString().