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):
- 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)
- 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.
- If it was successful, you get a instance ID. Use that instance ID to get the values.
- 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.
- 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.
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()