Resources | Resources |



Using JSON interfaces

JSONGen and JSONParser are C libraries supporting a set of functions to manipulate JSON data. When used, the module must link with the appropriate library.


JSONGen is used to construct a new JSON data string by appending to the current structure in a serial manner. API functions work in the context of the JSONGen data structure which holds the JSON data buffer being constructed. For quick usage, this structure can be declared on the stack.

Example: Constructing an array onto provided buffer

JSONGen gen={0};
char szBuf[256];
JSONGen_Ctor(&gen, szBuf, 256, 0,0);
JSONGen_PutJSON(&gen, "123e-2, 3456, -3, false", std_strlen("123e-2, 3456, -3, false"));
JSONGen_GetJSON(&gen, &psz, &nSize);
//String psz matches "[123e-2, 3456, -3, false]" 
and psz is same as 

Example: Constructing an object with dynamically allocated buffer

//A simple way to implement ReallocFunc
static int JSONGen_ReallocFunc(void *pv, int nSize, void **ppv)
   return IEnv_ErrRealloc((IEnv *)pv, nSize, ppv);

JSONGen gen={0};
JSONGen_Ctor(&gen, 0,0, JSONGen_ReallocFunc, 
(void *)me->piEnv);
JSONGen_PutKey(&gen, "value", std_strlen("value"));
JSONGen_PutInt(&gen, 123456);
JSONGen_GetJSON(&gen, &psz, &nSize);
//String psz matches "{\"value\": 123456}" 
and valid until Dtor() call


JSONParser library is used to parse and query given JSON data for its structure and values. Similar to JSONGen, this API works in the context of JSONParser data structure and when declared on the stack requires no memory allocation.

Example: Lookup an object key and read its value

//szBuf[] = "{ \"key1\" : \"Hello\" }"
JSONParser js = {0};
JSONParser_Ctor(&js, szBuf, 0);
JSONParser_ObjectLookup(&js, 0, "key1", 0, &keyID, &valID);
JSONParser_GetString(&js, valID, psz, nSize, &nSize);
//String psz matches "Hello"

Example: Generic lookup with a dot-separated lookup string

//szBuf[] = "{ \"Outer\" : [234, [{ \"Lower\" : \"Value\" }]] }"
JSONParser js = {0};
JSONParser_Ctor(&js, szBuf, 0);
JSONParser_Lookup(&js, 0, "Outer.1.0.Lower", 0, &keyID, &valID);
JSONParser_GetString(&js, valID, psz, nSize, &nSize);
//String psz matches "Value"


IJSONTree is an interface that can be used to build and serialize JSON data or to query an existing one. Unlike JSONParser and JSONGen APIs, this object creates an internal in-memory representation of JSON data as a tree, which is then manipulated to build, query or serialize the JSON data.

Example: Lookup JSON data

// cpszJSON -> "{\"a\": 234, \"b\": 2111, 
\"c\": \"d\", \"e\": [1, 2]}"
IEnv_CreateInstance(piEnv, AEECLSID_JSONTree, 
(void **)&pijt));
IJSONTree_ParseJSON(pijt, cpszJSON, std_strlen(cpszJSON), 
IJSONTree_PathGet(pijt, pjvRoot, "e.1", &pjv));
IJSONTree_GetInt(pijt, pjv, &n));
//n == 2

Example: Creation by path set

IEnv_CreateInstance(piEnv, AEECLSID_JSONTree, (void **)&pijt));
IJSONTree_ParseJSON(pijt, "{}", std_strlen("{}"), &pjvRoot));
IJSONTree_CreateInt(pijt, 1234, &pjv);
IJSONTree_PathSet(pijt, pjvRoot, "abc", pjv);
IJSONTree_Serialize(pijt, pjvRoot, psz, 512, 0);
//psz matches "{\"abc\": 1234 }"