And the Game Maker JSON parser WORKS!

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

I’ve  finished writing 90% of the JSON parser it can parse all basic stuff! Still no support for ‘true’, ‘false’ and ‘null’ as values but I’ll look into it. Other quick fixes include that when parsing a number in scientific notation, the ‘e’ must be lowercase but that’s an easy fix that I can do later. I still haven’t bothered to look at error handling.

Here’s a screenie of it in action.

As promised, I’ll release the source code. It’s licensed under the BSD Free Software License and the conditions of that are (in simple speak):

  • You must include the copyright notice when distributing (whether in source or binary form) for the JSON parser.
  • You aren’t allowed to use my name to promote the final product (but credit given is appreciated).

Yes, the source code is ugly but it works!

Here’s how to use it (instructions may change as I release different versions):

  1. Call json_object_init(); But only once! And you have to make sure that the instance created by it must not be destroyed (I’m working on fixing it)
  2. Call json_parse(string); and test whether the result is -1. If it is, the string is malformed or contains grammar that the parser currently does not understand.
  3. If it was successful, you get a instance ID. Use that instance ID to get the values.
  4. Call json_get_value(obj, [key, force_instance_id_return]); Where obj is the instance ID returned from the previous call. The key is only used when returning data from an object or array. If json_get_value is called and the result is a instance ID to a end node, instead of returning a instance ID, the actual value is returned. Set the last argument to true if you want a instance ID.
  5. Call json_destroy(obj); to destroy the object when you are done. Only ever call this function on the root object. AKA the one you got from json_get_value();.

The code creates instances so be sure that won’t interfere with your game. All instances are created with no sprite at (-1,-1) in the room.

Enjoy the basic JSON parser. Please do point out suggestions and bugs.

Edit: Major bug fixes. Fixed the ‘e’ problem and some problems where objects weren’t parsed correctly. Also added a json_dump function to easily see parsed data.

Download Source Code

Edit2: Bugger, another bug discovered as I posted that. For some reason, the json_dump function some times outputs the first key twice. To fix it, simply add curr_key to the list of var’s on the top of json_dump()

3 thoughts on “And the Game Maker JSON parser WORKS!

  1. Quite lovely. I was looking for this. Glad to see it’s been done! I’ll be using this to integrate weather data from WeatherUnderground into my application.

    Thanks for making my life easier. xb

    Like

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