-
Notifications
You must be signed in to change notification settings - Fork 0
/
jm
executable file
·398 lines (321 loc) · 11.1 KB
/
jm
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env php
<?php
# (C) Copyright Peter Koppstein ([email protected])
# License: Apache License 2.0 (see website)
# Website: https://github.com/pkoppstein/jm
# With special thanks to https://github.com/halaxa, the creator of "JSON Machine".
$jmVersion="0.0.11 of 2023.01.04";
# NEWS:
# 0.0.8 allow process file descriptors
# 0.0.9 --tag KEYNAME
# 0.0.10 --keys
$bn=basename(__FILE__);
$home = exec( 'echo ~' ) ;
$dirs = array(
$home.'/vendor/autoload.php',
__DIR__.'/vendor/autoload.php',
dirname(__DIR__).'/vendor/autoload.php'
);
$autoload = '';
foreach ($dirs as &$value) {
if (is_file($value)) { $autoload = $value; break; }
}
if ($autoload == '') {
$autoload = $home . '/github/json-machine/src/autoloader.php';
if (is_file($autoload)) {
spl_autoload_register(require $autoload);
} else {
fwrite(STDERR, __FILE__ .
': composer autoloader missing. Try running "composer require halaxa/json-machine".'."\n");
exit(1);
}
} else {
require_once $autoload;
}
use JsonMachine\Items;
use JsonMachine\JsonDecoder\PassThruDecoder;
function usage() {
global $bn;
echo <<<END
Usage: {$bn} [ OPTIONS ] [ FILEPATH ... ]
or: {$bn} [-h | --help]
where FILEPATH defaults to stdin, and the other options are:
-s | --keys | --tag KEYNAME
--array
--bigint_as_string | --recode
--count | --limit=LIMIT
--pointer=JSONPOINTER *
--version
The --tag option precludes the --array, --keys, and -s options.
JSONPOINTER defaults to ''.
* Several pointers may be specified by repeating the --pointer option.
END;
}
function help() {
global $bn, $jmVersion;
echo <<<END
The -h option gives a usage summary, and --help gives more detailed help.
The other options are described below.
One of the main uses of {$bn} is to stream losslessly a JSON array or
JSON object that occurs at the top-level or within a very large JSON structure.
In this document, streaming a JSON array is to be understood as
producing a stream of the top-level items in the array (one line per
item), or just the indices if the -s or --keys option is specified.
Similarly, streaming a JSON object means producing a stream of
the top-level values, or of the key-value singleton objects if the -s
option is specified, or of just the keys if the --keys option is specified.
Streaming other JSON values simply means printing them, though the way
in which JSON numbers are printed depends on the mutually exclusive
`--recode` and `--bigint_as_string` options:
* --recode causes all JSON numbers to be presented as PHP numeric values
with the potential loss of information this implies;
* --bigint_as_string causes JSON "big integers" to be converted to
strings to avoid loss of information, but other numbers will be
converted to PHP numeric values;
* if neither of these options is specified, the literal form of numbers is preserved.
If no JSON Pointer is specified, or if it is specified as the empty
string (""), then the top-level entity is streamed.
If a JSON Pointer is specified, then all the corresponding entities
are streamed successively, as illustrated in the last example in the
Examples section below.
The -s option directs that JSON objects be streamed as singleton (single-key) objects.
The --keys option specifies that JSON objects and arrays be streamed by emitting
only their keys or indices.
The --array option causes the streamed items to be presented as a JSON array.
The --count option specifies that for each input source or file, the
count of the number of lines that would be emitted by this script when
streaming should be printed instead; for each file, the count is
preceded by the file name.
The --limit option limits the total number of lines that are emitted
by this script per input file, when streaming.
If --tag KEYNAME is specified, then instead of printing a JSON value, X,
on a line, the values TAG and X are printed as tab-separated values on
a single line:
TAG<tab>X
where TAG is the value associated with the key named KEYNAME if X is
an object with that key, or else the empty string; that is, if X is
not a JSON object with the specified key, then no TAG is printed.
Examples:
(1) jm <<< '[1,"2", {"a": 4}, [5.0000000000000000000000000006]]'
yields:
1
"2"
{"a":4}
[5.0000000000000000000000000006]
(2) jm --tag a <<< '[{"a": 1}, {"a": [2]}, {"b": [3]}]' | sed 's/\t/<tab>/'
yields:
1<tab>{"a": 1}
[2]<tab>{"a": [2]}
<tab>{"b": [3]}
(3) jm --keys <<< '{"a": 1, "b": [2]}'
yields:
"a"
"b"
(4) jm <<< '{"a": 1, "b": [2,3]}'
yields
1
[2,3]
(5) jm -s <<< '{"a": 1, "b": [2,3]}'
yields
{"a": 1}
{"b": [2,3]}
(6) jm --pointer /a --pointer /b <<< '{"a": 1, "b": [2,3]}'
yields
1
2
3
(7) jm --pointer /results <<< '{"results": {"a": 1, "b": [2,3]}}'
yields the same stream as (2) above.
(8) jm --bigint_as_string <<< '[10000000000000000000002, 3.0000000000000000000004]'
yields
"10000000000000000000002"
3
(9) jm --array <<< '{"a": 1, "b": [2,3]}'
yields
[
1,
[2,3]
]
(10) jm --recode <(echo '[1.000000000000000001,20000000000000000003]')
yields
1
2.0e+19
(11) jm --tag a <<< '[{"a": 1}, {"a": [2]}, {"b": [3]}]' | sed 's/\t/<tab>/'
yields
1<tab>{"a":1}
[2]<tab>{"a":[2]}
<tab>{"b":[3]}
(12) jm --pointer "/-" <<< '[1,[2,3]]'
yields
1
2
3
Note that in the last example, the JSON Pointer "/-" points in turn to
the items in the top-level array (i.e. 1 and then [2,3]), and that
streaming 1 produces 1, and streaming [2,3] produces 2 and then 3.
{$bn} is based on JSON Machine. For further details, see documentation on:
* "JSON Machine" e.g. https://github.com/halaxa/json-machine
* "JSON Pointer" e.g. https://www.rfc-editor.org/rfc/rfc6901#section-5
This is version {$jmVersion} of {$bn}.
\n
END;
exit(0);
}
$options = getopt("hs", ['debug', 'help', 'array', 'bigint_as_string', 'count', 'keys',
'limit:', 'pointer:', 'recode', 'tag:', 'version'],
$rest_index);
if (isset($options['help'])) { usage(); help(); exit(0); }
if (isset($options['h'])) { usage(); exit(0); }
if (isset($options['version'])) {
print "$bn version {$jmVersion}\n";
exit(0);
}
$singleton_option = isset($options['s']);
$count_option = isset($options['count']);
$debug_option = isset($options['debug']);
$recode_option = isset($options['recode']);
$bigint_option = isset($options['bigint_as_string']);
$array_option = isset($options['array']);
$limit_option = isset($options['limit']) ? $options['limit'] : false;
$tag_option = isset($options['tag']) ? $options['tag'] : false;
$keys_option = isset($options['keys']);
$suffix = isset($options['array']) ? ",\n" : "\n";
# Disallow: -s --keys
if ($singleton_option && $keys_option) {
fwrite(STDERR, __FILE__ .
": the --s and --keys options are mutually exclusive.\n");
exit(2);
}
# Disallow: --tag if -s, --keys, or --array is specified
if ($tag_option && ($singleton_option || $array_option || $keys_option)) {
fwrite(STDERR, __FILE__ .
": the --tag option precludes the --array, -s and --keys options.\n");
exit(2);
}
# Disallow: --bigint_as_string --recode
if ($recode_option && $bigint_option) {
fwrite(STDERR, __FILE__ .
": at most one of the --recode and --bigint_as_string options may be specified.\n");
exit(2);
}
# Disallow: --count --limit
if ($count_option && $limit_option) {
fwrite(STDERR, __FILE__ .
": at most one of the --count and --limit options may be specified.\n");
exit(2);
}
$pointer_options = isset($options['pointer']) ? ["pointer" => $options['pointer']] : [];
# By default, use the PassThruDecoder
if (! $recode_option) {$pointer_options += ['decoder' => new PassThruDecoder ]; }
## Debugging info:
if ($debug_option) {
if ($recode_option) { print "NOTE: --recode option was specified\n"; }
if ($bigint_option) { print "NOTE: --bigint_as_integer was specified\n"; }
if ($limit_option) { print "NOTE: --limit={$limit_option} was specified\n"; }
if (isset($options['pointer'])) {
print "Note: JSONPOINTER is: " . $options['pointer'] . "\n";
}
}
# Down the recoding rabbit hole:
function tweak($value) {
global $bigint_option;
global $recode_option;
# var_dump($value);
if ($recode_option) { return json_encode($value); }
if ($bigint_option) { return json_encode(json_decode($value, flags: JSON_BIGINT_AS_STRING)); }
return $value;
}
# maybe tag the value
function tag($value) {
global $tag_option;
$v = tweak($value);
if ($tag_option) {
$json = json_decode($value);
if (is_object($json) and property_exists($json, $tag_option)) {
echo tweak(json_encode($json -> $tag_option));
}
echo "\t" . $v;
} else {
echo $v;
}
}
function process_objects($filepath, $options) {
global $array_option;
global $count_option;
global $debug_option;
global $keys_option;
global $limit_option;
global $suffix;
$preface = ($filepath == "php://stdin") ? "" : $filepath. ": ";
$i=0;
$iterator = Items::fromFile( $filepath, $options);
if ($count_option) {
foreach ($iterator as $key => $value) { $i++; }
print $preface . $i ."\n";
return;
}
if ($array_option) { print "[\n"; }
foreach ($iterator as $key => $value) {
if (gettype($key) == "integer") {
if ($i > 0 ) { print $suffix; }
print tweak($key);
} else {
if ($i > 0 ) { print $suffix; }
if ($keys_option) {
if ($debug_option) { print 'debug: key is ' . $key . "\n"; }
print tweak($key);
} else {
print '{' . tweak($key) . ': ' . tweak($value) . "}" ;
}
}
$i++;
if ($limit_option && ($i >= $limit_option)) {break;}
}
if ($array_option) { print "\n]\n"; } else {print "\n";}
}
function process($filepath, $options, $singleton_option) {
global $limit_option;
global $count_option;
global $array_option;
global $keys_option;
global $suffix;
if ($singleton_option || $keys_option) { process_objects($filepath, $options); }
else {
$preface = ($filepath == "php://stdin") ? "" : $filepath. ": ";
$i=0;
$iterator = Items::fromFile( $filepath, $options);
if ($count_option) {
foreach ($iterator as $key => $value) { $i++; }
print $preface . $i . "\n";
return;
}
if ($array_option) { print "[\n"; }
foreach ($iterator as $row) {
if ($i > 0) { echo $suffix; }
echo tag($row);
$i++;
if ($limit_option && ($i >= $limit_option)) {break;}
}
if ($array_option) { print "\n]\n"; } else {print "\n";}
}
}
$pos_args = array_slice($argv, $rest_index);
$argx = count($pos_args);
if ($argx == 0) { process('php://stdin', $pointer_options, $singleton_option); exit(0); }
# Interpret the remaining arguments as filepaths or process file descriptors (/dev/fd/...) if possible.
foreach( $pos_args as &$filepath ) {
if (preg_match('!^/dev/fd/[0-9]*$!', $filepath)) {
if (file_exists($filepath)) { continue; }
else {
fwrite(STDERR, __FILE__ . ': specified process file descriptor not found: ' . $filepath . "\n");
exit(1);
}
}
if (! is_file($filepath)) {
fwrite(STDERR, __FILE__ . ': specified file not found: ' . $filepath . "\n");
exit(1);
}
}
foreach( $pos_args as &$filepath ) {
process( $filepath, $pointer_options, $singleton_option );
}