Not logged in

Jsi implements a non-validating JSON parser. That is, will accept JSON that is not strictly compliant.



The main function for parsing JSON to generate a Jsi_Value is:

Jsi_RC Jsi_JSONParse(Jsi_Interp *interp, const char *js, Jsi_Value **ret, int flags); 

If the bit flags field does not contain JSI_JSON_STRICT, the parser will accept non-standard, unquoted property names, as in:

{file:"FF", line:NN}


This function is used primarily to reduce C-code.

Jsi_RC Jsi_JSONParseFmt(Jsi_Interp *interp, Jsi_Value **ret, const char *fmt, ...);

Consider a simple example where we wish to return a javascript object:

{file:"File", line:1}

In C we would write something like:

Jsi_Obj *nobj;
Jsi_ValueMakeObject(interp, ret, nobj = Jsi_ObjectNew(interp));
Jsi_ObjInsertFromValue( interp, nobj, Jsi_ValueNewStringKey(interp, "file"), 
    Jsi_ValueNewStringKey(interp, file));
Jsi_ObjInsertFromValue( interp, nobj, Jsi_ValueNewStringKey(interp, "line"),
   Jsi_ValueNewNumber(interp, line));

Alternatively, this can be simplified to:

return Jsi_JSONParse(interp, ret, "{file:\"%s\", line:%d}", file, line);

The more deeply nested the object, the more code is simplified, eg:

Jsi_JSONParseFmt(interp, ret, "{ a: [ {x:%d, y:%d}, {x:%d, y:[%d,%d,%d]}] }",a,b,c,d,e,f);

Due to the efficiency nature of low-level parsing, this adds very little extra overhead.

This approach can save a lot of code, particularly when writing commands that return nested objects and/or arrays. .

Note: permissive mode is the default. So there is no need to quote property names which in C rapidly becomes tedious:

"{\"os\":\"%s\", \"platform\":\"%s\", \"hasThreads\":%s, \"pointerSize\":%d, "

Note: the above works only for cases where data does not contain special JSON characters.


For the low level, parses JSON into an array of tokens in a highly stack efficient manner. It uses a single array of tokens for output so that for limited size JSON strings, no memory gets allocated during the parse. When memory does get allocated, it is only to resize the token array.


Any data sent between sub-interps will first be converted to/from JSON. This because all data objects are private to an interp.