Jibble

Author Topic: MODULE: jsonparser 0.1.0  (Read 231 times)

eri0o

MODULE: jsonparser 0.1.0
« on: 28 Feb 2021, 20:41 »
jsonparser version 0.1.0


Get Latest Release jsonparser.scm | GitHub Repo | Demo Windows | Demo Linux | Download project .zip

I recently found a cool unrelated tool that I wanted to add to my workflow, but the tool output was a json file. There's no good object to represent a JSON object in AGS and I was too lazy to make a plugin.
So I made a parser! This is a JSON minimal parser for Adventure Game Studio. It's based on JSMN, which is a C JSON parser that had an easy to read code.

It's not well tested, but I thought to "formally" release it here in case someone had a need that it would be enough for it. I probably need to work on a better demo and write better docs for it too. But hey, first release!

Usage
Spoiler: ShowHide

If you wish to handle things more manually, you can use a thiner parser that is also faster:

Code: Adventure Game Studio
  1. String json_string = "{ \"name\":\"John\", \"age\":30, \"car\":null }";
  2. JsonParser* parser = new JsonParser;
  3.  
  4. int token_count = 8;
  5. JsonToken* t[] = JsonToken.NewArray(token_count);
  6.  
  7. int r = parser.Parse(json_string, t, token_count);
  8.  
  9. // now that you have the Tokens, you can use them to parse as you wish!
  10. if (r < 0) Display("Failed to parse JSON: %d\n", r);
  11. if (r < 1 || t[0].type != eJSON_Tok_OBJECT) Display("Object expected\n");
  12.  
  13. for(int i=0; i<r  ; i++){
  14.   JsonToken* tok = t[i];
  15.   Display(String.Format("%d ; %s ; %d ; %s ; %d ; %d ; %d",
  16.     i, tok.ToString(json_string), tok.size , tok.TypeAsString,  tok.start ,  tok.end ,  tok.parent ));
  17. }
  18.  
  19. Display("JSON Parsing has FINISHED for string\n\n%s", json_string);
This module also packs a more approacheable (but less tested) parser:

Code: Adventure Game Studio
  1. String json_string = ""; json_string = json_string.Append("{\"squadName\":\"Super squad\",\"formed\":2016,\"active\":true,\"members\":[");
  2. json_string = json_string.Append("{\"name\":\"Molecule Man\",\"age\":29,\"secretIdentity\":\"Dan Jukes\",\"powers\":[\"Radiation resistance\",\"Radiation blast\"]},");
  3. json_string = json_string.Append("{\"name\":\"Madam Uppercut\",\"age\":39,\"secretIdentity\":\"Jane Wilson\",\"powers\":[\"Million punch\",\"Super reflexes\"]},");
  4. json_string = json_string.Append("{\"name\":\"Eternal Flame\",\"age\":100,\"secretIdentity\":\"Unknown\",\"powers\":[\"Immortality\",\"Heat Immunity\",\"Interdimensional jump\"]}]}");
  5.  
  6. MiniJsonParser jp;
  7. jp.Init(json_string); // parse json_string and internally generate the tokens
  8.  
  9. while(jp.NextToken()) // advance the current token and exit when there are no tokens left
  10. {    
  11.   if(jp.CurrentTokenIsLeaf)  // usually the interesting information is on the leafs
  12.   {
  13.     Display(String.Format("%s: %s", jp.CurrentFullKey, jp.CurrentTokenAsString));
  14.   }    
  15. }
  16.  
  17. Display("JSON Parsing has FINISHED for string\n\n%s", json_string);

Script API

Spoiler: ShowHide

JsonParser
JsonParser.Parse
Code: Adventure Game Studio
  1. int JsonParser.Parse(String json_string, JsonToken *tokens[], int num_tokens)
Parses a JSON data string into and array of tokens, each describing a single JSON object. Negative return is a JsonError, otherwise it's the number of used tokens.

You need to preallocate a number of tokens to be used by this method before calling it. Use JsonToken.NewArray(count) to help you.

JsonParser.Reset
Code: Adventure Game Studio
  1. void JsonParser.Reset()
Marks the parser for reset, useful if you want to use it again with a different file. Reset only actually happens when Parse is called.

JsonParser.pos
Code: Adventure Game Studio
  1. int JsonParser.pos
offset in the JSON string

JsonParser.toknext
Code: Adventure Game Studio
  1. int JsonParser.toknext
next token to allocate

JsonParser.toksuper
Code: Adventure Game Studio
  1. int JsonParser.toksuper
superior token node, e.g. parent object or array

JsonToken
JsonToken.NewArray
Code: Adventure Game Studio
  1. static JsonToken* [] JsonToken.NewArray(int count)
Static helper to ease Token Array creation. Ex: JsonToken* t[] = JsonToken.NewArray(token_count);

