Code: Select all
**free
ctl-opt dftactgrp(*no) actgrp('KLEMENT') bnddir('YAJL');
/copy YAJL_H
dcl-ds CarrierList qualified;
num_carrier int(10) inz(0);
dcl-ds carrier dim(999);
name varchar(30);
id varchar(6);
service varchar(15);
end-ds;
validationErrors varchar(500);
end-ds;
dcl-s docNode like(YAJL_VAL);
dcl-s errMsg varchar(500);
dcl-s carriersNode like(YAJL_VAL);
dcl-s i int(10);
dcl-s count int(10);
dcl-s key varchar(50);
dcl-s val like(YAJL_VAL);
dcl-s msg char(52);
// ----------------------------------------------------------
// Load the JSON document from the IFS
// ----------------------------------------------------------
docNode = yajl_stmf_load_tree('pargent70.json': errMsg);
if docNode = *null;
// TODO: handle error
endif;
// ----------------------------------------------------------
// Find the "Carriers" node in the object
// ----------------------------------------------------------
carriersNode = yajl_object_find(docNode: 'Carriers');
if carriersNode = *null;
// Error: There was no "Carriers" node in the object.
// TODO: Handle error
endif;
// ----------------------------------------------------------
// Load the list of carriers from the "Carriers" object
// into our data structure.
// ----------------------------------------------------------
i = 0;
count = 0;
dow YAJL_OBJECT_LOOP(carriersNode: i: key: val);
count += 1;
carrierList.num_carrier = count;
CarrierList.carrier(count).name = key;
CarrierList.carrier(count).id = grabString(val: 'CarrierID');
CarrierList.carrier(count).service = grabString(val: 'CarrierService');
enddo;
// ----------------------------------------------------------
// Read the validationErrors field
// ----------------------------------------------------------
carrierList.validationErrors = grabString(docNode: 'ValidationErrors');
// ----------------------------------------------------------
// Free up YAJL resources now that everying has been
// loaded into the DS
// ----------------------------------------------------------
yajl_tree_free(docNode);
// ----------------------------------------------------------
// TODO: This is just to prove that the concept works.
// Replace with "real" business logic in the
// finished program.
// ----------------------------------------------------------
for i = 1 to carrierList.num_carrier;
msg = carrierList.carrier(i).name
+ ' id=' + carrierList.carrier(i).id
+ ' srv=' + carrierList.carrier(i).service;
dsply msg;
endfor;
*inlr = *on;
return;
// ----------------------------------------------------------
// grabString(): Returns a string node from within an object
// or '' if the node wasn't found
//
// obj = (input) object to locate the key in
// keyName = (input) key ("field name") of the string
// within the json object ("obj")
//
// Returns the value of the string, or '' if the string
// was not found or wasn't a string.
// ----------------------------------------------------------
dcl-proc grabString;
dcl-pi *n varchar(65535);
obj like(YAJL_VAL) value;
keyName varchar(50) const;
end-pi;
dcl-s result varchar(65535) static;
dcl-s tempNode like(YAJL_VAL) static;
tempNode = yajl_object_find(obj: keyName);
if tempNode = *null;
result = '';
else;
result = yajl_get_string(tempNode);
endif;
return result;
end-proc;