|
1 | 1 | #!/usr/bin/awk -f
|
2 | 2 | #
|
3 | 3 | # Software: JSON.awk - a practical JSON parser written in awk
|
4 |
| -# Version: 1.12 |
5 |
| -# Author: step- on github.com |
| 4 | +# Version: 1.2 |
| 5 | +# Author: user step- on GitHub.com |
6 | 6 | # License: This software is licensed under the MIT or the Apache 2 license.
|
7 | 7 | # Project home: https://github.com/step-/JSON.awk.git
|
8 | 8 | # Credits: This software includes major portions of JSON.sh, a pipeable JSON
|
|
16 | 16 | # printf "%s\n" Filepath [Filepath...] | awk [-v Option="value"...] -f JSON.awk
|
17 | 17 | # Options: (default value in braces)
|
18 | 18 | # BRIEF=: 0 or 1 when 1 don't print non-leaf nodes {1}
|
19 |
| -# STREAM=: 0 or 1 when 0 store jpaths in JPATHS[] for stub function apply() {1} |
20 |
| -# Setting STREAM=0 is intended for custom applications that rewrite function apply(). |
| 19 | +# STREAM=: 0 or 1 when 0 don't output and call externally-defined callbacks. |
| 20 | +# Setting STREAM=0 is intended for custom applications that embed JSON.awk. |
21 | 21 |
|
22 | 22 | BEGIN { #{{{
|
23 | 23 | if (BRIEF == "") BRIEF=1 # when 1 parse() omits printing non-leaf nodes
|
@@ -48,31 +48,22 @@ BEGIN { #{{{
|
48 | 48 | reset() # See important application note in reset()
|
49 | 49 |
|
50 | 50 | tokenize($0) # while(get_token()) {print TOKEN}
|
51 |
| - if (0 == parse()) { |
52 |
| - apply(JPATHS, NJPATHS) |
| 51 | + if (0 == parse() && 0 == STREAM) { |
| 52 | + # Call back the embedding program passing an array of jpaths. |
| 53 | + cb_jpaths(JPATHS, NJPATHS) |
53 | 54 | }
|
54 | 55 | }
|
55 | 56 | #}}}
|
56 | 57 |
|
57 | 58 | END { # process invalid files {{{
|
58 |
| - for(name in FAILS) { |
59 |
| - print "invalid: " name |
60 |
| - print FAILS[name] |
| 59 | + if (0 == STREAM) { |
| 60 | + # Call back the embedding program passing an associative array |
| 61 | + # of failed objects. |
| 62 | + cb_fails(FAILS, NFAILS) |
61 | 63 | }
|
62 | 64 | }
|
63 | 65 | #}}}
|
64 | 66 |
|
65 |
| -# Function apply is a stub reserved for custom applications. It matters only |
66 |
| -# when STREAM=0. In its default form below apply() simply replicates JSON.sh's |
67 |
| -# output mode. JSON.sh prints in function parse() when STREAM=1 only. Default |
68 |
| -# stub function apply() prints below when STREAM=0 only because then (and only |
69 |
| -# then) size can be > 0. |
70 |
| -function apply (ary, size, i) { # stub {{{ |
71 |
| - for (i=1; i <= size; i++) |
72 |
| - print ary[i] |
73 |
| -} |
74 |
| -#}}} |
75 |
| - |
76 | 67 | function get_token() { #{{{
|
77 | 68 | # usage: {tokenize($0); while(get_token()) {print TOKEN}}
|
78 | 69 |
|
@@ -235,9 +226,18 @@ function reset() { #{{{
|
235 | 226 | #}}}
|
236 | 227 |
|
237 | 228 | function scream(msg) { #{{{
|
| 229 | + NFAILS += (FILENAME in FAILS ? 0 : 1) |
238 | 230 | FAILS[FILENAME] = FAILS[FILENAME] (FAILS[FILENAME]!="" ? "\n" : "") msg
|
239 |
| - msg = FILENAME ": " msg |
240 |
| - print msg >"/dev/stderr" |
| 231 | + if(0 == STREAM) { |
| 232 | + # Call back the embedding program passing the error message, |
| 233 | + # which will be printed to stderr if the callback returns non-zero. |
| 234 | + if(cb_fail1(msg)) { |
| 235 | + print FILENAME ": " msg >"/dev/stderr" |
| 236 | + } |
| 237 | + } else { |
| 238 | + # Print error message when not not embedded. |
| 239 | + print FILENAME ": " msg >"/dev/stderr" |
| 240 | + } |
241 | 241 | }
|
242 | 242 | #}}}
|
243 | 243 |
|
|
0 commit comments