Game Development Reference
In-Depth Information
JSON
Rather than having your application log all of its progress directly to an HTML file, you can instead have your
application write a lightweight HTML log file during startup. Then you have each system log its activities to a separate
JSON 4 file. JSON (JavaScript Object Notation) is a means of defining a JavaScript object with a set of keys and values.
This simple file format allows for easy interchange of data between applications and web services. 5
In your HTML file you use another piece of JavaScript to parse your JSON log files and appropriately add their
data to the HTML page. You update your JavaScript log files using a similar method to how you originally updated
your HTML log file. When you create a new object or array, you push the appropriate closing symbol onto the stack. To
update the JavaScript file for a system, you must first seek to the end of the file, minus the string length of your closing
symbol stack. You can then write the update, followed by writing out all the terminating symbols within your stack.
There are several advantages to logging applications' activity to JSON files. You can easily log processes that
may be running concurrently (as they now have their own JSON file). Also, as your log data is separate from your log
presentation, you can use the same JSON data within other logs. For example, you may wish to store the files as part of
an archive so that you can compare log data from several sessions.
The other advantage of exporting your log data to JSON objects is that you can take advantage of numerous third-
party libraries to manipulate JSON data in other languages4. This allows you to take log data and feed it back into the
game or application. For example, if you have a JSON file that has logged un-freed memory allocations, then you can
have the application break when that un-freed memory is being allocated. This allows the log data to feed back into
the application's debugging.
As you have written JavaScript data, as opposed to raw HTML tags, you can perform some operations on the
data before you convert it into HTML. For example, Listing 9-3 and Listing 9-4 show a brief demonstration sorting a
memory log JSON file into a report. The program sorts the data in order of memory allocation size, so that the largest
memory allocation is shown at the top of the table. It also contains a text box to allow the user to optionally enter a
size threshold. Any memory allocations below this threshold will become hidden in the table. See Figure 9-2 for a
visualization of this example.
Listing 9-3. Report.html Organizing Logged JavaScript Data
<HTML>
<HEAD>
<style type="text/css">
tr.odd { background-color:#CCCCCC }
tr.even { background-color:#EEEEEE }
tr.off { display:none; }
#report_table{background-color:#FFFFFF; padding: 2px;
border-color:#333333; border-style:solid;
border-width:2px; margin-left: auto;
margin-right: auto }
</style>
</HEAD>
<script type="text/javascript">
// Function called to initially populate the table.
function generate_table()
4 “Introducing JSON.” www.json.org/ .
5 “JSON: The Fat-Free Alternative to XML.” www.json.org/xml.html .
 
Search WWH ::




Custom Search