RuneHive-Game
Loading...
Searching...
No Matches
GsonParser.java
Go to the documentation of this file.
1package com.runehive.util.parser;
2
3import com.google.gson.*;
4import com.google.gson.stream.JsonReader;
5import org.apache.logging.log4j.LogManager;
6import org.apache.logging.log4j.Logger;
7
8import java.io.FileReader;
9
10/**
11 * This class provides an easy to use google gson parser specifically designed for parsing JSON files.
12 *
13 * @author Seven
14 * @author Daniel
15 */
16public abstract class GsonParser extends GenericParser {
17
18 private static final Logger logger = LogManager.getLogger(GsonParser.class);
19
20 /** The {@code Gson} object. */
21 protected transient Gson builder;
22
23 /**
24 * Creates a new {@code GsonParser}.
25 *
26 * @param path The specified path of the json file to parse.
27 */
28 public GsonParser(String path) {
29 this(path, true);
30 }
31
32 /**
33 * Creates a new {@code GsonParser}.
34 *
35 * @param path The specified path of the json file to parse.
36 * @param log The flag that denotes to log messages.
37 */
38 public GsonParser(String path, boolean log) {
39 super(path, ".json", log);
40 this.builder = new GsonBuilder().create();
41 }
42
43 public void initialize(int size) {
44 }
45
46 /**
47 * The method allows a user to modify the data as its being parsed.
48 *
49 * @param data The {@code JsonObject} that contains all serialized information.
50 */
51 protected abstract void parse(JsonObject data);
52
53 /**
54 * This method handles what happens after the parser has ended.
55 */
56 protected void onEnd() {
57 }
58
59 @Override
60 public final void deserialize() {
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 }
90
91}
int index
The index of the current line being parsed.
GenericParser(String path, String extension, boolean log)
Creates a new GenericParser.
final Path path
The path of the file to parse.
GsonParser(String path)
Creates a new GsonParser.
transient Gson builder
The Gson object.
GsonParser(String path, boolean log)
Creates a new GsonParser.
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.
final void deserialize()
The method that deserializes the file information.