JSON_PAR (RPGLE)

JSON Parser
Details ....

Copybooks  

'json_c.rpgle'
'unicode_c.rpgle'
'json_h.rpgle'

Procedures  

abortParsing
Abort parsing
back
Move parser back
json_parseexported
Parse JSON string
more
Checks for more characters in string
next
Next character
nextArray
Next JSON array
nextCleanChar
Next clean character
nextObject
Next JSON object
nextString
Next string
nextValue
Next value

Detailed Description  

A utility module for parsing JSON strings.

The parser string should be a valid JSON string or else and escape message will be sent to the caller.

The parser also accepts the equal sign (=) as a separator for the key and the value. The parser also supports the ; instead of the , for separating pairs of values or values in an array. Strings may be enclosed in single or double quotes. Arrays can either be enclosed in curly braces () or in brackets []. Empty objects and arrays are supported. Unicode representation of a character is supported like u00CF.
Author:
Mihael Schmidt
Date:
2009-03-07
Infos:
  The JSON syntax is not fully supported at the moment.
Changes:
09.04.2011   —   Mihael Schmidt
Fixed parser error when newline characters follow a numeric value.

23.07.2011   —   Matthias Aum
Arrays only support (JSON array without JSON object)

Links:
http://www.json.org

Procedure Documentation  

abortParsing  

voidabortParsing(Pointer, Zeichen)
The memory of the parser header is deallocated (freed) and an escape message is sent to the previous call stack entry with the passed message text.
Parameter:
PointerPointer to parser header
Zeichen (1000)   constMessage text

back  

voidback(Pointer)
Moves the position of the parser back by one position.
Parameter:
PointerPointer to parser header structure

json_parse  

Pointerjson_parse(Pointer, Pointer)
Creates a JSON object or array from the passed string. If the passed string is not a valid JSON string an escape message will be sent. The string must be null terminated.
Parameter:
Pointer   const   omissiblePointer to JSON string
Pointer   const   optionalPointer to parser header (only for internal use)
Return value:
PointerPointer to JSON object or array
Exported.

more  

voidmore(Pointer)
Checks if there are still more characters to parse in the given string.
Parameter:
PointerPointer to the rest of the JSON string to be parsed

next  

Zeichennext(Pointer)
Returns the next character in the JSON string and moves the parser forward by one position.
Parameter:
PointerPointer to parser header structure
Return value:
Zeichen (1)next character or null if there are no more characters

nextArray  

PointernextArray(Pointer)
Returns the JSON array which is at the next position in the JSON string. This procedure expects the position of the parser to be on the opening bracket of the array ( [ ). If there is no JSON array at the current position an escape message will be sent.

Empty arrays are also supported.
Parameter:
PointerPointer to the parser header structure
Return value:
PointerPointer to JSON array

nextCleanChar  

ZeichennextCleanChar(Pointer)
Moves the parser to the position on the next clean character and returns it to the caller. Character like carriage return, linefeed and blanks are ignored. On a null character an escape message will be sent.
Parameter:
PointerPointer to the parser header structure
Return value:
Zeichen (1)next clean character

nextObject  

PointernextObject(Pointer)
Returns the JSON object which is at the next position in the JSON string. This procedure expects the position of the parser to be on the opening bracket of the array ( { ).

Empty objects are supported.
Parameter:
PointerPointer to the parser header structure
Return value:
PointerPointer to JSON object

nextString  

ZeichennextString(Pointer, Zeichen)
Returns the next string from the current parser position on and moves the parser to the position behind the returned string.
Parameter:
PointerPointer to the parser header structure
Zeichen (1)   constOpen quote character of the string (' or ")
Return value:
Zeichen (10000)next JSON string

nextValue  

voidnextValue(Pointer, Pointer, Integer)
Returns the next value. The parser is moved after the next value. The returned value can be any supported JSON data type including array and object.
Parameter:
Pointer   constPointer to the return value data structure
PointerPointer to the JSON string
Numerisch (Integer) (10)JSON data type of the return value