Procedure for Calling C Encode Functions

This section describes the step-by-step procedure for calling a C JSON encode function.

Before any encode function can be called; the user must first initialize an encoding context. This is a variable of type OSCTXT. This variable holds all of the working data used during the encoding of a message. The context variable is declared as a normal automatic variable within the top-level calling function. It must be initialized before use. This can be accomplished by using the rtInitContext function as follows:

   OSCTXT ctxt;

   if (rtInitContext (&ctxt) != 0) {
      /* initialization failed, could be a license problem */
      printf ("context initialization failed (check license)\n");
      return -1;
   }

After initializing the context and populating a variable of the structure to be encoded, an encode function can be called to encode the message.

A complete example may be found in the employee sample program, here edited for brevity:

   stat = rtInitContext (&ctxt);
   
   stat = rtxStreamFileCreateWriter (&ctxt, filename);

   if (stat != 0) {
      printf ("Unable to create file stream.\n");
      rtxErrPrint(&ctxt);
      return stat;
   }

   rtxSetDiag (&ctxt, verbose);

   asn1Init_PersonnelRecord (&employee);   
   
   /* populate employee structure */

   stat = asn1JsonEnc_PersonnelRecord (&ctxt, &employee);