/**
 @class Valence_RPG_Toolkit.vvJson
 The vvJson procedures provide a way for the RPG program to format your data into
 simple and complex JSON strings to pass back to the browser.  These are  "wrapper" functions that utilize the open source <a href="http://rpgnextgen.com/index.php?content=json">JSON utility from RPG Next Gen</a>.
 */

/**
 @method _create
 Create a JSON object.

 @return {pointer}
 A pointer to the created JSON object is passed.

 # RPG Example

     d vJsonRoot     s               *
     /free
     // Create the JSON object and receive the pointer.
     vJsonRoot = vvjson_create();
     /end-free

 */

/**
 @method _getArray
 Returns the Array value of the entry.

 @param {pointer, const (required)} pointer
 The pointer address of the array.

 @param {32766a, const varying (required)} string
 The name of the of the array.

 @return {pointer}
 The pointer address for the searched string is returned.

 # RPG Example

 data = [{
           "CUSNO" : 1006,
           "orders" : [
           { "ORDERNO" : 5047,
             "CNAME"   : "American",
             "ORDSTS"  : "C"
           }, {
             "ORDNO"   : 5171,
             "CNAME"   : "American",
             "ORDSTS   : "C"
           }]
         ]}


     d String         s         65535a
     d OrderInfo      s               *
     d base           s               *
     d order          s               *
     d orders         s               *
     d vCustomerId    s             10i 0
     d vOrderId       s             10i 0
     d vStatus        s              1a
     d cusArrsz       s             10i 0
     d ordArrsz       s             10i 0
     d i              s             10i 0
     d x              s             10i 0
      /free

      // Pull in the string of JSON formatted data
      String = vvIn_Char('data');
      // Set a pointer and parse the string
      base   = vvjson_parse(%addr(string));
      // Get the size for the loop
      cusArrsz = vvjsona_size(base);
      for i = 0 to cusArrsz -1;
        // Get the object at our sequence number
        orderInfo = vvjsona_getObject(base : i);
        // Get the Customer number
        vCustomerId = vvjson_getInt(orderInfo : 'CUSNO');
        // Get the array of data
        orders    = vvjson_getArray(orderInfo : 'orders');
        // Get the size for the loop
        ordArrsz  = vvjsona_size(orders);
        for x = 0 to ordArraz -1;
           // Get the object at our sequence number
           order   = vvjsona_getObject(order :x);
           // Get each element of our array by name
           vOrderId = vvjson_getInt(order : 'ORDERNO');
           vStatus  = vvjson_getString(order : 'ORDSTS');
           //... continue to get any applicable fields
        Endfor;
       Endfor;
       // Dispose of our pointer
       vvjsona_dispase(base);
      /End-free

 */

/**
 @method _getInt
 Returns the integer value of the entry.

 @param {pointer, const (required)} pointer
 The pointer reference to the object.

 @param {32766a, const, varying (required)} string
 The name of the name/value pair.

 @return {integer}
 An integer value will be returned.

 # RPG Example

     d vOrderId       s             10i 0
     d order          s               *
      // Get the integer value
      vOrderId = vvjson_getInt(order : 'ORDERNO');
 */

/**
 @method _getString
 Returns the string value of an entry.

 @param {pointer (required)} pointer
 The pointer reference to the object.

 @param {32766a, const, varying (required)} string
 The name of the name/value pair.

 @return {string}
 A string value will be returned.

 # RPG Example
     d vName          s             25a
     d order          s               *
      /Free
        // Get the string Value
        vName = vvjson_getString(order : 'CNAME');
      /End-Free
 */

/**
 @method _parse
 Creates a JSON object or array from the string passed in from the browser.  The string must be null terminated.

 @param {pointer (optional)} pointer
 The pointer reference to the string.

 @param {pointer (optional)} pointer
 Pointer for the parser header (only for internal use).

 @return {pointer}
 Returns a pointer to the parsed object.

 #RPG Example
     d vString        s         65535a
     d base           s              *
      /Free
        // Pull in the string of JSON formatted data
        vString = vvIn_Char('data');
        //Set a pointer and parse the string
        base   = vvjson_parse(%addr(vString));
      /End-Free
 */
