-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsontime2.rpgle
114 lines (97 loc) · 4.15 KB
/
jsontime2.rpgle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//------------------------------------------------------------------
// Desc: This is an example of running curl and processing results in RPG as JSON
//
// Steps in process:
// Run QSHCURL curl command to get time from: date.jsontest.com as JSON.
// QSHCURL writes results to IFS file so they can be consumed.
// YAJL functions read the json results and parses the data.
//
// Requirements:
// YAJL library needed from ScottKlement.com and must be in library list
// https://www.scottklement.com/yajl
//
// QShell on i library QSHONI needs to be installed.
// https://www.github/com/richardschoen/qshoni
//
// Article ref links:
// https://www.itjungle.com/2018/01/22/guru-using-curl-interact-web-services/
//------------------------------------------------------------------
H DFTACTGRP(*NO) ACTGRP('YAJL') OPTION(*SRCSTMT)
H BNDDIR('YAJL')
/include yajl_h
// Data structure for date.jsontest.com JSON results
D list_t ds qualified
D template
D date 16A
D time 16A
D milliseconds 15p 0
*// Result list of 1 entry expected
D result ds qualified
D success 1n
D errmsg 500a varying
D list likeds(list_t) dim(1)
D docNode s like(yajl_val)
D list s like(yajl_val)
D node s like(yajl_val)
D val s like(yajl_val)
/free
// https://www.itjungle.com/2018/01/22/guru-using-curl-interact-web-services/
Dcl-S i int(10);
Dcl-S lastElem int(10);
Dcl-S cmderror int(10);
Dcl-S cmdline char(4096);
Dcl-S errMsg varchar(500);
dcl-S ifsoutput varchar(255);
Dcl-S msg char(52);
// Command line execution
dcl-pr qcmdexc ExtPgm;
cmd char(4096) Const Options(*VarSize);
cmdLength packed(15: 5) Const;
end-pr;
// Set the IFS output file name for JSON
ifsoutput = '/tmp/jsontime.json';
// Build curl command to run ro get date from date.jsontest.com
// STDOUT results from curl go to IFS file /tmp/jsontime.json
cmdline='QSHONI/QSHCURL CMDLINE(''--url date.jsontest.com'') ' +
'IFSSTDOUT(*YES) ' +
'IFSFILE(''' + %trim(ifsoutput) + ''') ' +
'IFSOPT(*REPLACE)';
// Run QSHCURL command and handle any errors nicely.
monitor;
qcmdexc(%trim(cmdline):%len(%trim(cmdline)));
cmdError=0;
on-error;
cmdError=1;
endmon;
// If errors, exit the program
if cmdError = 1;
*inlr=*on;
return;
endif;
// Load JSON file into memory node
node = yajl_stmf_load_tree(%trim(ifsoutput) : errMsg );
if errMsg <> '';
// handle error
endif;
// Load JSON results to array. We only use element one
// since date.jsontest.com only returns one date/time element
i = 1;
val = YAJL_object_find(node: 'date');
result.list(i).date = yajl_get_string(val);
val = YAJL_object_find(node: 'time');
result.list(i).time = yajl_get_string(val);
val = YAJL_object_find(node: 'milliseconds_since_epoch');
result.list(i).milliseconds = yajl_get_number(val);
// Display parsed JSON result fields
// after they have been extracted with YAJL
msg='Date: ' + result.list(i).date;
dsply msg;
msg='Time: ' + result.list(i).time;
dsply msg;
msg= 'Milliseconds since epoch: ' +
%char(result.list(i).milliseconds);
dsply msg;
// Release YAJL object memory
yajl_tree_free(node);
*inlr = *on;
return;