How do I write a json file in a specific format?
I am almost done with my program, it loads perfectly, but one problem I am facing is when I edit the json file through my program, I cannot reload the json file, because when I save the file the formatting becomes different.
So my question is, how do I format a json file in the same way I read it?
Here's my method for writing a new json file.
public static boolean write(File npcDefFile) {
try {
FileWriter writer = new FileWriter(npcDefFile);
Throwable localThrowable3 = null;
try {
Gson gson = new Gson();
String json = gson.toJson(NPCDefinitions.getNPCDefinitions());
writer.write(json);
writer.flush();
} catch (Throwable localThrowable1) {
localThrowable3 = localThrowable1;
throw localThrowable1;
} finally {
if (writer != null) {
if (localThrowable3 != null) {
try {
writer.close();
} catch (Throwable localThrowable2) {
localThrowable3.addSuppressed(localThrowable2);
}
} else {
writer.close();
}
}
}
} catch (NullPointerException | IOException e) {
System.out.println(e.getLocalizedMessage());
return false;
}
return true;
}
Here's the correct format (3 spaces)
[
{
"id": 0,
"name": "Hans",
"examine": "Servant of the Duke of Lumbridge.",
"combat": 0,
"size": 1,
"attackable": false,
"aggressive": false,
"retreats": false,
"poisonous": false,
"respawn": 10,
"maxHit": 0,
"hitpoints": 0,
"attackSpeed": 7,
"attackAnim": 0,
"defenceAnim": 0,
"deathAnim": 0,
"attackBonus": 0,
"defenceMelee": 0,
"defenceRange": 0,
"defenceMage": 0
},
{
"id": 1,
"name": "Man",
"examine": "One of Wildy many citizens.",
"combat": 2,
"size": 1,
"attackable": true,
"aggressive": false,
"retreats": true,
"poisonous": false,
"respawn": 10,
"maxHit": 1,
"hitpoints": 7,
"attackSpeed": 7,
"attackAnim": 422,
"defenceAnim": 1834,
"deathAnim": 836,
"attackBonus": 9,
"defenceMelee": 9,
"defenceRange": 9,
"defenceMage": 0
}
]
Here's the format after saving the file to a new json file. (Compact)
[{"id":0,"name":"Hans","examine":"Servant of the Duke of Lumbridge.","combat":0,"size":1,"attackable":false,"aggressive":false,"retreats":false,"poisonous":false,"respawn":10,"maxhit":0,"hp":0,"attackspeed":7,"attackanim":0,"defenceanim":0,"deathanim":0,"attackbonus":0,"defencemelee":0,"defencerange":0,"defencemage":0},{"id":1,"name":"Man","examine":"One of Wildy\u0027s many citizens.","combat":2,"size":1,"attackable":true,"aggressive":false,"retreats":true,"poisonous":false,"respawn":10,"maxhit":1,"hp":7,"attackspeed":7,"attackanim":422,"defenceanim":1834,"deathanim":836,"attackbonus":9,"defencemelee":9,"defencerange":9,"defencemage":0}]
This is how I load the json file.
/**
* A dynamic method that allows the user to read and modify the parsed data.
*
* @param reader
* the reader for retrieving the parsed data.
* @param builder
* the builder for retrieving the parsed data.
*/
public abstract void load(JsonObject reader, Gson builder);
/**
* Loads the parsed data. How the data is loaded is defined by
* {@link JsonLoader#load(JsonObject, Gson)}.
*
* @return the loader instance, for chaining.
*/
public final JsonLoader load() {
try (FileReader in = new FileReader(Paths.get(path).toFile())) {
JsonParser parser = new JsonParser();
JsonArray array = (JsonArray) parser.parse(in);
Gson builder = new GsonBuilder().create();
for (int i = 0; i < array.size(); i++) {
JsonObject reader = (JsonObject) array.get(i);
load(reader, builder);
}
} catch (Exception e) {
e.printStackTrace();
}
return this;
}
Any help would be appreciated.
EDIT:
Here's the exception that was caught.
java.lang.NullPointerException
at NpcDefinitionLoader.load(NpcDefinitionLoader.java:28)
at JsonLoader.load(JsonLoader.java:56)
at NPCDefinitions.loadNPCDefinitions(NPCDefinitions.java:76)
at DefinitionEditor.loadFile(DefinitionEditor.java:171)
at DefinitionEditor.loadButtonActionPerformed(DefinitionEditor.java:790)
at DefinitionEditor.actionPerformed(DefinitionEditor.java:827)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased
Here is the first line an error occurred
int maxHit = reader.get("maxHit").getAsInt();
Second line error
load(reader, builder);
source to share
JSON object keys are stored in lowercase. JSON is case sensitive.
In a compact file, it has maxhit instead of maxHit . Check NpcDefinitions to see how this field / property is declared.
Pretty JSON printing with Gson can be done using Gson.toJson (JsonElement, JsonWriter). There's a good example of indentation in the docs: https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonWriter.html
source to share