bvstone

YAJL Update

Posted:

YAJL Update

A while back I was trying to find out if the YAJL port from Scott Klement had a function that would return a pointer to node data as well as get the length.  See this thread:

http://archive.midrange.com/rpg400-l/201604/msg00138.html

What Scott, Barbara (and a little bit of me) came up with were to create two new subprocedures.  Following additions to the member YAJLR4 in QRPGLESRC:

      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      * YAJL_GET_STRING_UTF8(): Retrieve a pointer to the raw UTF-8
      *                         data for a YAJL string
      *
      *   node = (input) YAJL tree node
      *
      * returns a pointer to a C-style, zero-terminated string
      *      or *NULL if the node is not a string
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P YAJL_GET_STRING_UTF8...
     P                 B                   export
     D                 PI              *
     D   node                              like(yajl_val) value

     D nv              ds                  likeds(yajl_val_t)
     D                                     based(node)

      /free
       if YAJL_IS_STRING(node);
         return nv.string;
       else;
         return *null;
       endif;
      /end-free
     P                 E

      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      * YAJL_GET_STRING_BUF(): Retrieve a pointer to the YAJL String
      *
      *   node = (input) YAJL tree node
      *   buf = (input) pointer to buffer to load
      *   bufSize = (input) size of buffer to load
      *
      * returns size of buffer or -1 for error
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P YAJL_GET_STRING_BUF...
     P                 B                   export
     D                 PI            10U 0
     D   node                              like(yajl_val) value
     D   buf                           *   value
     D   bufSize                     10U 0 value

     D memcpy          pr              *   extproc('memcpy')
     D   dst                           *   value
     D   src                           *   value
     D   len                         10u 0 value

     d strlen          PR            10u 0 extproc('strlen')
     d  p                              *   value

     D nv              ds                  likeds(yajl_val_t)
     D                                     based(node)
     D str             s               *
     D len             s             10u 0
      /free
       if YAJL_IS_STRING(node);
          init_gconv();
          str = yajl_iconv_toLocal( gConv: nv.string );
          len = strlen(str);

          if (len > bufSize);
            len = bufSize;
          endif;

          memcpy(buf:str:len);
          yajl_iconv_string_free(str);
          return len;
       else;
          return -1;
       endif;
      /end-free
     P                 E

The YAJL_GET_STRING_UTF8 is exactly how Scott suggested.

The YAJL_GET_STRING_BUF is a little different.  Instead of returning a boolean (indicator) it returns the size of the string (or -1 for an error).

Here's an example of how I am using it:

 val = YAJL_object_find(node:'data');
 
 data@ = %alloc(MAX_SIZE);

 if (data@ = *null);
   //error
 endif;

 rc = yajl_get_string_buf(val:data@:MAX_SIZE);

 // do stuff with the data

 dealloc(en) l_data@;

After the call to yajl_get_string_buf, the variable rc will contain the size of the data and data@ will point to the data, converted from UTF8 to the jobs CCSID.

Other changes made were to the following additions to the /copy member YAJL_H in QRPGLESRC:

      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      * YAJL_GET_STRING_UTF8(): Retrieve a pointer to the raw UTF-8
      *                         data for a YAJL string
      *
      *   node = (input) YAJL tree node
      *
      * returns a pointer to a C-style, zero-terminated string
      *      or *NULL if the node is not a string
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     D YAJL_GET_STRING_UTF8...
     D                 PR              *
     D   node                              like(yajl_val) value

      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      * YAJL_GET_STRING_BUF(): Retrieve a pointer to the YAJL String
      *
      *   node = (input) YAJL tree node
      *   buf = (input) pointer to buffer to load
      *   bufSize = (input) size of buffer to load
      *
      * returns size of buffer or -1 for error
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     D YAJL_GET_STRING_BUF...
     D                 PR            10u 0
     D   node                              like(yajl_val) value
     D   buf                           *   value
     D   bufSize                     10u 0 value

And finally the updated binder langauge member YAJLR4 in QSRVSRC:

STRPGMEXP  SIGNATURE('YAJLR4 1.1      ')
  export symbol(yajl_buf_parse)
  export symbol(yajl_stmf_parse)
  export symbol(yajl_buf_load_tree)
  export symbol(yajl_stmf_load_tree)
  export symbol(yajl_is_array)
  export symbol(yajl_is_false)
  export symbol(yajl_is_null)
  export symbol(yajl_is_number)
  export symbol(yajl_is_object)
  export symbol(yajl_is_string)
  export symbol(yajl_is_true)
  export symbol(yajl_get_number)
  export symbol(yajl_get_string)
  export symbol(yajl_array_elem)
  export symbol(yajl_array_loop)
  export symbol(yajl_array_size)
  export symbol(yajl_object_elem)
  export symbol(yajl_object_find)
  export symbol(yajl_object_loop)
  export symbol(yajl_object_size)
  export symbol(yajl_genOpen)
  export symbol(yajl_genClose)
  export symbol(yajl_beginObj)
  export symbol(yajl_endObj)
  export symbol(yajl_beginArray)
  export symbol(yajl_endArray)
  export symbol(yajl_addChar)
  export symbol(yajl_addNum)
  export symbol(yajl_addBool)
  export symbol(yajl_addNull)
  export symbol(yajl_copyBuf)
  export symbol(yajl_saveBuf)
  export symbol(yajl_writeStdout)
  export symbol(yajl_stdin_load_tree)
  export symbol(yajl_addCharEx)
  export symbol(yajl_addCharStmf)
  export symbol(yajl_exbuf_new)
  export symbol(yajl_exbuf_concat_ptr)
  export symbol(yajl_exbuf_concat)
  export symbol(yajl_exbuf_free)
  export symbol(yajl_save_string_stmf)
  export symbol(yajl_getBuf)
  export symbol(yajl_tree_free_rpg)
  export symbol(yajl_get_string_utf8)
  export symbol(yajl_get_string_buf)