JsonToken.ToString
Code: Adventure Game Studio
  1. String JsonToken.ToString(String json_string)
pass the json_string that was parsed and generated this token to recover the string this token refers to

JsonToken.type
Code: Adventure Game Studio
  1. JsonTokenType JsonToken.type
The type of the token: object, array, string etc.

JsonToken.start
Code: Adventure Game Studio
  1. int JsonToken.start
The start position in JSON data string.

JsonToken.end
Code: Adventure Game Studio
  1. int JsonToken.end
The end position in JSON data string.

JsonToken.size
Code: Adventure Game Studio
  1. int JsonToken.size
The size tells about the direct children of the token, 0 if it's a leaf value, 1 or bigger if it's a key or object/array.

JsonToken.parent
Code: Adventure Game Studio
  1. int JsonToken.parent
If it's a child, is the index position of the parent in the token array.

JsonToken.TypeAsString
Code: Adventure Game Studio
  1. readonly attribute String JsonToken.TypeAsString
Utility function for debugging, returns the type of the token in a String format.

JsonTokenType
- eJSON_Tok_UNDEFINED, a valid token should never have this type.
- eJSON_Tok_OBJECT, an object, it holds keys and values, values can be any other type.
- eJSON_Tok_ARRAY, an array, the token will contain direct ordered children.
- eJSON_Tok_STRING, the token is a string, could be a key, could be a value, context is needed.
- eJSON_Tok_PRIMITIVE, the token is either a number (float or integer), a boolean (true or false) or null.

JsonError
Used to check parse results.
- eJSON_Error_InsuficientTokens, Not enough tokens were provided. Please use more tokens.
- eJSON_Error_InvalidCharacter, Invalid character inside JSON string.
- eJSON_Error_Partial, The string is not a full JSON packet, more bytes expected.

MiniJsonParser
MiniJsonParser.Init
Code: Adventure Game Studio
  1. void MiniJsonParser.Init(String json_string)
Initialize the parser passing a JSON as a string. Common usage is: MiniJsonParser jp; jp.Init(json_string);.

MiniJsonParser.NextToken
Code: Adventure Game Studio
  1. bool MiniJsonParser.NextToken()
Advances to the next token. Returns false if no tokens left.

MiniJsonParser.CurrentTokenAsString[/code]
Code: Adventure Game Studio
  1. readonly attribute String MiniJsonParser.CurrentTokenAsString
The current token content, as a String.

MiniJsonParser.CurrentTokenType
Code: Adventure Game Studio
  1. readonly attribute JsonTokenType MiniJsonParser.CurrentTokenType
The current token type.

MiniJsonParser.CurrentTokenSize
Code: Adventure Game Studio
  1. readonly attribute int MiniJsonParser.CurrentTokenSize
The current token size, 0 if it's a leaf value, 1 or bigger if it's a key or object/array.

MiniJsonParser.CurrentState
Code: Adventure Game Studio
  1. readonly attribute MiniJsonParserState MiniJsonParser.CurrentState
The current state of our mini parser. Helps understanding the JSON tokens we got when parsing.

MiniJsonParser.CurrentFullKey
Code: Adventure Game Studio
  1. readonly attribute String MiniJsonParser.CurrentFullKey
Gets the current dot separated key.

MiniJsonParser.CurrentTokenIsLeaf
Code: Adventure Game Studio
  1. readonly attribute bool MiniJsonParser.CurrentTokenIsLeaf
Checks if the state and key type currently are a leaf. True if it's, usually leafs are the interesting tokens we want when parsing.

MiniJsonParserState
- eJP_State_START, The parser just started.
- eJP_State_KEY, The current token is key in an object.
- eJP_State_VALUE, The current token is a value in an object.
- eJP_State_ARRVALUE, The current token is a value in an array.
- eJP_State_STOP, Don't parse anything in this state, but the parser is not necessarily done.


License

This code is made by eri0o and is licensed with MIT LICENSE. The code on this module is based on Serge's JSMN, which is also MIT licensed and is referenced in the license.
« Last Edit: 01 Mar 2021, 00:06 by eri0o »

Joseph DiPerla

  • Joseph DiPerla, Adventure Game Creator Wannabe!
    • I can help with backgrounds
    • I can help with characters
    • I can help with play testing
    • I can help with story design
    • I can help with translating
    • I can help with voice acting
    • I can help with web design
Re: MODULE: jsonparser 0.1.0
« Reply #1 on: 01 Mar 2021, 00:49 »
That's pretty cool! Thanks.
Joseph DiPerla--- http://www.adventurestockpile.com
Play my Star Wars MMORPG: http://sw-bfs.com
See my Fiverr page for translation and other services: https://www.fiverr.com/josephdiperla
Google Plus Adventure Community: https://plus.google.com/communities/116504865864458899575