1 The `json()` function parses the given string value as JSON. 2 3 Throws an exception if the given input value is not a string. 4 Throws an exception if the given input string cannot be parsed as JSON. 5 6 Returns the resulting value. 7 8 -- Testcase -- 9 {% 10 print(join("\n", [ 11 json("null"), 12 json("true"), 13 json("false"), 14 json("123"), 15 json("456.7890000"), 16 json("-1.4E10"), 17 json("1e309"), 18 json('"A string \u2600"'), 19 json("[ 1, 2, 3 ]"), 20 json('{ "test": [ 1, 2, 3 ] }'), 21 22 // surrounding white space is ignored 23 json(' [ 1, 2, 3 ] ') 24 ]), "\n"); 25 %} 26 -- End -- 27 28 -- Expect stdout -- 29 null 30 true 31 false 32 123 33 456.789 34 -14000000000 35 Infinity 36 A string ☀ 37 [ 1, 2, 3 ] 38 { "test": [ 1, 2, 3 ] } 39 [ 1, 2, 3 ] 40 -- End -- 41 42 43 Passing a non-string value throws an exception. 44 45 -- Testcase -- 46 {% 47 json(true); 48 %} 49 -- End -- 50 51 -- Expect stderr -- 52 Type error: Passed value is neither a string nor an object 53 In line 2, byte 11: 54 55 ` json(true);` 56 Near here ---^ 57 58 59 -- End -- 60 61 62 Unparseable JSON throws exceptions. 63 64 -- Testcase -- 65 {% 66 json('[ "incomplete", "array" '); 67 %} 68 -- End -- 69 70 -- Expect stderr -- 71 Syntax error: Failed to parse JSON string: unexpected end of data 72 In line 2, byte 33: 73 74 ` json('[ "incomplete", "array" ');` 75 Near here -------------------------^ 76 77 78 -- End -- 79 80 -- Testcase -- 81 {% 82 json('invalid syntax'); 83 %} 84 -- End -- 85 86 -- Expect stderr -- 87 Syntax error: Failed to parse JSON string: unexpected character 88 In line 2, byte 23: 89 90 ` json('invalid syntax');` 91 Near here ---------------^ 92 93 94 -- End -- 95 96 -- Testcase -- 97 {% 98 json('[] trailing garbage'); 99 %} 100 -- End -- 101 102 -- Expect stderr -- 103 Syntax error: Trailing garbage after JSON data 104 In line 2, byte 28: 105 106 ` json('[] trailing garbage');` 107 Near here --------------------^ 108 109 110 -- End -- 111 112 113 Additionally, `json()` accepts objects implementing a read method as input. 114 During JSON parsing, the read method is repeatedly invoked with a buffer size 115 hint as sole argument. The return value of the read method is converted to a 116 string if needed and passed on to the JSON parser. A `null` or an empty string 117 return value is treated as EOF, ending the parse process. 118 119 -- Testcase -- 120 {% 121 let fs = require("fs"); 122 123 // parse JSON from open file handle 124 printf("%.J\n", 125 json(fs.open("files/test.json")) 126 ); 127 %} 128 -- End -- 129 130 -- Expect stdout -- 131 { 132 "hello": "world" 133 } 134 -- End -- 135 136 -- File test.json -- 137 {"hello":"world"} 138 -- End -- 139 140 141 The `json()` function is able to parse JSON from any object providing a `read()` 142 method that incrementally yields JSON source data. 143 144 -- Testcase -- 145 {% 146 let parts = [ 147 '{"some"', 148 ':', 149 '"object"', 150 ', ', 151 '"etc."', 152 ':', 153 !0, // this is stringified to "true" 154 '}' 155 ]; 156 157 let producer = { 158 read: function(size) { 159 return shift(parts); 160 } 161 }; 162 163 // parse JSON from producer object 164 printf("%.J\n", 165 json(producer) 166 ); 167 %} 168 -- End -- 169 170 -- Expect stdout -- 171 { 172 "some": "object", 173 "etc.": true 174 } 175 -- End -- 176 177 178 Passing objects or resources not providing a `read()` method yields an exception. 179 180 -- Testcase -- 181 {% 182 json({}); 183 %} 184 -- End -- 185 186 -- Expect stderr -- 187 Type error: Input object does not implement read() method 188 In line 2, byte 9: 189 190 ` json({});` 191 Near here -^ 192 193 194 -- End -- 195 196 197 Exceptions triggered by the `read()` method are properly forwarded. 198 199 -- Testcase -- 200 {% 201 json({ 202 read: function() { 203 die("Exception in read()"); 204 } 205 }); 206 %} 207 -- End -- 208 209 -- Expect stderr -- 210 Exception in read() 211 In [anonymous function](), line 4, byte 29: 212 called from function json ([C]) 213 called from anonymous function ([stdin]:6:3) 214 215 ` die("Exception in read()");` 216 Near here ---------------------------^ 217 218 219 -- End -- 220 221 222 EOF stops parsing and does not lead to further `read()` invocations. 223 224 -- Testcase -- 225 {% 226 let parts = [ 227 '["some",', 228 '"JSON array",', 229 'true,false,1,2,3', 230 ']', 231 '', // empty string treated as EOF 232 '{"some":', // this is not reached in the first pass 233 '"object"}', 234 null, // null treated as EOF 235 '"test ', // this is not reached in the second pass 236 'value"' 237 ]; 238 239 let producer = { read: () => shift(parts) }; 240 241 printf("%.J\n", [ 242 json(producer), 243 json(producer), 244 json(producer) 245 ]); 246 %} 247 -- End -- 248 249 -- Expect stdout -- 250 [ 251 [ 252 "some", 253 "JSON array", 254 true, 255 false, 256 1, 257 2, 258 3 259 ], 260 { 261 "some": "object" 262 }, 263 "test value" 264 ] 265 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt