XML & JSON

I think I broke my laptop (no sound, mouse doesn’t work and keyboard has difficulties understanding finnish) but it isn’t slowing my game development yet. Anyway I had to do some research a while ago about serialization and deserialization (i.e. how to save my game information and how to load for example character or item objects from files). In small casual games there probably isn’t need for larger serialization but in strategy games I don’t think it wouldn’t be smart to pass a huge Java object array or list around between activities.

XML

First option that came into my mind was XML. I’m not much of an XML expert but do know it is a markup language and I had heard that it isn’t exactly a winner in the database or serialization circles. I had some programming experience of it though. Of course through the Android layouts but I had also made parser that made OpenGL vertices from SVG paths. I also knew that Android has also XML parsers in the Android API (http://developer.android.com/training/basics/network-ops/xml.html).

So for example short array for characters in my game would look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<GameCharacterArray>
<GameCharacter>
	<name>Markku Markkanen</name>
	<characterId>1</characterId>
	<accuaracy>4</accuaracy>
	<speed>5</speed>
	<strength>9</strength>
	<perception>5</perception>
	<intelligence>2</intelligence>
	<firearmSkill>33<firearmSkill>
	<characterType>POLICE</characterType>
</GameCharacter>
<GameCharacter>
<name>Sam Diego</name>
<characterId>2</characterId>
	<accuaracy>7</accuaracy>
	<speed>4</speed>
	<strength>6</strength>
	<perception>7</perception>
	<intelligence>8</intelligence>
	<firearmSkill>47<firearmSkill>
	<characterType>POLICE</characterType>
</GameCharacter>
</GameCharacterArray>

Size of this file would be about 600 bytes (indentations affect on the size of the file a little bit). XML is easy for human to read but start tags and end tags makes this file difficult to edit by hand in text editor without any autocomplete tricks. It probably would be easier to make GameCharacter into an empty element and current elements inside GameCharacter into attributes. Which would look something like this:

<GameCharacter
	name = "Markku Markkanen"
	characterId= "1"
	accuaracy="4"
	speed="5"
	strength="9"
	perception="5"
	intelligence="2"
	firearmSkill="33"
	characterType="POLICE"
/>

So this is a little bit more easier to read and it takes less space: about 400 bytes.

JSON

JSON (JavaScript Object Notation) is an alternative for XML in serialization. First I just passed JSON because I haven’t used much JavaScript and I thought JSON is something made specially for JavaScript and if I use it in Android with Java  my laptop will explode and I get laptop shrapnels in my hands and it will hurt my hands. I got over my JSON fobia after I looked closer to the syntax of JSON and found out that JSONObject and JSONArray is included in Android API (http://developer.android.com/reference/org/json/package-summary.html).

My stumpy GameCharacterArray would look like this in JSON:

[
{
"characterId":1,
"name":"Markku Markkanen",
"accuaracy":4,
"speed":5,
"strength":9,
"perception":5,
"intelligence":2,
"firearmSkill":33,
"characterType":"POLICE"
},
{
"characterId":2,
"name":"Sam Diego",
"accuaracy":7,
"speed":4,
"strength":6,
"perception":7,
"intelligence":8,
"firearmSkill":47,
"characterType":"POLICE"
}
]

JSON is pretty easy to read. The square brackets define a JSON object array and curly brackets define JSON object. Handling JSON files is pretty easy with the help of JSONTokenizer, JSONObjects and JSONArray. Size of this file would be 331 bytes.

Comparison

Both XML and JSON are easy to read for humans. XML is ubiquitous but JSON is more specialized. I found conference article by Audie Sumaray and S. Kama Makki where XML and JSON are compared in data serialization with binary serialization formats: Apache Thrift and Google’s Protocol Buffer (Such a boring name). Well the conclusion in Sumaray’s and Makki’s article was that JSON is exceptionally better in data serialization than XML, both in speed and size. Protocol Buffer and Apache Thrift were faster and more compact but they are not human readable so they do not work for me. So my choice in the end was JSON and I haven’t regretted it yet. Of course there would have YAML which is also pretty popular data serialization format but I was too lazy to look into it.

References

Audie Sumaray and S. Kami Makki. 2012. A comparison of data serialization formats for optimal efficiency on a mobile platform. In Proceedings of the 6th International Conference on Ubiquitous Information Management and Communication (ICUIMC ’12). ACM, New York, NY, USA, , Article 48 , 6 pages. DOI=10.1145/2184751.2184810 http://doi.acm.org/10.1145/2184751.2184810

.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s