RuneHive-Game
Loading...
Searching...
No Matches
com.runehive.util.parser.JsonSaver Class Reference

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.

Detailed Description

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");
Author
lare96 http://github.org/lare96

Definition at line 32 of file JsonSaver.java.

Constructor & Destructor Documentation

◆ JsonSaver() [1/2]

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.

60 {
61 this(false);
62 }

◆ JsonSaver() [2/2]

com.runehive.util.parser.JsonSaver.JsonSaver ( boolean singletonTable)

Creates a new JsonSaver.

Parameters
singletonTabledetermines if only one table can exist.

Definition at line 70 of file JsonSaver.java.

70 {
71 this.singletonTable = singletonTable;
72 }

References singletonTable.

Member Function Documentation

◆ current()

JsonObject com.runehive.util.parser.JsonSaver.current ( )

Definition at line 79 of file JsonSaver.java.

79 {
80 return currentWriter;
81 }

References currentWriter.

Referenced by com.runehive.game.world.items.ItemDefinition.dump(), and com.runehive.game.world.items.ItemDefinition.dump().

Here is the caller graph for this function:

◆ publish()

void com.runehive.util.parser.JsonSaver.publish ( String path)

Publishes the contents of this JsonSaver to the file at path.

Parameters
paththe path to publish the contents.

Definition at line 90 of file JsonSaver.java.

90 {
91 try (FileWriter fw = new FileWriter(path)) {
92 fw.write(toString());
93 } catch (final Exception e) {
94 e.printStackTrace();
95 }
96 }

References toString().

Referenced by com.runehive.game.world.items.ItemDefinition.dump(), and com.runehive.game.world.items.ItemDefinition.dump().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializer()

Gson com.runehive.util.parser.JsonSaver.serializer ( )

Gets the internal gson that allows for serialization.

Returns
the internal gson.

Definition at line 103 of file JsonSaver.java.

103 {
104 return serializer;
105 }

References serializer.

◆ split()

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.

Exceptions
IllegalStateExceptionif this instance is only allowed one internal table.

Definition at line 116 of file JsonSaver.java.

116 {
117 Preconditions.checkState(!singletonTable, "JsonSaver instance is a singleton table!");
118 array.add(currentWriter);
119 currentWriter = new JsonObject();
120 }

References array, currentWriter, and singletonTable.

Referenced by com.runehive.game.world.items.ItemDefinition.dump(), com.runehive.game.world.items.ItemDefinition.dump(), and toString().

Here is the caller graph for this function:

◆ 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.

133 {
134 if (singletonTable) {
135 return serializer.toJson(currentWriter);
136 }
137 if (currentWriter.entrySet().size() > 0) {
138 split();
139 }
140 return serializer.toJson(array);
141 }

References array, currentWriter, serializer, singletonTable, and split().

Referenced by publish().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ array

final JsonArray com.runehive.util.parser.JsonSaver.array = new JsonArray()
private

An array that will hold all of our sub-tables.

Definition at line 43 of file JsonSaver.java.

Referenced by split(), and toString().

◆ currentWriter

JsonObject com.runehive.util.parser.JsonSaver.currentWriter = new JsonObject()
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().

◆ serializer

final Gson com.runehive.util.parser.JsonSaver.serializer = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create()
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().

◆ singletonTable

final boolean com.runehive.util.parser.JsonSaver.singletonTable
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().


The documentation for this class was generated from the following file: