JSON Data Storage - nt314p/Zork GitHub Wiki

In our Zork game, the game state can be set using external (non java) files. On runtime, the program reads data from the files and loads that data to initialize the game. For example, maps, character dialogue, and the player's inventory can be stored within these external files. When choosing which file type to use (text, JSON, XML, etc...), we considered some attributes that we would like our file type to have.

Easy human read and write

We are always trying to find the easiest way to do something, and inputting data for our Zork game is no exception. We want to make it as easy as possible for humans to input information into the file. The computer/program can do the more difficult work.

Simple file parsing

But wait. We're the ones writing the program. If we make our file easy to understand as humans, then we make it harder for the computer to understand, and thus parse the file. If we are going to be building the program that parses the file, we want to make the file easy to parse so our job on the programming side won't be hard either. We need to strike a balance.

JSON - Built for lazy humans

We chose JSON as our file type to store the game data. But what is JSON? Well, JSON stands for JavaScript Object Notation. Data is stored within JSON using predefined syntax. It is both easy for humans to read and write JSON, and additionally, there exists a PREMADE library for parsing JSON files in Java. How convenient! This means that JSON is the ultimate work-reducing file type for us! Take a look at a sample of JSON code below:

{
	"type": "weapon",
	"name": "Knowledge",
	"weight": "0",
	"damage": "9000",
	"descriptions": [
		"look:The knowledge feels warm and powerful in your hand"
	]
}

Although you may not know the exact syntax or formatting of JSON, you can easily look at this sample file and get its meaning.

Compare it to this sample text file meant to be interpreted by Java using a custom parser:

Knowledge|t:weapon|w:0|d:9000|dp:(look;The knowledge feels warm and powerful in your hand)|

Not only is this hard to read and understand, but can you imagine the programming nightmare of attempting to parse this file? Even though JSON may be harder to parse, because it is a common form of data storage, it has prebuilt libraries meaning we don't have to do any parsing work. However, for this custom formatted file, a custom parser needs to be created from scratch.