Functions

JsmnInit()

This function is used to (re)initialize a parser object. It removes all leftover data from parser for each new JSON packet and sets all members to initial values. In most cases this function should be called before each new JSON packet. See Reentry for special case. Note: callback and pcache member on parser object is not modified. This means that the application will need to be initialize values.

Direction Name Type Description
Out parser jsmn_parser Pointer Pointer to jsmn_parser to be initialized
Return error INT Always returns 0 currently

JsmnParse()

Parses a JSON string into tokens and pass them to a user defined callback function. This is the backbone of Jsmn. Note: Parse function supports js object format as well as standard JSON.

Arguments

Direction Name Type Description
In parser jsmn_parser Pointer Pointer to the configured jsmn_parser to be used while parsing
In js STRING Pointer Pointer to JSON string to be parsed
In len UINT String length JSON
In tokens jsmntok_t Pointer Pointer to array of jsmntok_t for temporary storage for parsing and caching
In numTokens UINT Size of token array (number of elements)
Return tokensFound UDINT The number of parsed objects. If an error occurs a negative error number will be returned instead

Determining required numTokens

The numTokens needed to successfully parse a JSON string is directly dependent on number of levels expected in the JSON. 1 token is needed for every member or value following only the deepest route. For example JSON given below, JsmnParse will require at least 7 tokens:

{
    "object": {
        "subObject" : {
            "mem1": 1
        },
        "mem2": 2
    },
    "mem3": 3
}

Notice the deepest level in object is member “mem1”. So counting every member and value starting at root to “mem1” we get:

  • 1 token for root object
  • 1 token for “object” member
  • 1 token for “object”’s value
  • 1 token for “subObject” member
  • 1 token for “subObject”’s value
  • 1 token for “mem1” member
  • 1 token for “mem1”’s value

Note: We ignored mem2 and mem3 because they are not apart of the deepest route.

Tip

It is usually best to add a few extra tokens to support additional data in JSON packet without erroring.

Callback data

Callback Function

Application can define a callback function that will be called for every new token found while parsing. Callback function will given information regarding token via jsmn_callback_data type and have access to pUserData specified by the application. If no callback is desired set parser.callback.pFunction to 0. Example callback function definition below.

// userData - Value specified by application via parser.callback.pUserData
// data - Token data for callback
unsigned long jsonCallback(userData_typ* userData, jsmn_callback_data* data);

JsonParse()

Warning

This function is Deprecated. Use may cause PLC exceptions! May be revived in future library versions.

Json parse is a wrapper for jsmnParse function that simplifies the usage but looses the ability to use callbacks and reentrance.

Direction Name Type Description
In pJsonString STRING Pointer Pointer to jsmn_parser to be initialized
Out pTokenArray jsmntok_t Array Pointer Pointer to array of tokens. Array must be large enough to hold all tokens found in JSON string max JSMN_MAX_TOKENS
Return tokensFound UDINT The number of parsed objects. If an error occurs a negative error number will be returned instead

JsonTokenEqual()

Warning

This function is Deprecated. Use may cause PLC exceptions! May be revived in future library versions.

Finds index of token pName in pTokenArray parsed with JsonParse from pJsonString.

Direction Name Type Description
In pJsonString STRING Pointer Pointer to JSON string that tokens were parsed from
In pTokenArray jsmntok_t Array Pointer Pointer to array of tokens, parsed from JSON string
In pName STRING Pointer Pointer to string containing name of token to find
Return index INT Index of token in pTokenArray. Returns -1 if not found

JsonGetValue()

Warning

This function is Deprecated. Use may cause PLC exceptions! May be revived in future library versions.

Finds value of token pName in pTokenArray parsed with JsonParse from pJsonString.

Direction Name Type Description
In pJsonString STRING Pointer Pointer to JSON string that tokens were parsed from
In pTokenArray jsmntok_t Array Pointer Pointer to array of tokens, parsed from JSON string
In pName STRING Pointer Pointer to string containing name of token to find
In pStrValue STRING Pointer Pointer to string to be populated with token value
In StrLength UDINT Max pStrValue length
Return index INT Index of token in pTokenArray. Returns -1 if not found