Update: Writing a JSON parser

Edit: Information on this page is out-dated. Please go here to find out more about the new JSON game maker library.

I just finished writing the script that puts everything into the objects. Most of the stuff is working now! All the recursive functions are working as they should and there are no leaks as far as I can tell. There are still a few bugs that need ironing (graceful error handling is a huge on on this list) and I still need to finish implementing array support (it’s about 75% done).

Here’s some screenies to enjoy 😀

The JSON parser is almost done. Just about 70% completed now!

Okay, time for some news.

I redid the basic structure and data handling stuff of the parser. The initializing function first adds some object resources.

Then, each object contains pointers to other objects (except ones that actually contain the value).

Basically like the following:

A “object” object.

The object has a ‘Create’ event that initializes a ds_map and some variables.

The destroy event destroys every object that is being pointed to by the ds_map (and setting off a chain reaction so all the other ones deallocate all their resources and so on). So in the end, everything is being taken care of. That’s some pro resource management.

Each entry in the ds_map is a pointer to another object.

A “array” object.

Same as above except with a ds_list

A “nodevalue” object.

This is simple, it just holds a value. Other objects point to this one.

Overall, this system is way better than what I previously planned to do. They all point to each other and members can be easily accessed. That means, I can parse the whole thing into one object.

The only downside is it must create instances and could muck up instance counts and potentially could introduce problems into the game. Nevertheless, I’ll document it and it should be fine if the programmer is competent.

Now, on the parsing side, everything is going superbly awesomely, well. I’ve just hit the first milestone.

The parser can handle strings (and un-escapes everything except unicode representations) and numbers (even scientific notation). It has functions to skip white space and all. And at a bit over 1 microsecond to parse that little bit isn’t too bad – workable but slower than other, well developed parsers. Still, not bad considering it is my first parser with craptastic programming skills. Here’s a snippet:

I’ll eventually release all the source code. It’ll all be under the BSD License.

Other changes include that I am now using a stack to return data. It’s better because:

  • It’s less messy than working with string pointers (well, they aren’t exactly string pointers but yeah) and variable_local_set(); functions.
  • You can chain the same stack all the way down so there’s less resource management to do (the root function just calls a ds_list_destroy();)

So far, I’d say the parser is about 45% complete. At this point, the biggest challenge is handling errors gracefully.

And that is all folks.

2 thoughts on “Update: Writing a JSON parser

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s