RuneHive-Game
Loading...
Searching...
No Matches
com.runehive.util.parser.GsonParser Class Referenceabstract

This class provides an easy to use google gson parser specifically designed for parsing JSON files. More...

Inheritance diagram for com.runehive.util.parser.GsonParser:
Collaboration diagram for com.runehive.util.parser.GsonParser:

Public Member Functions

final void deserialize ()
 The method that deserializes the file information.
 GsonParser (String path)
 Creates a new GsonParser.
 GsonParser (String path, boolean log)
 Creates a new GsonParser.
void initialize (int size)
Public Member Functions inherited from com.runehive.util.parser.GenericParser
 GenericParser (String path, String extension, boolean log)
 Creates a new GenericParser.
final int getIndex ()
 Gets the current index of the line being parsed.
void onRead ()
 The method called after all the data has been parsed.
void run ()
String toString ()

Protected Member Functions

void onEnd ()
 This method handles what happens after the parser has ended.
abstract void parse (JsonObject data)
 The method allows a user to modify the data as its being parsed.

Protected Attributes

transient Gson builder
 The Gson object.
Protected Attributes inherited from com.runehive.util.parser.GenericParser
int index
 The index of the current line being parsed.
final Path path
 The path of the file to parse.

Static Private Attributes

static final Logger logger = LogManager.getLogger(GsonParser.class)

Detailed Description

This class provides an easy to use google gson parser specifically designed for parsing JSON files.

Author
Seven
Daniel

Definition at line 16 of file GsonParser.java.

Constructor & Destructor Documentation

◆ GsonParser() [1/2]

com.runehive.util.parser.GsonParser.GsonParser ( String path)

Creates a new GsonParser.

Parameters
pathThe specified path of the json file to parse.

Definition at line 28 of file GsonParser.java.

28 {
29 this(path, true);
30 }

References com.runehive.util.parser.GenericParser.path.

◆ GsonParser() [2/2]

com.runehive.util.parser.GsonParser.GsonParser ( String path,
boolean log )

Creates a new GsonParser.

Parameters
pathThe specified path of the json file to parse.
logThe flag that denotes to log messages.

Definition at line 38 of file GsonParser.java.

38 {
39 super(path, ".json", log);
40 this.builder = new GsonBuilder().create();
41 }

References com.runehive.util.parser.GenericParser.log, and com.runehive.util.parser.GenericParser.path.

Member Function Documentation

◆ deserialize()

final void com.runehive.util.parser.GsonParser.deserialize ( )

The method that deserializes the file information.

Reimplemented from com.runehive.util.parser.GenericParser.

Definition at line 60 of file GsonParser.java.

60 {
61 try (final JsonReader reader = new JsonReader(new FileReader(path.toFile()))) {
62 final JsonParser parser = new JsonParser();
63 final JsonElement element = parser.parse(reader);
64
65 if (element.isJsonNull()) {
66 logger.warn(String.format("json document=%s is null", path));
67 return;
68 }
69
70 if (element.isJsonArray()) {
71 final JsonArray array = element.getAsJsonArray();
72 initialize(array.size());
73 for (index = 0; index < array.size(); index++) {
74 final JsonElement element2 = array.get(index);
75 if (element2 == null) continue;
76 if (element2 instanceof JsonNull) continue;
77
78 final JsonObject data = (JsonObject) element2;
79 parse(data);
80 }
81 } else if (element.isJsonObject()) {
82 parse(element.getAsJsonObject());
83 }
84
85 onEnd();
86 } catch (Exception e) {
87 e.printStackTrace();
88 }
89 }
val index

References com.runehive.util.parser.GenericParser.index, initialize(), logger, onEnd(), parse(), and com.runehive.util.parser.GenericParser.path.

Referenced by com.runehive.game.world.entity.mob.npc.definition.NpcDefinition.main().

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

◆ initialize()

void com.runehive.util.parser.GsonParser.initialize ( int size)

Reimplemented in com.runehive.util.parser.old.EquipmentDefinitionParser.

Definition at line 43 of file GsonParser.java.

43 {
44 }

Referenced by deserialize().

Here is the caller graph for this function:

◆ onEnd()

void com.runehive.util.parser.GsonParser.onEnd ( )
protected

This method handles what happens after the parser has ended.

Reimplemented in com.runehive.util.parser.old.EquipmentDefinitionParser.

Definition at line 56 of file GsonParser.java.

56 {
57 }

Referenced by deserialize().

Here is the caller graph for this function:

◆ parse()

abstract void com.runehive.util.parser.GsonParser.parse ( JsonObject data)
abstractprotected

Member Data Documentation

◆ builder

◆ logger

final Logger com.runehive.util.parser.GsonParser.logger = LogManager.getLogger(GsonParser.class)
staticprivate

Definition at line 18 of file GsonParser.java.

Referenced by deserialize().


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