-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the EasyJSON wiki!
There are several JSON based classes, but the one that you will need to use EasyJSON is zdoctor.commons.json.JSonReader. To start you would create a new instance of JSonReader. A JSonReader needs either a file or the file name (assumed to be text in a JSON format) or a string (in the JSON format) called with the function fromString.
JSonReader reader = new JSonReader(jsonFile);After creating the reader, you either call readObject or readArray. This is because EasyJSON supports JSON files that only hold items in a JSON Array. If you call the wrong one it will let you know. If the JSON file or string was able to be parsed, it will populate the data, otherwise, it will be empty.
JSonReader reader = new JSonReader(jsonFile);
JSonObject json = reader.readObject();From there, a JSonObject is a HashMap with its keys being strings and its values being JSonValues.
A JSonArray is an ArrayList with each value being and JSonValue.
And that's all. No need to look iterate through the JSON, EasyJSON parses the data itself.
To demonstrate its usage, consider the following JSON file taken from Minecraft:
{
"parent": "block/block",
"elements": [
{ "from": [ 0, 0, 0 ],
"to": [ 16, 16, 16 ],
"faces": {
"down": { "texture": "#down", "cullface": "down" },
"up": { "texture": "#up", "cullface": "up" },
"north": { "texture": "#north", "cullface": "north" },
"south": { "texture": "#south", "cullface": "south" },
"west": { "texture": "#west", "cullface": "west" },
"east": { "texture": "#east", "cullface": "east" }
}
}
]
}Assuming the file name is blocks.json, the code to read it would look something like this:
JSonReader reader = new JSonReader(new File("block.json"));
JSonObject json = reader.readObject();The JSonObject would hold a string under the key "parent" and a JSonArray under "elements".
Like JSON, there are several types of values: string, number, boolean, null, JSON object and JSON Array. A JSonValue holds a variable of an enum of JSonType along with a getter. This can be used to see what type of data it is. This is talked about more in Retrieving Unknown Values.
Retrieving values is trivial, regardless of if you know what type of data they are.
Assuming we know these data types, we would:
JSonReader reader = new JSonReader(jsonFile);
JSonObject json = reader.read();
String parent = json.getString("parent");
JSonArray elements = json.getArray("elements");replace getString or getArray with getBoolean, getNumber, getDecimal or getObject depending on need
As mentioned before, JSonValues have an enum that has their type. In that case, you could implement a switch to get its value and either call the appropriate method or case it yourself. In this example we'll use an if statement.
JSonValue<?> value = json.get("parent");
String parent;
if(value.getType() == ValueType.STRING)
parent = (String) value.getValue();JsonObjects are HashMaps, so you can iterate over them like you would a HashMap. Their values are JSonValues.
JSonArrays are ArrayList with JSonValues, so you can iterate over them like you would an ArrayList.
EasyJSON is very flexible with new lines and white space and ignores them unless they are in a quote. Because of that multi-lined quotes are supported but everything until the end quote will be used. There are some rudimentary safeguards against an invalid JSON format as well.