The JSON C low-level encode functions handle the JSON encoding of simple XML schema data types. Calls to these functions are assembled in the C source code generated by the XBinder compiler to accomplish the encoding of complex structures. These functions are also directly callable from within a user's application program if the need to accomplish a low level encoding function exists.
The procedure to call a low-level encode function is the same as the procedure to call a compiler generated encode function described earlier. It is as follows:
The rtxInitContext function must first be called to initialize a context block structure.
Either a stream must be set up or a memory buffer specified to receive the encoded message. To set up a stream, one of the rtxStream functions must be called. To set up a memory buffer, the rtxInitContextBuffer function is used.
Encode functions are invoked to encode the JSON data types.
If a stream was used, the encoded message will have been written to the output stream. If a memory buffer was used, the result of the encoding will start at the beginning of the buffer. The encoded stream is a standard UTF-8 null-terminated text string.
For example, the following code fragment could be used to encode a document with a single, boolean value.
OSBOOL data; OSCTXT ctxt; int i, stat; const char* filename = "message.json"; /* Init context */ stat = rtxInitContext (&ctxt); if (0 != stat) { printf ("Context initialization failed.\n"); rtxErrPrint (&ctxt); return stat; } /* Populate structure of generated type */ data = TRUE; stat = rtxStreamFileCreateWriter (&ctxt, filename); if (0 == stat) stat = rtJsonEncBoolValue (&ctxt, data); rtxStreamClose (&ctxt); if (0 == stat) { printf ("encoded message:\n"); rtxPrintFile (filename); printf ("\n"); } else { printf ("Encoding failed\n"); rtxErrPrint (&ctxt); } }
A complete reference to all of the built-in C JSON encode functions is available in the XBinder C/C++ Runtime Reference Manual.