/**
 @method _putArray
 Adds an entry type of array.

 @param {pointer, const (required)} pointer
 Pointer to the JSON array.

 @param {32766a, const, varying (required)} string
 The name of the name/value pair.

 @param {pointer, const (required)} pointer
 The pointer to the array.

 #RPG Example
     d OrderArray       s               *
     d vJsonRoot        s               *
      /Free
        // Put an array in the JSON root
        vvjson_putArray(vJsonRoot: 'records' : OrderArray);
      /End-Free

 */

/**
 @method _putBoolean
 Adds an entry type of boolean.

 @param {pointer (required)} pointer
 Pointer to the JSON object.

 @param {65535a, const varying (required)} string
 The name of the name/value pair.

 @param {boolean (required)} boolean
 Boolean values:

   - `*ON`  indicates success.
   - `*OFF` indicates an error occurred.

 #RPG Example
     d vJsonRoot      s               *
      /Free
        // Put a boolean name/value pair in the JSON root
        vvjson_putBoolean(vJsonRoot : 'success' : '*ON');
      /End-Free
 */
/**
 @method _putInt
 Adds an entry type of integer.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 @param {65535a, const varying (required)} string
 The name of the name/value pair.

 @param {10i0,const (required)} integer
 The integer value.

 #RPG Example
     d vOrderId       s             10i 0
     d order          s               *
      /Free
        vOrderId = 2345;
        // Put an integer name/value pair in order
        vvjson_putInt(order : 'ordernumber' : vOrderId);
      /End-Free

 */

/**
 @method _putString
 Adds an entry type of string.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 @param {65535a, const varying (required)} string
 The name of the name/value pair.

 @param {65535a, const varying (required)} string
 The string value.

 #RPG Example
     d vCNAME         s             25a
     d customers      s               *
      /Free
        vCNAME = 'Company 1';
        // Put a string name/value pair in customers
        vvjson_putString(customers : 'customername' : vCNAME);
      /End-Free

 */


/**
 @method _toString
 Creates a null terminated string representation of the JSON object in JSON syntax.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 @return {pointer}
 Returns a pointer to the string.

 #RPG Example
     d vJsonRoot      s               *
     d vJsonString    s           1000a
      /Free
        // Convert the JSON root to a string value
        vJasonString = %Str(vvjson_toString(vJsonRoot);
      /End-Free
 */

/**
 @method _dispose
 The memory for the JSON object will be deallocated and the passed pointer will be set to *null.

 @param {pointer (required)} pointer
 Pointer to the JSON object.

 #RPG Example
     d vJsonRoot      s               *
      /Free
       //...Your code
       vvjson_dispose(vJsonRoot);
      /End-Free
 */

/**
 @method _abortIteration
 Resets the iteration of the JSON object.

 @param {pointer (required)} pointer
 Pointer to the JSON object.

 #RPG Example
     d vJsonObject      s            *
       /Free
        //...Your code
        vvjson_abortIteration(vJsonObject);
      /End-Free
 */

/**
 @method _clear
 Removes all entries from the JSON object.

 @param {pointer (required)} pointer
 Pointer to the JSON object.

 #RPG Example
     d vJsonObject    s               *
      /Free
       //...Your code
       vvjson_clear(vJsonObject);
      /End-Free
 */

/**
 @method _contains
 Checks if the JSON object contains an entry with the Key.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 @param {65535a, const varying (required)} string
 The name of the name/value pair.

 @return {boolean}

 - `*ON`  indicates success.
 - `*OFF` indicates an error occurred.

 #RPG Example
     d order          s               *
      /Free
       // Look for the existence of the string value
       If vvjson_contains(order : 'CNAME') = *on;
        // Do something here...
       Endif;
      /End-Free
 */

/**
 @method _get
 Gets the value of entry in the JSON object as a string.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 @param {65535a, const varying (required)} string
 The name of the name/value pair.

 @return {pointer}
 Pointer a null terminated string.  Or of the JSON object does not contain the passed string it will be *null.

 #RPG Example
     d order          s               *
      /Free
       // Get the string Value
       If vvjson_get(order : 'CNAME') <> *null;
        // Do something here...
       Endif;
      /End-Free
 */

/**

@method _getDouble
Returns the double value of the entry.

@param {pointer, const (required)} pointer
Pointer to the JSON object.

@param {65535a, const varying (required)} string
The name of the name/value pair.

@return {double}
Returns floating value.

#RPG Example

     d order          s               *
     d vFloat         s              8f
      /Free
       // Get the entry type
        vFloat = vvjson_getDouble(order : 'PRICE');
      /End-Free
 */