endpgmexp
STRPGMEXP  PGMLVL(*PRV) SIGNATURE('YAJLR4 1.0      ')
  export symbol(yajl_buf_parse)
  export symbol(yajl_stmf_parse)
  export symbol(yajl_buf_load_tree)
  export symbol(yajl_stmf_load_tree)
  export symbol(yajl_is_array)
  export symbol(yajl_is_false)
  export symbol(yajl_is_null)
  export symbol(yajl_is_number)
  export symbol(yajl_is_object)
  export symbol(yajl_is_string)
  export symbol(yajl_is_true)
  export symbol(yajl_get_number)
  export symbol(yajl_get_string)
  export symbol(yajl_array_elem)
  export symbol(yajl_array_loop)
  export symbol(yajl_array_size)
  export symbol(yajl_object_elem)
  export symbol(yajl_object_find)
  export symbol(yajl_object_loop)
  export symbol(yajl_object_size)
  export symbol(yajl_genOpen)
  export symbol(yajl_genClose)
  export symbol(yajl_beginObj)
  export symbol(yajl_endObj)
  export symbol(yajl_beginArray)
  export symbol(yajl_endArray)
  export symbol(yajl_addChar)
  export symbol(yajl_addNum)
  export symbol(yajl_addBool)
  export symbol(yajl_addNull)
  export symbol(yajl_copyBuf)
  export symbol(yajl_saveBuf)
  export symbol(yajl_writeStdout)
  export symbol(yajl_stdin_load_tree)
  export symbol(yajl_addCharEx)
  export symbol(yajl_addCharStmf)
  export symbol(yajl_exbuf_new)
  export symbol(yajl_exbuf_concat_ptr)
  export symbol(yajl_exbuf_concat)
  export symbol(yajl_exbuf_free)
  export symbol(yajl_save_string_stmf)
  export symbol(yajl_getBuf)
  export symbol(yajl_tree_free_rpg)
endpgmexp

 


Last edited 05/27/2016 at 16:04:14



Latest Posts:

BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i
Posted by January 27, 2019
BVSTools >> BVSTools Announcements
G4MSDRV Currently Not Supported G4MSDRV Currently Not Supported
Posted by January 17, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG
Posted by December 26, 2018
Programming >> RPG Programming
Create QRCODE in DDS Create QRCODE in DDS
Posted by September 21, 2018
Programming >> RPG Programming
Base64 Encoding a File with RPG Base64 Encoding a File with RPG
Posted by September 6, 2018
Programming >> RPG Programming
Building JSON with RPG and YAJL and Writing to Standard Output Building JSON with RPG and YAJL and Writing to Standard Output
Posted by August 31, 2018
Programming >> Proof of Concept (POC)
How to Delete Files or Empty Trash From Your Google Drive with your IBM i and RPG/ILE How to Delete Files or Empty Trash From Your Google Drive with your IBM i and RPG/ILE
Posted by July 24, 2018
BVSTools >> BVSTools Software Discussion >> GreenTools for G Suite (Google Apps) (G4G) Specific Discussion
GreenTools for G Suite (G4G) Updated to Include Delete and Empty Trash Function GreenTools for G Suite (G4G) Updated to Include Delete and Empty Trash Function
Posted by July 24, 2018
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
What to Do If Your License Keys Don't Work What to Do If Your License Keys Don't Work
Posted by July 18, 2018
BVSTools >> BVSTools Software Discussion
MAILTOOL Updated to Allow Failed Message on Invalid Recipient MAILTOOL Updated to Allow Failed Message on Invalid Recipient
Posted by May 20, 2018
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
Non HTTPS Callbacks Removed from GreenTools for G Suite (G4G) Non HTTPS Callbacks Removed from GreenTools for G Suite (G4G)
Posted by April 15, 2018
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
IBM i Related Survey Available IBM i Related Survey Available
Posted by April 7, 2018
IBM Power Systems >> (QGPL) IBM i
BVSTools Releases Braintree Webhook Open Source Application - Node.js BVSTools Releases Braintree Webhook Open Source Application - Node.js
Posted by April 5, 2018
Programming >> Open Source
BVSTools Now Offering Web Services (BETA) BVSTools Now Offering Web Services (BETA)
Posted by April 3, 2018
BVSTools >> BVSTools Announcements
Creating a Reverse SSL Proxy Using RPG on the IBM i - Part 2 Creating a Reverse SSL Proxy Using RPG on the IBM i - Part 2
Posted by March 29, 2018
Programming >> Web Programming

Reply




Copyright 1983-2018 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).