/** * \brief Send JSON String * * This program creates a JSON string from a list of objects. It lists all * objects in the library QGPL with the linked list utilities service program. * * \author Mihael Schmidt * \date 05.08.2008 * * \link http://www.extjs.com Ext JS */ // The binding directory WEB contains entries for the // service programs JSON, LIST, LUTIL and QHTTPSVR/QTMHCGI. H dftactgrp(*no) actgrp(*caller) bnddir('WEB' : 'QC2LE') *--------------------------------------------------------------- * PEP *--------------------------------------------------------------- D main PR extpgm('JSON_EXTJS') D main PI *--------------------------------------------------------------- * Prototypes *--------------------------------------------------------------- D strlen PR 10I 0 extproc('strlen') D buffer1 * value * D writeStdOut PR extproc('QtmhWrStout') D data 32767A const options(*varsize) D dataLength 10I 0 const D errorCode 8000A options(*varsize) * D writeToStdOut PR D dataPtr * const D textLength 10I 0 const * /include 'json/json_h.rpgle' /include 'llist_h.rpgle' /include 'lutil_h.rpgle' *--------------------------------------------------------------- * Variables *--------------------------------------------------------------- D list S * D ptr S * D json S * D json_arr S * D json_entry S * D dataPtr S * D contentType S 30A D i S 10I 0 * D object DS qualified based(ptr) D library 10A D name 10A D type 10A * D CRLF C const(x'0d25') /free contentType = 'content-type: text/json' + CRLF + CRLF; // get all libraries list = lutil_listObjects('QGPL'); // // The created JSON string looks like this. // // { // objects : // [ // { ID : 1 , Library : 'QGPL' , Name : 'JAVAFBA' , Type : '*SQLPKG' }, // { ID : 2 , Library : 'QGPL' , Name : 'MSACCESSFBA' , Type : '*SQLPKG' }, // ... // ] // } // // The key objects is associated with an array. This array has x elements // (one for each object in QGPL). Each element is another JSON object and // each element represents one row in the grid. The keys of each row element // (Id, Library, Name, Type) must correspond with the ids in the Ext JS // data store and column model. // json = json_create(); json_arr = jsona_create(); ptr = list_getNext(list); dow (ptr <> *null); i += 1; json_entry = json_create(); json_putInt(json_entry : 'Id' : %int(i)); json_putString(json_entry : 'Library' : %trimr(object.library)); json_putString(json_entry : 'Name' : %trimr(object.name)); json_putString(json_entry : 'Type' : %trimr(object.type)); jsona_putObject(json_arr : json_entry); ptr = list_getNext(list); enddo; // assemble json string json_putArray(json : 'objects' : json_arr); dataPtr = json_toString(json); // send data to stdout in json format writeToStdOut(%addr(contentType) : %len(%trimr(contentType))); writeToStdOut(dataPtr : strlen(dataPtr)); // free memory json_dispose(json); list_dispose(list); *inlr = *on; return; /end-free /** * \brief Write to STDOUT * * Writes the passed data to STDOUT. * * \author Mihael Schmidt * * \param Pointer to the data * \param Data length */ P writeToStdOut B D PI D pointer * const D textLength 10I 0 const * D remainingLength... D S 10I 0 D len S 10I 0 D ptr S * D data S 32767A based(ptr) D errorcode S 1024A /free remainingLength = textLength; ptr = pointer; // write data to stdout in slices because the api // has a max data buffer of 32767 characters dow (remainingLength > 0); if (remainingLength > %size(data)); writeStdOut(data : %size(data) : errorcode); else; writeStdOut(%subst(data : 1 : remainingLength) : remainingLength : errorcode); endif; ptr += %size(data); remainingLength -= %size(data); enddo; /end-free P E