jsonparse
jsonparse |
Purpose
Parse a JSON document.
Synopsis
function jsonparse(afct:array(range) of string,ctx:ctxtype): integer
Arguments
afct
|
Event function table. Each entry of this array is the name of the function to call when the corresponding event occurs. The expected events are (all of these entries are optional):
|
||||||||||||||||
ctx
|
Value passed as firt argument of all event functions
|
Return value
0 if successful,
1 in case of parsing error or a non-zero value returned by an event function
Example
This example displays values of object members
"name" and
"age" of a JSON document:
declarations afct:array(range) of string s_ctx=record cnt:integer end-record c:s_ctx end-declarations public function setvalue_all(ctx:s_ctx,name:text,type:integer,val:text):integer if name="name" or name="age" then writeln(name,":",val) ctx.cnt+=1 end-if end-function afct(JSON_FCT_TEXT):="setvalue_all" ! A value as a text fopen("mydoc.json",F_INPUT) rts:=jsonparse(afct,c) fclose(F_INPUT) writeln("line count:",c.cnt)
Further information
1. This function is an alternative approach to
jsonload for processing JSON documents: instead of loading into memory the entire document this function calls a dedicated routine whenever it identifies a JSON entity. For instance a specific function is called when an object is open and another one when it is closed. It is up to the Mosel program to decide how to handle the document via these
event handling functions.
2. To each event type corresponds a specific function signature. These functions return an integer that decides whether parsing should continue: a non-zero value will cause the parsing to cancel (this value is used as the return value of
jsonparse). The expected function signatures are:
The name argument is not empty only when the value corresponds to an object member: in this case this parameter is the label of this member. The type argument passed to the text_val function indicates the type of the data (0 for null, 1 for text, 2 for numerical and 3 for Boolean): this function is used with the textual representation of the value when the required type-specific function is not available. For instance this function will be called with type=3 if a Boolean value has been read and the entry JSON_FCT_BOOL is not defined in the function table.
- JSON_FCT_OPEN_OBJ
- function open_object(ctx:ctxtype, name:text):integer
- JSON_FCT_CLOSE_OBJ
- function close_object(ctx:ctxtype):integer
- JSON_FCT_OPEN_ARR
- function open_array(ctx:ctxtype, name:text):integer
- JSON_FCT_CLOSE_ARR
- function close_array(ctx:ctxtype):integer
- JSON_FCT_TEXT
- function text_val(ctx:ctxtype, name:text, type:integer, val:text):integer
- JSON_FCT_NUM
- function num_val(ctx:ctxtype, name:text, name:text, val:real):integer
- JSON_FCT_BOOL
- function bool_val(ctx:ctxtype, name:text, name:text, val:boolean):integer
- JSON_FCT_NULL
- function null_val(ctx:ctxtype, name:text):integer
The name argument is not empty only when the value corresponds to an object member: in this case this parameter is the label of this member. The type argument passed to the text_val function indicates the type of the data (0 for null, 1 for text, 2 for numerical and 3 for Boolean): this function is used with the textual representation of the value when the required type-specific function is not available. For instance this function will be called with type=3 if a Boolean value has been read and the entry JSON_FCT_BOOL is not defined in the function table.
3. An error message indicating the location of the error is displayed when the parsing fails or if an event function returns a negative value (a positive value also interrupts parsing but no message is displayed).
Related topics
Module