/**
 @method _getEntryType
 Returns the type of the entry.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 @param {65535a, const varying (required)} string
 The name of the name/value pair.

 @return {10i0}
 Returns the integer type.

 #RPG Example
     d order          s               *
     d vType          s             10i 0
      /Free
       // Get the entry type
        vType = vvjson_getEntryType(order : 'CNAME');
      /End-Free
 */

/**
 @method _getLong
 Returns the long value of the entry.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 @param {65535a, const varying (required)} string
 The name of the name/value pair.

 @return {10i0}
 Returns the long value.

 #RPG Example
     d order          s               *
     d vLong          s             10i 0
      /Free
       // Get the entry type
       vLong = vvjson_getLong(order : 'NUMBER');
     /End-Free
 */



/**
 @method _getNext
 Iterates through all the JSON entries and returns a pointer to the key of the next entry.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 @return {pointer}
 Returns the pointer to the next entry.  *null is returned if there are no more entries.

 #RPG Example
    d order          s               *
     /Free
      // Get the next entry
      If vvjson_getNext(order) <> *null;
       // Do something here...
      Endif;
     /End-Free
 */

/**
 @method _getObject
 Returns a pointer to the object.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 @param {65535a, const varying (required)} string
 The name of the name/value pair.

 @return {pointer}
 Returns a pointer to the object.

 #RPG Example
     d order          s               *
     d orderPtr       s               *
      /Free
       // Get the pointer
       orderPtr = vvjson_getObject(order : 'ORDERNUM');
      /End-Free
 */

/**
 @method _isEmpty
 Checks the JSON object for entries.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 @return {boolean}

 - `*ON`  JSON object is empty.
 - `*OFF` JSON object is not empty.

 #RPG Example
     d order          s               *
      /Free
       // See if the object is empty
       If vvjson_isEmpty(order) = *off;
        // Do something here...
       Endif;
      /End-Free
 */

/**
 @method _isNull
 Checks the JSON object to see if it's null.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 @return {boolean}

 - `*ON`  entry is type null.
 - `*OFF` entry is another type.

 #RPG Example
     d order          s               *
      /Free
       // See if the object is null
       If vvjson_isNull(order) <> *off;
        // Do something here...
       Endif;
      /End-Free
 */

/**
 @method _putNull
 Adds an entry type of null to the array.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 @param {65535a, const varying (required)} string
 The name of the name/value pair.

 #RPG Example
     d order          s               *
      /Free
       // Insert a null
       vvjson_putNull(order : 'CUSNO');
      /End-Free
 */

/**
 @method _putObject
 Adds an entry type of null to the array.

 @param {pointer, const (required)} pointer
 Pointer to the JSON array.

 @param {65535a, const varying (required)} string
 The name of the name/value pair.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 #RPG Example
     d order          s               *
     d customer       s               *
      /Free
       vvjson_putObject(order : 'CUSNO' : customer);
      /End-Free
 */

/**
 @method _remove
 Removes the entry from the JSON object.

 @param {pointer, const (required)} pointer
 Pointer to the JSON object.

 @param {65535a, const varying (required)} string
 The name of the name/value pair.

 #RPG Example
     d order          s               *
      /Free
       vvjson_remove(order : 'CUSNO');
      /End-Free
 */

/**
 @method _putDouble
 Adds an entry type of double to the object.

 @param {pointer, const (required)} pointer
 Pointer to the JSON array.

 @param {65535a, const varying (required)} string
 The name of the name/value pair.

 @param {floating, const (required)} floating
 Floating point entry.

 #RPG Example
     d order          s               *
     d vPrice         s              8f
      /Free
       vvjson_putDouble(order : 'PRICE' : vPrice);
      /End-Free
 */

/**
 @method _putLong
 Adds an entry type of long to the object.

 @param {pointer, const (required)} pointer
 Pointer to the JSON array.

 @param {65535a, const varying (required)} string
 The name of the name/value pair.

 @param {10i0, const (required)} 10i0
 Floating point entry.

 #RPG Example
     d order          s               *
     d vLong          s             10i 0
      /Free
       vvjson_putLong(order : 'QUANTITY' : vLong);
      /End-Free
 */