… | |
… | |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include "assert.h" |
6 | #include "string.h" |
6 | #include "string.h" |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
8 | |
8 | |
9 | #define F_ASCII 0x00000001 |
9 | #define F_ASCII 0x00000001UL |
10 | #define F_UTF8 0x00000002 |
10 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004 |
11 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008 |
12 | #define F_CANONICAL 0x00000008UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
13 | #define F_SPACE_BEFORE 0x00000010UL |
14 | #define F_SPACE_AFTER 0x00000020 |
14 | #define F_SPACE_AFTER 0x00000020UL |
15 | #define F_JSON_RPC 0x00000040 |
|
|
16 | #define F_ALLOW_NONREF 0x00000080 |
15 | #define F_ALLOW_NONREF 0x00000080UL |
17 | #define F_SHRINK 0x00000100 |
16 | #define F_SHRINK 0x00000100UL |
|
|
17 | #define F_MAXDEPTH 0xf8000000UL |
|
|
18 | #define S_MAXDEPTH 27 |
|
|
19 | |
|
|
20 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
21 | |
|
|
22 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
23 | // F_BLESSED? <=> { $__class__$ => } |
18 | |
24 | |
19 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
20 | #define F_DEFAULT 0 |
26 | #define F_DEFAULT (13UL << S_MAXDEPTH) |
21 | |
27 | |
22 | #define INIT_SIZE 32 // initial scalar size to be allocated |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
|
|
29 | #define INDENT_STEP 3 // spaces per indentation level |
|
|
30 | |
|
|
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
32 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
23 | |
33 | |
24 | #define SB do { |
34 | #define SB do { |
25 | #define SE } while (0) |
35 | #define SE } while (0) |
26 | |
36 | |
27 | static HV *json_stash; |
37 | static HV *json_stash; // JSON::XS:: |
|
|
38 | |
|
|
39 | ///////////////////////////////////////////////////////////////////////////// |
|
|
40 | // utility functions |
|
|
41 | |
|
|
42 | static UV * |
|
|
43 | SvJSON (SV *sv) |
|
|
44 | { |
|
|
45 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
46 | croak ("object is not of type JSON::XS"); |
|
|
47 | |
|
|
48 | return &SvUVX (SvRV (sv)); |
|
|
49 | } |
|
|
50 | |
|
|
51 | static void |
|
|
52 | shrink (SV *sv) |
|
|
53 | { |
|
|
54 | sv_utf8_downgrade (sv, 1); |
|
|
55 | if (SvLEN (sv) > SvCUR (sv) + 1) |
|
|
56 | { |
|
|
57 | #ifdef SvPV_shrink_to_cur |
|
|
58 | SvPV_shrink_to_cur (sv); |
|
|
59 | #elif defined (SvPV_renew) |
|
|
60 | SvPV_renew (sv, SvCUR (sv) + 1); |
|
|
61 | #endif |
|
|
62 | } |
|
|
63 | } |
|
|
64 | |
|
|
65 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
66 | // case of an error. |
|
|
67 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
68 | // but use the very good perl function to parse anything else. |
|
|
69 | // note that we never call this function for a ascii codepoints |
|
|
70 | static UV |
|
|
71 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
72 | { |
|
|
73 | if (s[0] > 0xdf || s[0] < 0xc2) |
|
|
74 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
75 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
76 | { |
|
|
77 | *clen = 2; |
|
|
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
79 | } |
|
|
80 | else |
|
|
81 | return (UV)-1; |
|
|
82 | } |
|
|
83 | |
|
|
84 | ///////////////////////////////////////////////////////////////////////////// |
|
|
85 | // encoder |
28 | |
86 | |
29 | // structure used for encoding JSON |
87 | // structure used for encoding JSON |
30 | typedef struct |
88 | typedef struct |
31 | { |
89 | { |
32 | char *cur; |
90 | char *cur; // SvPVX (sv) + current output position |
33 | STRLEN len; // SvLEN (sv) |
|
|
34 | char *end; // SvEND (sv) |
91 | char *end; // SvEND (sv) |
35 | SV *sv; |
92 | SV *sv; // result scalar |
36 | UV flags; |
93 | U32 flags; // F_* |
37 | int max_recurse; |
94 | U32 indent; // indentation level |
38 | int indent; |
95 | U32 maxdepth; // max. indentation/recursion level |
39 | } enc_t; |
96 | } enc_t; |
40 | |
|
|
41 | // structure used for decoding JSON |
|
|
42 | typedef struct |
|
|
43 | { |
|
|
44 | char *cur; |
|
|
45 | char *end; |
|
|
46 | const char *err; |
|
|
47 | UV flags; |
|
|
48 | } dec_t; |
|
|
49 | |
|
|
50 | static UV * |
|
|
51 | SvJSON (SV *sv) |
|
|
52 | { |
|
|
53 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
54 | croak ("object is not of type JSON::XS"); |
|
|
55 | |
|
|
56 | return &SvUVX (SvRV (sv)); |
|
|
57 | } |
|
|
58 | |
|
|
59 | ///////////////////////////////////////////////////////////////////////////// |
|
|
60 | |
97 | |
61 | static void |
98 | static void |
62 | need (enc_t *enc, STRLEN len) |
99 | need (enc_t *enc, STRLEN len) |
63 | { |
100 | { |
64 | if (enc->cur + len >= enc->end) |
101 | if (enc->cur + len >= enc->end) |
… | |
… | |
122 | STRLEN clen; |
159 | STRLEN clen; |
123 | UV uch; |
160 | UV uch; |
124 | |
161 | |
125 | if (is_utf8) |
162 | if (is_utf8) |
126 | { |
163 | { |
127 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
164 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
165 | uch = decode_utf8 (str, end - str, &clen); |
128 | if (clen == (STRLEN)-1) |
166 | if (clen == (STRLEN)-1) |
129 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
167 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
130 | } |
168 | } |
131 | else |
169 | else |
132 | { |
170 | { |
133 | uch = ch; |
171 | uch = ch; |
134 | clen = 1; |
172 | clen = 1; |
135 | } |
173 | } |
136 | |
174 | |
|
|
175 | if (uch > 0x10FFFFUL) |
|
|
176 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
177 | |
137 | if (uch < 0x80 || enc->flags & F_ASCII) |
178 | if (uch < 0x80 || enc->flags & F_ASCII) |
138 | { |
179 | { |
139 | if (uch > 0xFFFFUL) |
180 | if (uch > 0xFFFFUL) |
140 | { |
181 | { |
141 | need (enc, len += 11); |
182 | need (enc, len += 11); |
142 | sprintf (enc->cur, "\\u%04x\\u%04x", |
183 | sprintf (enc->cur, "\\u%04x\\u%04x", |
143 | (uch - 0x10000) / 0x400 + 0xD800, |
184 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
144 | (uch - 0x10000) % 0x400 + 0xDC00); |
185 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
145 | enc->cur += 12; |
186 | enc->cur += 12; |
146 | } |
187 | } |
147 | else |
188 | else |
148 | { |
189 | { |
149 | static char hexdigit [16] = "0123456789abcdef"; |
190 | static char hexdigit [16] = "0123456789abcdef"; |
… | |
… | |
167 | } |
208 | } |
168 | while (--clen); |
209 | while (--clen); |
169 | } |
210 | } |
170 | else |
211 | else |
171 | { |
212 | { |
172 | need (enc, 10); // never more than 11 bytes needed |
213 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
173 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
214 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
174 | ++str; |
215 | ++str; |
175 | } |
216 | } |
176 | } |
217 | } |
177 | } |
218 | } |
… | |
… | |
179 | |
220 | |
180 | --len; |
221 | --len; |
181 | } |
222 | } |
182 | } |
223 | } |
183 | |
224 | |
184 | #define INDENT SB \ |
225 | static void |
|
|
226 | encode_indent (enc_t *enc) |
|
|
227 | { |
185 | if (enc->flags & F_INDENT) \ |
228 | if (enc->flags & F_INDENT) |
186 | { \ |
229 | { |
187 | int i_; \ |
230 | int spaces = enc->indent * INDENT_STEP; |
188 | need (enc, enc->indent); \ |
|
|
189 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
190 | encode_ch (enc, ' '); \ |
|
|
191 | } \ |
|
|
192 | SE |
|
|
193 | |
231 | |
194 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
232 | need (enc, spaces); |
195 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
233 | memset (enc->cur, ' ', spaces); |
196 | #define COMMA SB \ |
234 | enc->cur += spaces; |
|
|
235 | } |
|
|
236 | } |
|
|
237 | |
|
|
238 | static void |
|
|
239 | encode_space (enc_t *enc) |
|
|
240 | { |
|
|
241 | need (enc, 1); |
197 | encode_ch (enc, ','); \ |
242 | encode_ch (enc, ' '); |
|
|
243 | } |
|
|
244 | |
|
|
245 | static void |
|
|
246 | encode_nl (enc_t *enc) |
|
|
247 | { |
198 | if (enc->flags & F_INDENT) \ |
248 | if (enc->flags & F_INDENT) |
199 | NL; \ |
249 | { |
|
|
250 | need (enc, 1); |
|
|
251 | encode_ch (enc, '\n'); |
|
|
252 | } |
|
|
253 | } |
|
|
254 | |
|
|
255 | static void |
|
|
256 | encode_comma (enc_t *enc) |
|
|
257 | { |
|
|
258 | encode_ch (enc, ','); |
|
|
259 | |
|
|
260 | if (enc->flags & F_INDENT) |
|
|
261 | encode_nl (enc); |
200 | else if (enc->flags & F_SPACE_AFTER) \ |
262 | else if (enc->flags & F_SPACE_AFTER) |
201 | SPACE; \ |
263 | encode_space (enc); |
202 | SE |
264 | } |
203 | |
265 | |
204 | static void encode_sv (enc_t *enc, SV *sv); |
266 | static void encode_sv (enc_t *enc, SV *sv); |
205 | |
267 | |
206 | static void |
268 | static void |
207 | encode_av (enc_t *enc, AV *av) |
269 | encode_av (enc_t *enc, AV *av) |
208 | { |
270 | { |
209 | int i, len = av_len (av); |
271 | int i, len = av_len (av); |
210 | |
272 | |
211 | encode_ch (enc, '['); NL; |
273 | encode_ch (enc, '['); encode_nl (enc); |
212 | ++enc->indent; |
274 | ++enc->indent; |
213 | |
275 | |
214 | for (i = 0; i <= len; ++i) |
276 | for (i = 0; i <= len; ++i) |
215 | { |
277 | { |
216 | INDENT; |
278 | encode_indent (enc); |
217 | encode_sv (enc, *av_fetch (av, i, 0)); |
279 | encode_sv (enc, *av_fetch (av, i, 0)); |
218 | |
280 | |
219 | if (i < len) |
281 | if (i < len) |
220 | COMMA; |
282 | encode_comma (enc); |
221 | } |
283 | } |
222 | |
284 | |
223 | NL; |
285 | encode_nl (enc); |
224 | |
286 | |
225 | --enc->indent; |
287 | --enc->indent; |
226 | INDENT; encode_ch (enc, ']'); |
288 | encode_indent (enc); encode_ch (enc, ']'); |
227 | } |
289 | } |
228 | |
290 | |
229 | static void |
291 | static void |
230 | encode_he (enc_t *enc, HE *he) |
292 | encode_he (enc_t *enc, HE *he) |
231 | { |
293 | { |
… | |
… | |
245 | else |
307 | else |
246 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
308 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
247 | |
309 | |
248 | encode_ch (enc, '"'); |
310 | encode_ch (enc, '"'); |
249 | |
311 | |
250 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
312 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
251 | encode_ch (enc, ':'); |
313 | encode_ch (enc, ':'); |
252 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
314 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
253 | encode_sv (enc, HeVAL (he)); |
315 | encode_sv (enc, HeVAL (he)); |
254 | } |
316 | } |
255 | |
317 | |
256 | // compare hash entries, used when all keys are bytestrings |
318 | // compare hash entries, used when all keys are bytestrings |
257 | static int |
319 | static int |
… | |
… | |
263 | HE *b = *(HE **)b_; |
325 | HE *b = *(HE **)b_; |
264 | |
326 | |
265 | STRLEN la = HeKLEN (a); |
327 | STRLEN la = HeKLEN (a); |
266 | STRLEN lb = HeKLEN (b); |
328 | STRLEN lb = HeKLEN (b); |
267 | |
329 | |
268 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
330 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
269 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
331 | cmp = la - lb; |
270 | |
332 | |
271 | return cmp; |
333 | return cmp; |
272 | } |
334 | } |
273 | |
335 | |
274 | // compare hash entries, used when some keys are sv's or utf-x |
336 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
281 | static void |
343 | static void |
282 | encode_hv (enc_t *enc, HV *hv) |
344 | encode_hv (enc_t *enc, HV *hv) |
283 | { |
345 | { |
284 | int count, i; |
346 | int count, i; |
285 | |
347 | |
286 | encode_ch (enc, '{'); NL; ++enc->indent; |
348 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
287 | |
349 | |
288 | if ((count = hv_iterinit (hv))) |
350 | if ((count = hv_iterinit (hv))) |
289 | { |
351 | { |
290 | // for canonical output we have to sort by keys first |
352 | // for canonical output we have to sort by keys first |
291 | // actually, this is mostly due to the stupid so-called |
353 | // actually, this is mostly due to the stupid so-called |
292 | // security workaround added somewhere in 5.8.x. |
354 | // security workaround added somewhere in 5.8.x. |
293 | // that randomises hash orderings |
355 | // that randomises hash orderings |
294 | if (enc->flags & F_CANONICAL) |
356 | if (enc->flags & F_CANONICAL) |
295 | { |
357 | { |
296 | HE *he, *hes [count]; |
358 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
297 | int fast = 1; |
359 | int fast = 1; |
298 | |
360 | |
299 | i = 0; |
361 | i = 0; |
300 | while ((he = hv_iternext (hv))) |
362 | while ((he = hv_iternext (hv))) |
301 | { |
363 | { |
… | |
… | |
308 | |
370 | |
309 | if (fast) |
371 | if (fast) |
310 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
372 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
311 | else |
373 | else |
312 | { |
374 | { |
313 | // hack to disable "use bytes" |
375 | // hack to forcefully disable "use bytes" |
314 | COP *oldcop = PL_curcop, cop; |
376 | COP cop = *PL_curcop; |
315 | cop.op_private = 0; |
377 | cop.op_private = 0; |
|
|
378 | |
|
|
379 | ENTER; |
|
|
380 | SAVETMPS; |
|
|
381 | |
|
|
382 | SAVEVPTR (PL_curcop); |
316 | PL_curcop = &cop; |
383 | PL_curcop = &cop; |
317 | |
384 | |
318 | SAVETMPS; |
|
|
319 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
385 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
386 | |
320 | FREETMPS; |
387 | FREETMPS; |
321 | |
388 | LEAVE; |
322 | PL_curcop = oldcop; |
|
|
323 | } |
389 | } |
324 | |
390 | |
325 | for (i = 0; i < count; ++i) |
391 | for (i = 0; i < count; ++i) |
326 | { |
392 | { |
327 | INDENT; |
393 | encode_indent (enc); |
328 | encode_he (enc, hes [i]); |
394 | encode_he (enc, hes [i]); |
329 | |
395 | |
330 | if (i < count - 1) |
396 | if (i < count - 1) |
331 | COMMA; |
397 | encode_comma (enc); |
332 | } |
398 | } |
333 | |
399 | |
334 | NL; |
400 | encode_nl (enc); |
335 | } |
401 | } |
336 | else |
402 | else |
337 | { |
403 | { |
338 | SV *sv; |
404 | SV *sv; |
339 | HE *he = hv_iternext (hv); |
405 | HE *he = hv_iternext (hv); |
340 | |
406 | |
341 | for (;;) |
407 | for (;;) |
342 | { |
408 | { |
343 | INDENT; |
409 | encode_indent (enc); |
344 | encode_he (enc, he); |
410 | encode_he (enc, he); |
345 | |
411 | |
346 | if (!(he = hv_iternext (hv))) |
412 | if (!(he = hv_iternext (hv))) |
347 | break; |
413 | break; |
348 | |
414 | |
349 | COMMA; |
415 | encode_comma (enc); |
350 | } |
416 | } |
351 | |
417 | |
352 | NL; |
418 | encode_nl (enc); |
353 | } |
419 | } |
354 | } |
420 | } |
355 | |
421 | |
356 | --enc->indent; INDENT; encode_ch (enc, '}'); |
422 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
357 | } |
423 | } |
358 | |
424 | |
359 | static void |
425 | static void |
360 | encode_sv (enc_t *enc, SV *sv) |
426 | encode_sv (enc_t *enc, SV *sv) |
361 | { |
427 | { |
… | |
… | |
383 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
449 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
384 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
450 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
385 | } |
451 | } |
386 | else if (SvROK (sv)) |
452 | else if (SvROK (sv)) |
387 | { |
453 | { |
388 | if (!--enc->max_recurse) |
454 | SV *rv = SvRV (sv); |
|
|
455 | |
|
|
456 | if (enc->indent >= enc->maxdepth) |
389 | croak ("data structure too deep (hit recursion limit)"); |
457 | croak ("data structure too deep (hit recursion limit)"); |
390 | |
458 | |
391 | sv = SvRV (sv); |
|
|
392 | |
|
|
393 | switch (SvTYPE (sv)) |
459 | switch (SvTYPE (rv)) |
394 | { |
460 | { |
395 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
461 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
396 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
462 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
397 | |
463 | |
398 | default: |
464 | default: |
399 | croak ("JSON can only represent references to arrays or hashes"); |
465 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
466 | SvPV_nolen (sv)); |
400 | } |
467 | } |
401 | } |
468 | } |
402 | else if (!SvOK (sv)) |
469 | else if (!SvOK (sv)) |
403 | encode_str (enc, "null", 4, 0); |
470 | encode_str (enc, "null", 4, 0); |
404 | else |
471 | else |
405 | croak ("encountered perl type that JSON cannot handle"); |
472 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
|
|
473 | SvPV_nolen (sv), SvFLAGS (sv)); |
406 | } |
474 | } |
407 | |
475 | |
408 | static SV * |
476 | static SV * |
409 | encode_json (SV *scalar, UV flags) |
477 | encode_json (SV *scalar, U32 flags) |
410 | { |
478 | { |
411 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
479 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
412 | croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); |
480 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
413 | |
481 | |
414 | enc_t enc; |
482 | enc_t enc; |
415 | enc.flags = flags; |
483 | enc.flags = flags; |
416 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
484 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
417 | enc.cur = SvPVX (enc.sv); |
485 | enc.cur = SvPVX (enc.sv); |
418 | enc.end = SvEND (enc.sv); |
486 | enc.end = SvEND (enc.sv); |
419 | enc.max_recurse = 0; |
|
|
420 | enc.indent = 0; |
487 | enc.indent = 0; |
|
|
488 | enc.maxdepth = DEC_DEPTH (flags); |
421 | |
489 | |
422 | SvPOK_only (enc.sv); |
490 | SvPOK_only (enc.sv); |
423 | encode_sv (&enc, scalar); |
491 | encode_sv (&enc, scalar); |
424 | |
492 | |
425 | if (!(flags & (F_ASCII | F_UTF8))) |
493 | if (!(flags & (F_ASCII | F_UTF8))) |
426 | SvUTF8_on (enc.sv); |
494 | SvUTF8_on (enc.sv); |
427 | |
495 | |
428 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
496 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
429 | |
497 | |
430 | #ifdef SvPV_shrink_to_cur |
|
|
431 | if (enc.flags & F_SHRINK) |
498 | if (enc.flags & F_SHRINK) |
432 | SvPV_shrink_to_cur (enc.sv); |
499 | shrink (enc.sv); |
433 | #endif |
500 | |
434 | return enc.sv; |
501 | return enc.sv; |
435 | } |
502 | } |
436 | |
503 | |
437 | ///////////////////////////////////////////////////////////////////////////// |
504 | ///////////////////////////////////////////////////////////////////////////// |
|
|
505 | // decoder |
438 | |
506 | |
439 | #define WS \ |
507 | // structure used for decoding JSON |
|
|
508 | typedef struct |
|
|
509 | { |
|
|
510 | char *cur; // current parser pointer |
|
|
511 | char *end; // end of input string |
|
|
512 | const char *err; // parse error, if != 0 |
|
|
513 | U32 flags; // F_* |
|
|
514 | U32 depth; // recursion depth |
|
|
515 | U32 maxdepth; // recursion depth limit |
|
|
516 | } dec_t; |
|
|
517 | |
|
|
518 | static void |
|
|
519 | decode_ws (dec_t *dec) |
|
|
520 | { |
440 | for (;;) \ |
521 | for (;;) |
441 | { \ |
522 | { |
442 | char ch = *dec->cur; \ |
523 | char ch = *dec->cur; |
|
|
524 | |
443 | if (ch > 0x20 \ |
525 | if (ch > 0x20 |
444 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
526 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
445 | break; \ |
527 | break; |
|
|
528 | |
446 | ++dec->cur; \ |
529 | ++dec->cur; |
447 | } |
530 | } |
|
|
531 | } |
448 | |
532 | |
449 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
533 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
534 | |
450 | #define EXPECT_CH(ch) SB \ |
535 | #define EXPECT_CH(ch) SB \ |
451 | if (*dec->cur != ch) \ |
536 | if (*dec->cur != ch) \ |
452 | ERR (# ch " expected"); \ |
537 | ERR (# ch " expected"); \ |
453 | ++dec->cur; \ |
538 | ++dec->cur; \ |
454 | SE |
539 | SE |
455 | |
540 | |
|
|
541 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
542 | #define DEC_DEC_DEPTH --dec->depth |
|
|
543 | |
456 | static SV *decode_sv (dec_t *dec); |
544 | static SV *decode_sv (dec_t *dec); |
457 | |
545 | |
458 | static signed char decode_hexdigit[256]; |
546 | static signed char decode_hexdigit[256]; |
459 | |
547 | |
460 | static UV |
548 | static UV |
461 | decode_4hex (dec_t *dec) |
549 | decode_4hex (dec_t *dec) |
462 | { |
550 | { |
463 | signed char d1, d2, d3, d4; |
551 | signed char d1, d2, d3, d4; |
|
|
552 | unsigned char *cur = (unsigned char *)dec->cur; |
464 | |
553 | |
465 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
466 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
554 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
467 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
|
|
468 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
555 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
469 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
470 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
556 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
471 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
472 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
557 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
473 | |
558 | |
474 | dec->cur += 4; |
559 | dec->cur += 4; |
475 | |
560 | |
476 | return ((UV)d1) << 12 |
561 | return ((UV)d1) << 12 |
477 | | ((UV)d2) << 8 |
562 | | ((UV)d2) << 8 |
… | |
… | |
480 | |
565 | |
481 | fail: |
566 | fail: |
482 | return (UV)-1; |
567 | return (UV)-1; |
483 | } |
568 | } |
484 | |
569 | |
485 | #define APPEND_GROW(n) SB \ |
|
|
486 | if (cur + (n) >= end) \ |
|
|
487 | { \ |
|
|
488 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
489 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
490 | cur = SvPVX (sv) + ofs; \ |
|
|
491 | end = SvEND (sv); \ |
|
|
492 | } \ |
|
|
493 | SE |
|
|
494 | |
|
|
495 | #define APPEND_CH(ch) SB \ |
|
|
496 | APPEND_GROW (1); \ |
|
|
497 | *cur++ = (ch); \ |
|
|
498 | SE |
|
|
499 | |
|
|
500 | static SV * |
570 | static SV * |
501 | decode_str (dec_t *dec) |
571 | decode_str (dec_t *dec) |
502 | { |
572 | { |
503 | SV *sv = NEWSV (0,2); |
573 | SV *sv = 0; |
504 | int utf8 = 0; |
574 | int utf8 = 0; |
505 | char *cur = SvPVX (sv); |
|
|
506 | char *end = SvEND (sv); |
|
|
507 | |
575 | |
508 | for (;;) |
576 | do |
509 | { |
577 | { |
510 | unsigned char ch = *(unsigned char *)dec->cur; |
578 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
|
|
579 | char *cur = buf; |
511 | |
580 | |
512 | if (ch == '"') |
581 | do |
513 | break; |
|
|
514 | else if (ch == '\\') |
|
|
515 | { |
582 | { |
516 | switch (*++dec->cur) |
583 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
584 | |
|
|
585 | if (ch == '"') |
517 | { |
586 | { |
518 | case '\\': |
587 | --dec->cur; |
519 | case '/': |
588 | break; |
520 | case '"': APPEND_CH (*dec->cur++); break; |
589 | } |
521 | |
590 | else if (ch == '\\') |
522 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
591 | { |
523 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
592 | switch (*dec->cur) |
524 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
525 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
526 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
527 | |
|
|
528 | case 'u': |
|
|
529 | { |
593 | { |
530 | UV lo, hi; |
594 | case '\\': |
531 | ++dec->cur; |
595 | case '/': |
|
|
596 | case '"': *cur++ = *dec->cur++; break; |
532 | |
597 | |
533 | hi = decode_4hex (dec); |
598 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
534 | if (hi == (UV)-1) |
599 | case 't': ++dec->cur; *cur++ = '\011'; break; |
535 | goto fail; |
600 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
|
|
601 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
|
|
602 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
536 | |
603 | |
537 | // possibly a surrogate pair |
604 | case 'u': |
538 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
539 | { |
605 | { |
540 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
606 | UV lo, hi; |
541 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
542 | |
|
|
543 | dec->cur += 2; |
607 | ++dec->cur; |
544 | |
608 | |
545 | lo = decode_4hex (dec); |
609 | hi = decode_4hex (dec); |
546 | if (lo == (UV)-1) |
610 | if (hi == (UV)-1) |
547 | goto fail; |
611 | goto fail; |
548 | |
612 | |
|
|
613 | // possibly a surrogate pair |
|
|
614 | if (hi >= 0xd800) |
|
|
615 | if (hi < 0xdc00) |
|
|
616 | { |
|
|
617 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
|
|
618 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
619 | |
|
|
620 | dec->cur += 2; |
|
|
621 | |
|
|
622 | lo = decode_4hex (dec); |
|
|
623 | if (lo == (UV)-1) |
|
|
624 | goto fail; |
|
|
625 | |
549 | if (lo < 0xdc00 || lo >= 0xe000) |
626 | if (lo < 0xdc00 || lo >= 0xe000) |
550 | ERR ("surrogate pair expected"); |
627 | ERR ("surrogate pair expected"); |
551 | |
628 | |
552 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
629 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
630 | } |
|
|
631 | else if (hi < 0xe000) |
|
|
632 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
633 | |
|
|
634 | if (hi >= 0x80) |
|
|
635 | { |
|
|
636 | utf8 = 1; |
|
|
637 | |
|
|
638 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
639 | } |
|
|
640 | else |
|
|
641 | *cur++ = hi; |
553 | } |
642 | } |
554 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
555 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
556 | |
|
|
557 | if (hi >= 0x80) |
|
|
558 | { |
643 | break; |
559 | utf8 = 1; |
|
|
560 | |
644 | |
561 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
562 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
563 | } |
|
|
564 | else |
645 | default: |
565 | APPEND_CH (hi); |
646 | --dec->cur; |
|
|
647 | ERR ("illegal backslash escape sequence in string"); |
566 | } |
648 | } |
567 | break; |
|
|
568 | |
|
|
569 | default: |
|
|
570 | --dec->cur; |
|
|
571 | ERR ("illegal backslash escape sequence in string"); |
|
|
572 | } |
649 | } |
|
|
650 | else if (ch >= 0x20 && ch <= 0x7f) |
|
|
651 | *cur++ = ch; |
|
|
652 | else if (ch >= 0x80) |
|
|
653 | { |
|
|
654 | --dec->cur; |
|
|
655 | |
|
|
656 | STRLEN clen; |
|
|
657 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
|
|
658 | if (clen == (STRLEN)-1) |
|
|
659 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
660 | |
|
|
661 | do |
|
|
662 | { |
|
|
663 | *cur++ = *dec->cur++; |
|
|
664 | } |
|
|
665 | while (--clen); |
|
|
666 | |
|
|
667 | utf8 = 1; |
|
|
668 | } |
|
|
669 | else if (!ch) |
|
|
670 | ERR ("unexpected end of string while parsing json string"); |
|
|
671 | else |
|
|
672 | ERR ("invalid character encountered"); |
|
|
673 | |
573 | } |
674 | } |
574 | else if (ch >= 0x20 && ch <= 0x7f) |
675 | while (cur < buf + SHORT_STRING_LEN); |
575 | APPEND_CH (*dec->cur++); |
676 | |
576 | else if (ch >= 0x80) |
677 | STRLEN len = cur - buf; |
|
|
678 | |
|
|
679 | if (sv) |
577 | { |
680 | { |
578 | STRLEN clen; |
681 | SvGROW (sv, SvCUR (sv) + len + 1); |
579 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
682 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
580 | if (clen == (STRLEN)-1) |
683 | SvCUR_set (sv, SvCUR (sv) + len); |
581 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
|
|
582 | |
|
|
583 | APPEND_GROW (clen); |
|
|
584 | do |
|
|
585 | { |
|
|
586 | *cur++ = *dec->cur++; |
|
|
587 | } |
|
|
588 | while (--clen); |
|
|
589 | |
|
|
590 | utf8 = 1; |
|
|
591 | } |
684 | } |
592 | else if (dec->cur == dec->end) |
|
|
593 | ERR ("unexpected end of string while parsing json string"); |
|
|
594 | else |
685 | else |
595 | ERR ("invalid character encountered"); |
686 | sv = newSVpvn (buf, len); |
596 | } |
687 | } |
|
|
688 | while (*dec->cur != '"'); |
597 | |
689 | |
598 | ++dec->cur; |
690 | ++dec->cur; |
599 | |
691 | |
600 | SvCUR_set (sv, cur - SvPVX (sv)); |
692 | if (sv) |
601 | |
693 | { |
602 | SvPOK_only (sv); |
694 | SvPOK_only (sv); |
603 | *SvEND (sv) = 0; |
695 | *SvEND (sv) = 0; |
604 | |
696 | |
605 | if (utf8) |
697 | if (utf8) |
606 | SvUTF8_on (sv); |
698 | SvUTF8_on (sv); |
607 | |
699 | } |
608 | #ifdef SvPV_shrink_to_cur |
700 | else |
609 | if (dec->flags & F_SHRINK) |
701 | sv = newSVpvn ("", 0); |
610 | SvPV_shrink_to_cur (sv); |
|
|
611 | #endif |
|
|
612 | |
702 | |
613 | return sv; |
703 | return sv; |
614 | |
704 | |
615 | fail: |
705 | fail: |
616 | SvREFCNT_dec (sv); |
|
|
617 | return 0; |
706 | return 0; |
618 | } |
707 | } |
619 | |
708 | |
620 | static SV * |
709 | static SV * |
621 | decode_num (dec_t *dec) |
710 | decode_num (dec_t *dec) |
… | |
… | |
702 | static SV * |
791 | static SV * |
703 | decode_av (dec_t *dec) |
792 | decode_av (dec_t *dec) |
704 | { |
793 | { |
705 | AV *av = newAV (); |
794 | AV *av = newAV (); |
706 | |
795 | |
707 | WS; |
796 | DEC_INC_DEPTH; |
|
|
797 | decode_ws (dec); |
|
|
798 | |
708 | if (*dec->cur == ']') |
799 | if (*dec->cur == ']') |
709 | ++dec->cur; |
800 | ++dec->cur; |
710 | else |
801 | else |
711 | for (;;) |
802 | for (;;) |
712 | { |
803 | { |
… | |
… | |
716 | if (!value) |
807 | if (!value) |
717 | goto fail; |
808 | goto fail; |
718 | |
809 | |
719 | av_push (av, value); |
810 | av_push (av, value); |
720 | |
811 | |
721 | WS; |
812 | decode_ws (dec); |
722 | |
813 | |
723 | if (*dec->cur == ']') |
814 | if (*dec->cur == ']') |
724 | { |
815 | { |
725 | ++dec->cur; |
816 | ++dec->cur; |
726 | break; |
817 | break; |
… | |
… | |
730 | ERR (", or ] expected while parsing array"); |
821 | ERR (", or ] expected while parsing array"); |
731 | |
822 | |
732 | ++dec->cur; |
823 | ++dec->cur; |
733 | } |
824 | } |
734 | |
825 | |
|
|
826 | DEC_DEC_DEPTH; |
735 | return newRV_noinc ((SV *)av); |
827 | return newRV_noinc ((SV *)av); |
736 | |
828 | |
737 | fail: |
829 | fail: |
738 | SvREFCNT_dec (av); |
830 | SvREFCNT_dec (av); |
|
|
831 | DEC_DEC_DEPTH; |
739 | return 0; |
832 | return 0; |
740 | } |
833 | } |
741 | |
834 | |
742 | static SV * |
835 | static SV * |
743 | decode_hv (dec_t *dec) |
836 | decode_hv (dec_t *dec) |
744 | { |
837 | { |
745 | HV *hv = newHV (); |
838 | HV *hv = newHV (); |
746 | |
839 | |
747 | WS; |
840 | DEC_INC_DEPTH; |
|
|
841 | decode_ws (dec); |
|
|
842 | |
748 | if (*dec->cur == '}') |
843 | if (*dec->cur == '}') |
749 | ++dec->cur; |
844 | ++dec->cur; |
750 | else |
845 | else |
751 | for (;;) |
846 | for (;;) |
752 | { |
847 | { |
753 | SV *key, *value; |
848 | SV *key, *value; |
754 | |
849 | |
755 | WS; EXPECT_CH ('"'); |
850 | decode_ws (dec); EXPECT_CH ('"'); |
756 | |
851 | |
757 | key = decode_str (dec); |
852 | key = decode_str (dec); |
758 | if (!key) |
853 | if (!key) |
759 | goto fail; |
854 | goto fail; |
760 | |
855 | |
761 | WS; EXPECT_CH (':'); |
856 | decode_ws (dec); EXPECT_CH (':'); |
762 | |
857 | |
763 | value = decode_sv (dec); |
858 | value = decode_sv (dec); |
764 | if (!value) |
859 | if (!value) |
765 | { |
860 | { |
766 | SvREFCNT_dec (key); |
861 | SvREFCNT_dec (key); |
767 | goto fail; |
862 | goto fail; |
768 | } |
863 | } |
769 | |
864 | |
770 | //TODO: optimise |
|
|
771 | hv_store_ent (hv, key, value, 0); |
865 | hv_store_ent (hv, key, value, 0); |
|
|
866 | SvREFCNT_dec (key); |
772 | |
867 | |
773 | WS; |
868 | decode_ws (dec); |
774 | |
869 | |
775 | if (*dec->cur == '}') |
870 | if (*dec->cur == '}') |
776 | { |
871 | { |
777 | ++dec->cur; |
872 | ++dec->cur; |
778 | break; |
873 | break; |
… | |
… | |
782 | ERR (", or } expected while parsing object/hash"); |
877 | ERR (", or } expected while parsing object/hash"); |
783 | |
878 | |
784 | ++dec->cur; |
879 | ++dec->cur; |
785 | } |
880 | } |
786 | |
881 | |
|
|
882 | DEC_DEC_DEPTH; |
787 | return newRV_noinc ((SV *)hv); |
883 | return newRV_noinc ((SV *)hv); |
788 | |
884 | |
789 | fail: |
885 | fail: |
790 | SvREFCNT_dec (hv); |
886 | SvREFCNT_dec (hv); |
|
|
887 | DEC_DEC_DEPTH; |
791 | return 0; |
888 | return 0; |
792 | } |
889 | } |
793 | |
890 | |
794 | static SV * |
891 | static SV * |
795 | decode_sv (dec_t *dec) |
892 | decode_sv (dec_t *dec) |
796 | { |
893 | { |
797 | WS; |
894 | decode_ws (dec); |
798 | switch (*dec->cur) |
895 | switch (*dec->cur) |
799 | { |
896 | { |
800 | case '"': ++dec->cur; return decode_str (dec); |
897 | case '"': ++dec->cur; return decode_str (dec); |
801 | case '[': ++dec->cur; return decode_av (dec); |
898 | case '[': ++dec->cur; return decode_av (dec); |
802 | case '{': ++dec->cur; return decode_hv (dec); |
899 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
838 | ERR ("'null' expected"); |
935 | ERR ("'null' expected"); |
839 | |
936 | |
840 | break; |
937 | break; |
841 | |
938 | |
842 | default: |
939 | default: |
843 | ERR ("malformed json string"); |
940 | ERR ("malformed json string, neither array, object, number, string or atom"); |
844 | break; |
941 | break; |
845 | } |
942 | } |
846 | |
943 | |
847 | fail: |
944 | fail: |
848 | return 0; |
945 | return 0; |
849 | } |
946 | } |
850 | |
947 | |
851 | static SV * |
948 | static SV * |
852 | decode_json (SV *string, UV flags) |
949 | decode_json (SV *string, U32 flags) |
853 | { |
950 | { |
854 | SV *sv; |
951 | SV *sv; |
855 | |
952 | |
856 | if (flags & F_UTF8) |
953 | if (flags & F_UTF8) |
857 | sv_utf8_downgrade (string, 0); |
954 | sv_utf8_downgrade (string, 0); |
… | |
… | |
859 | sv_utf8_upgrade (string); |
956 | sv_utf8_upgrade (string); |
860 | |
957 | |
861 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
958 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
862 | |
959 | |
863 | dec_t dec; |
960 | dec_t dec; |
864 | dec.flags = flags; |
961 | dec.flags = flags; |
865 | dec.cur = SvPVX (string); |
962 | dec.cur = SvPVX (string); |
866 | dec.end = SvEND (string); |
963 | dec.end = SvEND (string); |
867 | dec.err = 0; |
964 | dec.err = 0; |
|
|
965 | dec.depth = 0; |
|
|
966 | dec.maxdepth = DEC_DEPTH (dec.flags); |
868 | |
967 | |
|
|
968 | *SvEND (string) = 0; // this should basically be a nop, too |
869 | sv = decode_sv (&dec); |
969 | sv = decode_sv (&dec); |
870 | |
970 | |
871 | if (!sv) |
971 | if (!sv) |
872 | { |
972 | { |
|
|
973 | IV offset = dec.flags & F_UTF8 |
|
|
974 | ? dec.cur - SvPVX (string) |
873 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
975 | : utf8_distance (dec.cur, SvPVX (string)); |
874 | SV *uni = sv_newmortal (); |
976 | SV *uni = sv_newmortal (); |
|
|
977 | |
875 | // horrible hack to silence warning inside pv_uni_display |
978 | // horrible hack to silence warning inside pv_uni_display |
876 | COP cop; |
979 | COP cop = *PL_curcop; |
877 | memset (&cop, 0, sizeof (cop)); |
|
|
878 | cop.cop_warnings = pWARN_NONE; |
980 | cop.cop_warnings = pWARN_NONE; |
|
|
981 | ENTER; |
879 | SAVEVPTR (PL_curcop); |
982 | SAVEVPTR (PL_curcop); |
880 | PL_curcop = &cop; |
983 | PL_curcop = &cop; |
881 | |
|
|
882 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
984 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
985 | LEAVE; |
|
|
986 | |
883 | croak ("%s, at character offset %d (%s)", |
987 | croak ("%s, at character offset %d (%s)", |
884 | dec.err, |
988 | dec.err, |
885 | (int)offset, |
989 | (int)offset, |
886 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
990 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
887 | } |
991 | } |
888 | |
992 | |
889 | sv = sv_2mortal (sv); |
993 | sv = sv_2mortal (sv); |
890 | |
994 | |
891 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
995 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
892 | croak ("JSON object or array expected (but number, string, true, false or null found, use allow_nonref to allow this)"); |
996 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
893 | |
997 | |
894 | return sv; |
998 | return sv; |
895 | } |
999 | } |
896 | |
1000 | |
|
|
1001 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1002 | // XS interface functions |
|
|
1003 | |
897 | MODULE = JSON::XS PACKAGE = JSON::XS |
1004 | MODULE = JSON::XS PACKAGE = JSON::XS |
898 | |
1005 | |
899 | BOOT: |
1006 | BOOT: |
900 | { |
1007 | { |
901 | int i; |
1008 | int i; |
902 | |
1009 | |
903 | memset (decode_hexdigit, 0xff, 256); |
1010 | memset (decode_hexdigit, 0xff, 256); |
|
|
1011 | |
904 | for (i = 10; i--; ) |
1012 | for (i = 0; i < 256; ++i) |
905 | decode_hexdigit ['0' + i] = i; |
1013 | decode_hexdigit [i] = |
906 | |
1014 | i >= '0' && i <= '9' ? i - '0' |
907 | for (i = 7; i--; ) |
1015 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
908 | { |
1016 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
909 | decode_hexdigit ['a' + i] = 10 + i; |
1017 | : -1; |
910 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
911 | } |
|
|
912 | |
1018 | |
913 | json_stash = gv_stashpv ("JSON::XS", 1); |
1019 | json_stash = gv_stashpv ("JSON::XS", 1); |
914 | } |
1020 | } |
915 | |
1021 | |
916 | PROTOTYPES: DISABLE |
1022 | PROTOTYPES: DISABLE |
… | |
… | |
927 | utf8 = F_UTF8 |
1033 | utf8 = F_UTF8 |
928 | indent = F_INDENT |
1034 | indent = F_INDENT |
929 | canonical = F_CANONICAL |
1035 | canonical = F_CANONICAL |
930 | space_before = F_SPACE_BEFORE |
1036 | space_before = F_SPACE_BEFORE |
931 | space_after = F_SPACE_AFTER |
1037 | space_after = F_SPACE_AFTER |
932 | json_rpc = F_JSON_RPC |
|
|
933 | pretty = F_PRETTY |
1038 | pretty = F_PRETTY |
934 | allow_nonref = F_ALLOW_NONREF |
1039 | allow_nonref = F_ALLOW_NONREF |
935 | shrink = F_SHRINK |
1040 | shrink = F_SHRINK |
936 | CODE: |
1041 | CODE: |
937 | { |
1042 | { |
… | |
… | |
944 | RETVAL = newSVsv (self); |
1049 | RETVAL = newSVsv (self); |
945 | } |
1050 | } |
946 | OUTPUT: |
1051 | OUTPUT: |
947 | RETVAL |
1052 | RETVAL |
948 | |
1053 | |
|
|
1054 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
|
|
1055 | CODE: |
|
|
1056 | { |
|
|
1057 | UV *uv = SvJSON (self); |
|
|
1058 | UV log2 = 0; |
|
|
1059 | |
|
|
1060 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1061 | |
|
|
1062 | while ((1UL << log2) < max_depth) |
|
|
1063 | ++log2; |
|
|
1064 | |
|
|
1065 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1066 | |
|
|
1067 | RETVAL = newSVsv (self); |
|
|
1068 | } |
|
|
1069 | OUTPUT: |
|
|
1070 | RETVAL |
|
|
1071 | |
949 | void encode (SV *self, SV *scalar) |
1072 | void encode (SV *self, SV *scalar) |
950 | PPCODE: |
1073 | PPCODE: |
951 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1074 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
952 | |
1075 | |
953 | void decode (SV *self, SV *jsonstr) |
1076 | void decode (SV *self, SV *jsonstr) |
… | |
… | |
955 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1078 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
956 | |
1079 | |
957 | PROTOTYPES: ENABLE |
1080 | PROTOTYPES: ENABLE |
958 | |
1081 | |
959 | void to_json (SV *scalar) |
1082 | void to_json (SV *scalar) |
|
|
1083 | ALIAS: |
|
|
1084 | objToJson = 0 |
960 | PPCODE: |
1085 | PPCODE: |
961 | XPUSHs (encode_json (scalar, F_UTF8)); |
1086 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
962 | |
1087 | |
963 | void from_json (SV *jsonstr) |
1088 | void from_json (SV *jsonstr) |
|
|
1089 | ALIAS: |
|
|
1090 | jsonToObj = 0 |
964 | PPCODE: |
1091 | PPCODE: |
965 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1092 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
966 | |
1093 | |