1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
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 | #include "stdio.h" |
8 | #include <stdio.h> |
|
|
9 | #include <float.h> |
9 | |
10 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
13 | #endif |
13 | |
14 | |
… | |
… | |
25 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_SHRINK 0x00000200UL |
29 | #define F_SHRINK 0x00000200UL |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
31 | #define F_CONV_BLESSED 0x00000800UL |
|
|
32 | #define F_RELAXED 0x00001000UL |
|
|
33 | |
31 | #define F_MAXDEPTH 0xf8000000UL |
34 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define S_MAXDEPTH 27 |
35 | #define S_MAXDEPTH 27 |
33 | #define F_MAXSIZE 0x01f00000UL |
36 | #define F_MAXSIZE 0x01f00000UL |
34 | #define S_MAXSIZE 20 |
37 | #define S_MAXSIZE 20 |
|
|
38 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
35 | |
39 | |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
40 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
41 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
38 | |
42 | |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
43 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
… | |
… | |
56 | #endif |
60 | #endif |
57 | |
61 | |
58 | #define expect_false(expr) expect ((expr) != 0, 0) |
62 | #define expect_false(expr) expect ((expr) != 0, 0) |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
63 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | |
64 | |
|
|
65 | #ifdef USE_ITHREADS |
|
|
66 | # define JSON_SLOW 1 |
|
|
67 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
68 | #else |
|
|
69 | # define JSON_SLOW 0 |
|
|
70 | # define JSON_STASH json_stash |
|
|
71 | #endif |
|
|
72 | |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
73 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
62 | static SV *json_true, *json_false; |
74 | static SV *json_true, *json_false; |
63 | |
75 | |
|
|
76 | typedef struct { |
|
|
77 | U32 flags; |
|
|
78 | SV *cb_object; |
|
|
79 | HV *cb_sk_object; |
|
|
80 | } JSON; |
|
|
81 | |
64 | ///////////////////////////////////////////////////////////////////////////// |
82 | ///////////////////////////////////////////////////////////////////////////// |
65 | // utility functions |
83 | // utility functions |
66 | |
84 | |
67 | static UV * |
85 | inline void |
68 | SvJSON (SV *sv) |
|
|
69 | { |
|
|
70 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
71 | croak ("object is not of type JSON::XS"); |
|
|
72 | |
|
|
73 | return &SvUVX (SvRV (sv)); |
|
|
74 | } |
|
|
75 | |
|
|
76 | static void |
|
|
77 | shrink (SV *sv) |
86 | shrink (SV *sv) |
78 | { |
87 | { |
79 | sv_utf8_downgrade (sv, 1); |
88 | sv_utf8_downgrade (sv, 1); |
80 | if (SvLEN (sv) > SvCUR (sv) + 1) |
89 | if (SvLEN (sv) > SvCUR (sv) + 1) |
81 | { |
90 | { |
… | |
… | |
116 | typedef struct |
125 | typedef struct |
117 | { |
126 | { |
118 | char *cur; // SvPVX (sv) + current output position |
127 | char *cur; // SvPVX (sv) + current output position |
119 | char *end; // SvEND (sv) |
128 | char *end; // SvEND (sv) |
120 | SV *sv; // result scalar |
129 | SV *sv; // result scalar |
121 | U32 flags; // F_* |
130 | JSON json; |
122 | U32 indent; // indentation level |
131 | U32 indent; // indentation level |
123 | U32 maxdepth; // max. indentation/recursion level |
132 | U32 maxdepth; // max. indentation/recursion level |
124 | } enc_t; |
133 | } enc_t; |
125 | |
134 | |
126 | inline void |
135 | inline void |
… | |
… | |
200 | } |
209 | } |
201 | |
210 | |
202 | if (uch > 0x10FFFFUL) |
211 | if (uch > 0x10FFFFUL) |
203 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
212 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
204 | |
213 | |
205 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
214 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
206 | { |
215 | { |
207 | if (uch > 0xFFFFUL) |
216 | if (uch > 0xFFFFUL) |
208 | { |
217 | { |
209 | need (enc, len += 11); |
218 | need (enc, len += 11); |
210 | sprintf (enc->cur, "\\u%04x\\u%04x", |
219 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
224 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
233 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
225 | } |
234 | } |
226 | |
235 | |
227 | str += clen; |
236 | str += clen; |
228 | } |
237 | } |
229 | else if (enc->flags & F_LATIN1) |
238 | else if (enc->json.flags & F_LATIN1) |
230 | { |
239 | { |
231 | *enc->cur++ = uch; |
240 | *enc->cur++ = uch; |
232 | str += clen; |
241 | str += clen; |
233 | } |
242 | } |
234 | else if (is_utf8) |
243 | else if (is_utf8) |
… | |
… | |
255 | } |
264 | } |
256 | |
265 | |
257 | inline void |
266 | inline void |
258 | encode_indent (enc_t *enc) |
267 | encode_indent (enc_t *enc) |
259 | { |
268 | { |
260 | if (enc->flags & F_INDENT) |
269 | if (enc->json.flags & F_INDENT) |
261 | { |
270 | { |
262 | int spaces = enc->indent * INDENT_STEP; |
271 | int spaces = enc->indent * INDENT_STEP; |
263 | |
272 | |
264 | need (enc, spaces); |
273 | need (enc, spaces); |
265 | memset (enc->cur, ' ', spaces); |
274 | memset (enc->cur, ' ', spaces); |
… | |
… | |
275 | } |
284 | } |
276 | |
285 | |
277 | inline void |
286 | inline void |
278 | encode_nl (enc_t *enc) |
287 | encode_nl (enc_t *enc) |
279 | { |
288 | { |
280 | if (enc->flags & F_INDENT) |
289 | if (enc->json.flags & F_INDENT) |
281 | { |
290 | { |
282 | need (enc, 1); |
291 | need (enc, 1); |
283 | encode_ch (enc, '\n'); |
292 | encode_ch (enc, '\n'); |
284 | } |
293 | } |
285 | } |
294 | } |
… | |
… | |
287 | inline void |
296 | inline void |
288 | encode_comma (enc_t *enc) |
297 | encode_comma (enc_t *enc) |
289 | { |
298 | { |
290 | encode_ch (enc, ','); |
299 | encode_ch (enc, ','); |
291 | |
300 | |
292 | if (enc->flags & F_INDENT) |
301 | if (enc->json.flags & F_INDENT) |
293 | encode_nl (enc); |
302 | encode_nl (enc); |
294 | else if (enc->flags & F_SPACE_AFTER) |
303 | else if (enc->json.flags & F_SPACE_AFTER) |
295 | encode_space (enc); |
304 | encode_space (enc); |
296 | } |
305 | } |
297 | |
306 | |
298 | static void encode_sv (enc_t *enc, SV *sv); |
307 | static void encode_sv (enc_t *enc, SV *sv); |
299 | |
308 | |
… | |
… | |
308 | encode_ch (enc, '['); encode_nl (enc); |
317 | encode_ch (enc, '['); encode_nl (enc); |
309 | ++enc->indent; |
318 | ++enc->indent; |
310 | |
319 | |
311 | for (i = 0; i <= len; ++i) |
320 | for (i = 0; i <= len; ++i) |
312 | { |
321 | { |
|
|
322 | SV **svp = av_fetch (av, i, 0); |
|
|
323 | |
313 | encode_indent (enc); |
324 | encode_indent (enc); |
314 | encode_sv (enc, *av_fetch (av, i, 0)); |
325 | |
|
|
326 | if (svp) |
|
|
327 | encode_sv (enc, *svp); |
|
|
328 | else |
|
|
329 | encode_str (enc, "null", 4, 0); |
315 | |
330 | |
316 | if (i < len) |
331 | if (i < len) |
317 | encode_comma (enc); |
332 | encode_comma (enc); |
318 | } |
333 | } |
319 | |
334 | |
… | |
… | |
322 | --enc->indent; |
337 | --enc->indent; |
323 | encode_indent (enc); encode_ch (enc, ']'); |
338 | encode_indent (enc); encode_ch (enc, ']'); |
324 | } |
339 | } |
325 | |
340 | |
326 | static void |
341 | static void |
327 | encode_he (enc_t *enc, HE *he) |
342 | encode_hk (enc_t *enc, HE *he) |
328 | { |
343 | { |
329 | encode_ch (enc, '"'); |
344 | encode_ch (enc, '"'); |
330 | |
345 | |
331 | if (HeKLEN (he) == HEf_SVKEY) |
346 | if (HeKLEN (he) == HEf_SVKEY) |
332 | { |
347 | { |
… | |
… | |
342 | else |
357 | else |
343 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
358 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
344 | |
359 | |
345 | encode_ch (enc, '"'); |
360 | encode_ch (enc, '"'); |
346 | |
361 | |
347 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
362 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
348 | encode_ch (enc, ':'); |
363 | encode_ch (enc, ':'); |
349 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
364 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
350 | encode_sv (enc, HeVAL (he)); |
|
|
351 | } |
365 | } |
352 | |
366 | |
353 | // compare hash entries, used when all keys are bytestrings |
367 | // compare hash entries, used when all keys are bytestrings |
354 | static int |
368 | static int |
355 | he_cmp_fast (const void *a_, const void *b_) |
369 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
360 | HE *b = *(HE **)b_; |
374 | HE *b = *(HE **)b_; |
361 | |
375 | |
362 | STRLEN la = HeKLEN (a); |
376 | STRLEN la = HeKLEN (a); |
363 | STRLEN lb = HeKLEN (b); |
377 | STRLEN lb = HeKLEN (b); |
364 | |
378 | |
365 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
379 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
366 | cmp = la - lb; |
380 | cmp = lb - la; |
367 | |
381 | |
368 | return cmp; |
382 | return cmp; |
369 | } |
383 | } |
370 | |
384 | |
371 | // compare hash entries, used when some keys are sv's or utf-x |
385 | // compare hash entries, used when some keys are sv's or utf-x |
372 | static int |
386 | static int |
373 | he_cmp_slow (const void *a, const void *b) |
387 | he_cmp_slow (const void *a, const void *b) |
374 | { |
388 | { |
375 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
389 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
376 | } |
390 | } |
377 | |
391 | |
378 | static void |
392 | static void |
379 | encode_hv (enc_t *enc, HV *hv) |
393 | encode_hv (enc_t *enc, HV *hv) |
380 | { |
394 | { |
|
|
395 | HE *he; |
381 | int count, i; |
396 | int count; |
382 | |
397 | |
383 | if (enc->indent >= enc->maxdepth) |
398 | if (enc->indent >= enc->maxdepth) |
384 | croak ("data structure too deep (hit recursion limit)"); |
399 | croak ("data structure too deep (hit recursion limit)"); |
385 | |
400 | |
386 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
401 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
387 | |
402 | |
388 | if ((count = hv_iterinit (hv))) |
|
|
389 | { |
|
|
390 | // for canonical output we have to sort by keys first |
403 | // for canonical output we have to sort by keys first |
391 | // actually, this is mostly due to the stupid so-called |
404 | // actually, this is mostly due to the stupid so-called |
392 | // security workaround added somewhere in 5.8.x. |
405 | // security workaround added somewhere in 5.8.x. |
393 | // that randomises hash orderings |
406 | // that randomises hash orderings |
394 | if (enc->flags & F_CANONICAL) |
407 | if (enc->json.flags & F_CANONICAL) |
|
|
408 | { |
|
|
409 | int count = hv_iterinit (hv); |
|
|
410 | |
|
|
411 | if (SvMAGICAL (hv)) |
395 | { |
412 | { |
|
|
413 | // need to count by iterating. could improve by dynamically building the vector below |
|
|
414 | // but I don't care for the speed of this special case. |
|
|
415 | // note also that we will run into undefined behaviour when the two iterations |
|
|
416 | // do not result in the same count, something I might care for in some later release. |
|
|
417 | |
|
|
418 | count = 0; |
|
|
419 | while (hv_iternext (hv)) |
|
|
420 | ++count; |
|
|
421 | |
|
|
422 | hv_iterinit (hv); |
|
|
423 | } |
|
|
424 | |
|
|
425 | if (count) |
|
|
426 | { |
396 | int fast = 1; |
427 | int i, fast = 1; |
397 | HE *he; |
|
|
398 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
428 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
399 | HE **hes = _alloca (count * sizeof (HE)); |
429 | HE **hes = _alloca (count * sizeof (HE)); |
400 | #else |
430 | #else |
401 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
431 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
402 | #endif |
432 | #endif |
… | |
… | |
429 | |
459 | |
430 | FREETMPS; |
460 | FREETMPS; |
431 | LEAVE; |
461 | LEAVE; |
432 | } |
462 | } |
433 | |
463 | |
434 | for (i = 0; i < count; ++i) |
464 | while (count--) |
435 | { |
465 | { |
436 | encode_indent (enc); |
466 | encode_indent (enc); |
|
|
467 | he = hes [count]; |
437 | encode_he (enc, hes [i]); |
468 | encode_hk (enc, he); |
|
|
469 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
438 | |
470 | |
439 | if (i < count - 1) |
471 | if (count) |
440 | encode_comma (enc); |
472 | encode_comma (enc); |
441 | } |
473 | } |
442 | |
|
|
443 | encode_nl (enc); |
|
|
444 | } |
474 | } |
|
|
475 | } |
445 | else |
476 | else |
446 | { |
477 | { |
|
|
478 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
447 | HE *he = hv_iternext (hv); |
479 | if ((he = hv_iternext (hv))) |
448 | |
|
|
449 | for (;;) |
480 | for (;;) |
450 | { |
481 | { |
451 | encode_indent (enc); |
482 | encode_indent (enc); |
452 | encode_he (enc, he); |
483 | encode_hk (enc, he); |
|
|
484 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
453 | |
485 | |
454 | if (!(he = hv_iternext (hv))) |
486 | if (!(he = hv_iternext (hv))) |
455 | break; |
487 | break; |
456 | |
488 | |
457 | encode_comma (enc); |
489 | encode_comma (enc); |
458 | } |
490 | } |
|
|
491 | } |
459 | |
492 | |
460 | encode_nl (enc); |
493 | encode_nl (enc); |
461 | } |
|
|
462 | } |
|
|
463 | |
494 | |
464 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
495 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
465 | } |
496 | } |
466 | |
497 | |
467 | // encode objects, arrays and special \0=false and \1=true values. |
498 | // encode objects, arrays and special \0=false and \1=true values. |
… | |
… | |
473 | SvGETMAGIC (sv); |
504 | SvGETMAGIC (sv); |
474 | svt = SvTYPE (sv); |
505 | svt = SvTYPE (sv); |
475 | |
506 | |
476 | if (expect_false (SvOBJECT (sv))) |
507 | if (expect_false (SvOBJECT (sv))) |
477 | { |
508 | { |
|
|
509 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
510 | ? json_boolean_stash |
|
|
511 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
512 | |
478 | if (SvSTASH (sv) == json_boolean_stash) |
513 | if (SvSTASH (sv) == stash) |
479 | { |
514 | { |
480 | if (SvIV (sv) == 0) |
515 | if (SvIV (sv)) |
|
|
516 | encode_str (enc, "true", 4, 0); |
|
|
517 | else |
481 | encode_str (enc, "false", 5, 0); |
518 | encode_str (enc, "false", 5, 0); |
482 | else |
|
|
483 | encode_str (enc, "true", 4, 0); |
|
|
484 | } |
519 | } |
485 | else |
520 | else |
486 | { |
521 | { |
487 | #if 0 |
522 | #if 0 |
488 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
523 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
489 | { |
524 | { |
490 | // not yet |
525 | // not yet |
491 | } |
526 | } |
492 | #endif |
527 | #endif |
493 | if (enc->flags & F_CONV_BLESSED) |
528 | if (enc->json.flags & F_CONV_BLESSED) |
494 | { |
529 | { |
495 | // we re-bless the reference to get overload and other niceties right |
530 | // we re-bless the reference to get overload and other niceties right |
496 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
531 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
497 | |
532 | |
498 | if (to_json) |
533 | if (to_json) |
499 | { |
534 | { |
500 | dSP; |
535 | dSP; |
501 | ENTER; |
536 | |
502 | SAVETMPS; |
|
|
503 | PUSHMARK (SP); |
537 | ENTER; SAVETMPS; PUSHMARK (SP); |
504 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
538 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
505 | |
539 | |
506 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
540 | // calling with G_SCALAR ensures that we always get a 1 return value |
507 | // check anyways. |
|
|
508 | PUTBACK; |
541 | PUTBACK; |
509 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
542 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
510 | SPAGAIN; |
543 | SPAGAIN; |
511 | |
544 | |
|
|
545 | // catch this surprisingly common error |
|
|
546 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
547 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
548 | |
|
|
549 | sv = POPs; |
|
|
550 | PUTBACK; |
|
|
551 | |
512 | encode_sv (enc, POPs); |
552 | encode_sv (enc, sv); |
513 | |
553 | |
514 | FREETMPS; |
554 | FREETMPS; LEAVE; |
515 | LEAVE; |
|
|
516 | } |
555 | } |
517 | else if (enc->flags & F_ALLOW_BLESSED) |
556 | else if (enc->json.flags & F_ALLOW_BLESSED) |
518 | encode_str (enc, "null", 4, 0); |
557 | encode_str (enc, "null", 4, 0); |
519 | else |
558 | else |
520 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
559 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
521 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
560 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
522 | } |
561 | } |
523 | else if (enc->flags & F_ALLOW_BLESSED) |
562 | else if (enc->json.flags & F_ALLOW_BLESSED) |
524 | encode_str (enc, "null", 4, 0); |
563 | encode_str (enc, "null", 4, 0); |
525 | else |
564 | else |
526 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
565 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
527 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
566 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
528 | } |
567 | } |
… | |
… | |
531 | encode_hv (enc, (HV *)sv); |
570 | encode_hv (enc, (HV *)sv); |
532 | else if (svt == SVt_PVAV) |
571 | else if (svt == SVt_PVAV) |
533 | encode_av (enc, (AV *)sv); |
572 | encode_av (enc, (AV *)sv); |
534 | else if (svt < SVt_PVAV) |
573 | else if (svt < SVt_PVAV) |
535 | { |
574 | { |
536 | if (SvNIOK (sv) && SvIV (sv) == 0) |
575 | STRLEN len = 0; |
|
|
576 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
577 | |
|
|
578 | if (len == 1 && *pv == '1') |
|
|
579 | encode_str (enc, "true", 4, 0); |
|
|
580 | else if (len == 1 && *pv == '0') |
537 | encode_str (enc, "false", 5, 0); |
581 | encode_str (enc, "false", 5, 0); |
538 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
539 | encode_str (enc, "true", 4, 0); |
|
|
540 | else |
582 | else |
541 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
583 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
542 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
584 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
543 | } |
585 | } |
544 | else |
586 | else |
… | |
… | |
614 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
656 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
615 | SvPV_nolen (sv), SvFLAGS (sv)); |
657 | SvPV_nolen (sv), SvFLAGS (sv)); |
616 | } |
658 | } |
617 | |
659 | |
618 | static SV * |
660 | static SV * |
619 | encode_json (SV *scalar, U32 flags) |
661 | encode_json (SV *scalar, JSON *json) |
620 | { |
662 | { |
621 | enc_t enc; |
663 | enc_t enc; |
622 | |
664 | |
623 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
665 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
624 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
666 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
625 | |
667 | |
626 | enc.flags = flags; |
668 | enc.json = *json; |
627 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
669 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
628 | enc.cur = SvPVX (enc.sv); |
670 | enc.cur = SvPVX (enc.sv); |
629 | enc.end = SvEND (enc.sv); |
671 | enc.end = SvEND (enc.sv); |
630 | enc.indent = 0; |
672 | enc.indent = 0; |
631 | enc.maxdepth = DEC_DEPTH (flags); |
673 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
632 | |
674 | |
633 | SvPOK_only (enc.sv); |
675 | SvPOK_only (enc.sv); |
634 | encode_sv (&enc, scalar); |
676 | encode_sv (&enc, scalar); |
635 | |
677 | |
636 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
678 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
637 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
679 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
638 | |
680 | |
639 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
681 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
640 | SvUTF8_on (enc.sv); |
682 | SvUTF8_on (enc.sv); |
641 | |
683 | |
642 | if (enc.flags & F_SHRINK) |
684 | if (enc.json.flags & F_SHRINK) |
643 | shrink (enc.sv); |
685 | shrink (enc.sv); |
644 | |
686 | |
645 | return enc.sv; |
687 | return enc.sv; |
646 | } |
688 | } |
647 | |
689 | |
… | |
… | |
652 | typedef struct |
694 | typedef struct |
653 | { |
695 | { |
654 | char *cur; // current parser pointer |
696 | char *cur; // current parser pointer |
655 | char *end; // end of input string |
697 | char *end; // end of input string |
656 | const char *err; // parse error, if != 0 |
698 | const char *err; // parse error, if != 0 |
657 | U32 flags; // F_* |
699 | JSON json; |
658 | U32 depth; // recursion depth |
700 | U32 depth; // recursion depth |
659 | U32 maxdepth; // recursion depth limit |
701 | U32 maxdepth; // recursion depth limit |
660 | } dec_t; |
702 | } dec_t; |
661 | |
703 | |
662 | inline void |
704 | inline void |
|
|
705 | decode_comment (dec_t *dec) |
|
|
706 | { |
|
|
707 | // only '#'-style comments allowed a.t.m. |
|
|
708 | |
|
|
709 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
|
|
710 | ++dec->cur; |
|
|
711 | } |
|
|
712 | |
|
|
713 | inline void |
663 | decode_ws (dec_t *dec) |
714 | decode_ws (dec_t *dec) |
664 | { |
715 | { |
665 | for (;;) |
716 | for (;;) |
666 | { |
717 | { |
667 | char ch = *dec->cur; |
718 | char ch = *dec->cur; |
668 | |
719 | |
669 | if (ch > 0x20 |
720 | if (ch > 0x20) |
|
|
721 | { |
|
|
722 | if (expect_false (ch == '#')) |
|
|
723 | { |
|
|
724 | if (dec->json.flags & F_RELAXED) |
|
|
725 | decode_comment (dec); |
|
|
726 | else |
|
|
727 | break; |
|
|
728 | } |
|
|
729 | else |
|
|
730 | break; |
|
|
731 | } |
670 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
732 | else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
671 | break; |
733 | break; // parse error, but let higher level handle it, gives better error messages |
672 | |
734 | |
673 | ++dec->cur; |
735 | ++dec->cur; |
674 | } |
736 | } |
675 | } |
737 | } |
676 | |
738 | |
… | |
… | |
925 | is_nv = 1; |
987 | is_nv = 1; |
926 | } |
988 | } |
927 | |
989 | |
928 | if (!is_nv) |
990 | if (!is_nv) |
929 | { |
991 | { |
|
|
992 | int len = dec->cur - start; |
|
|
993 | |
930 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
994 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
931 | if (*start == '-') |
995 | if (*start == '-') |
932 | switch (dec->cur - start) |
996 | switch (len) |
933 | { |
997 | { |
934 | case 2: return newSViv (-( start [1] - '0' * 1)); |
998 | case 2: return newSViv (-( start [1] - '0' * 1)); |
935 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
999 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
936 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1000 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
937 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1001 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
938 | } |
1002 | } |
939 | else |
1003 | else |
940 | switch (dec->cur - start) |
1004 | switch (len) |
941 | { |
1005 | { |
942 | case 1: return newSViv ( start [0] - '0' * 1); |
1006 | case 1: return newSViv ( start [0] - '0' * 1); |
943 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1007 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
944 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1008 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
945 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1009 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
946 | } |
1010 | } |
947 | |
1011 | |
948 | { |
1012 | { |
949 | UV uv; |
1013 | UV uv; |
950 | int numtype = grok_number (start, dec->cur - start, &uv); |
1014 | int numtype = grok_number (start, len, &uv); |
951 | if (numtype & IS_NUMBER_IN_UV) |
1015 | if (numtype & IS_NUMBER_IN_UV) |
952 | if (numtype & IS_NUMBER_NEG) |
1016 | if (numtype & IS_NUMBER_NEG) |
953 | { |
1017 | { |
954 | if (uv < (UV)IV_MIN) |
1018 | if (uv < (UV)IV_MIN) |
955 | return newSViv (-(IV)uv); |
1019 | return newSViv (-(IV)uv); |
956 | } |
1020 | } |
957 | else |
1021 | else |
958 | return newSVuv (uv); |
1022 | return newSVuv (uv); |
959 | |
|
|
960 | // here would likely be the place for bigint support |
|
|
961 | } |
1023 | } |
962 | } |
|
|
963 | |
1024 | |
964 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
1025 | len -= *start == '-' ? 1 : 0; |
|
|
1026 | |
|
|
1027 | // does not fit into IV or UV, try NV |
|
|
1028 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
1029 | #if defined (LDBL_DIG) |
|
|
1030 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1031 | #endif |
|
|
1032 | ) |
|
|
1033 | // fits into NV without loss of precision |
|
|
1034 | return newSVnv (Atof (start)); |
|
|
1035 | |
|
|
1036 | // everything else fails, convert it to a string |
|
|
1037 | return newSVpvn (start, dec->cur - start); |
|
|
1038 | } |
|
|
1039 | |
|
|
1040 | // loss of precision here |
965 | return newSVnv (Atof (start)); |
1041 | return newSVnv (Atof (start)); |
966 | |
1042 | |
967 | fail: |
1043 | fail: |
968 | return 0; |
1044 | return 0; |
969 | } |
1045 | } |
… | |
… | |
999 | |
1075 | |
1000 | if (*dec->cur != ',') |
1076 | if (*dec->cur != ',') |
1001 | ERR (", or ] expected while parsing array"); |
1077 | ERR (", or ] expected while parsing array"); |
1002 | |
1078 | |
1003 | ++dec->cur; |
1079 | ++dec->cur; |
|
|
1080 | |
|
|
1081 | decode_ws (dec); |
|
|
1082 | |
|
|
1083 | if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
|
|
1084 | { |
|
|
1085 | ++dec->cur; |
|
|
1086 | break; |
|
|
1087 | } |
1004 | } |
1088 | } |
1005 | |
1089 | |
1006 | DEC_DEC_DEPTH; |
1090 | DEC_DEC_DEPTH; |
1007 | return newRV_noinc ((SV *)av); |
1091 | return newRV_noinc ((SV *)av); |
1008 | |
1092 | |
… | |
… | |
1013 | } |
1097 | } |
1014 | |
1098 | |
1015 | static SV * |
1099 | static SV * |
1016 | decode_hv (dec_t *dec) |
1100 | decode_hv (dec_t *dec) |
1017 | { |
1101 | { |
|
|
1102 | SV *sv; |
1018 | HV *hv = newHV (); |
1103 | HV *hv = newHV (); |
1019 | |
1104 | |
1020 | DEC_INC_DEPTH; |
1105 | DEC_INC_DEPTH; |
1021 | decode_ws (dec); |
1106 | decode_ws (dec); |
1022 | |
1107 | |
1023 | if (*dec->cur == '}') |
1108 | if (*dec->cur == '}') |
1024 | ++dec->cur; |
1109 | ++dec->cur; |
1025 | else |
1110 | else |
1026 | for (;;) |
1111 | for (;;) |
1027 | { |
1112 | { |
1028 | decode_ws (dec); EXPECT_CH ('"'); |
1113 | EXPECT_CH ('"'); |
1029 | |
1114 | |
1030 | // heuristic: assume that |
1115 | // heuristic: assume that |
1031 | // a) decode_str + hv_store_ent are abysmally slow |
1116 | // a) decode_str + hv_store_ent are abysmally slow. |
1032 | // b) most hash keys are short, simple ascii text |
1117 | // b) most hash keys are short, simple ascii text. |
1033 | // so try to "fast-match" such strings to avoid |
1118 | // => try to "fast-match" such strings to avoid |
1034 | // the overhead of hv_store_ent. |
1119 | // the overhead of decode_str + hv_store_ent. |
1035 | { |
1120 | { |
1036 | SV *value; |
1121 | SV *value; |
1037 | char *p = dec->cur; |
1122 | char *p = dec->cur; |
1038 | char *e = p + 24; // only try up to 24 bytes |
1123 | char *e = p + 24; // only try up to 24 bytes |
1039 | |
1124 | |
1040 | for (;;) |
1125 | for (;;) |
1041 | { |
1126 | { |
|
|
1127 | // the >= 0x80 is true on most architectures |
1042 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1128 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1043 | { |
1129 | { |
1044 | // slow path, back up and use decode_str |
1130 | // slow path, back up and use decode_str |
1045 | SV *key = decode_str (dec); |
1131 | SV *key = decode_str (dec); |
1046 | if (!key) |
1132 | if (!key) |
1047 | goto fail; |
1133 | goto fail; |
1048 | |
1134 | |
1049 | decode_ws (dec); EXPECT_CH (':'); |
1135 | decode_ws (dec); EXPECT_CH (':'); |
1050 | |
1136 | |
|
|
1137 | decode_ws (dec); |
1051 | value = decode_sv (dec); |
1138 | value = decode_sv (dec); |
1052 | if (!value) |
1139 | if (!value) |
1053 | { |
1140 | { |
1054 | SvREFCNT_dec (key); |
1141 | SvREFCNT_dec (key); |
1055 | goto fail; |
1142 | goto fail; |
… | |
… | |
1067 | int len = p - key; |
1154 | int len = p - key; |
1068 | dec->cur = p + 1; |
1155 | dec->cur = p + 1; |
1069 | |
1156 | |
1070 | decode_ws (dec); EXPECT_CH (':'); |
1157 | decode_ws (dec); EXPECT_CH (':'); |
1071 | |
1158 | |
|
|
1159 | decode_ws (dec); |
1072 | value = decode_sv (dec); |
1160 | value = decode_sv (dec); |
1073 | if (!value) |
1161 | if (!value) |
1074 | goto fail; |
1162 | goto fail; |
1075 | |
1163 | |
1076 | hv_store (hv, key, len, value, 0); |
1164 | hv_store (hv, key, len, value, 0); |
… | |
… | |
1092 | |
1180 | |
1093 | if (*dec->cur != ',') |
1181 | if (*dec->cur != ',') |
1094 | ERR (", or } expected while parsing object/hash"); |
1182 | ERR (", or } expected while parsing object/hash"); |
1095 | |
1183 | |
1096 | ++dec->cur; |
1184 | ++dec->cur; |
|
|
1185 | |
|
|
1186 | decode_ws (dec); |
|
|
1187 | |
|
|
1188 | if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
|
|
1189 | { |
|
|
1190 | ++dec->cur; |
|
|
1191 | break; |
|
|
1192 | } |
1097 | } |
1193 | } |
1098 | |
1194 | |
1099 | DEC_DEC_DEPTH; |
1195 | DEC_DEC_DEPTH; |
1100 | return newRV_noinc ((SV *)hv); |
1196 | sv = newRV_noinc ((SV *)hv); |
|
|
1197 | |
|
|
1198 | // check filter callbacks |
|
|
1199 | if (dec->json.flags & F_HOOK) |
|
|
1200 | { |
|
|
1201 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1202 | { |
|
|
1203 | HE *cb, *he; |
|
|
1204 | |
|
|
1205 | hv_iterinit (hv); |
|
|
1206 | he = hv_iternext (hv); |
|
|
1207 | hv_iterinit (hv); |
|
|
1208 | |
|
|
1209 | // the next line creates a mortal sv each time its called. |
|
|
1210 | // might want to optimise this for common cases. |
|
|
1211 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1212 | |
|
|
1213 | if (cb) |
|
|
1214 | { |
|
|
1215 | dSP; |
|
|
1216 | int count; |
|
|
1217 | |
|
|
1218 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1219 | XPUSHs (HeVAL (he)); |
|
|
1220 | |
|
|
1221 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1222 | |
|
|
1223 | if (count == 1) |
|
|
1224 | { |
|
|
1225 | sv = newSVsv (POPs); |
|
|
1226 | FREETMPS; LEAVE; |
|
|
1227 | return sv; |
|
|
1228 | } |
|
|
1229 | |
|
|
1230 | FREETMPS; LEAVE; |
|
|
1231 | } |
|
|
1232 | } |
|
|
1233 | |
|
|
1234 | if (dec->json.cb_object) |
|
|
1235 | { |
|
|
1236 | dSP; |
|
|
1237 | int count; |
|
|
1238 | |
|
|
1239 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1240 | XPUSHs (sv_2mortal (sv)); |
|
|
1241 | |
|
|
1242 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1243 | |
|
|
1244 | if (count == 1) |
|
|
1245 | { |
|
|
1246 | sv = newSVsv (POPs); |
|
|
1247 | FREETMPS; LEAVE; |
|
|
1248 | return sv; |
|
|
1249 | } |
|
|
1250 | |
|
|
1251 | SvREFCNT_inc (sv); |
|
|
1252 | FREETMPS; LEAVE; |
|
|
1253 | } |
|
|
1254 | } |
|
|
1255 | |
|
|
1256 | return sv; |
1101 | |
1257 | |
1102 | fail: |
1258 | fail: |
1103 | SvREFCNT_dec (hv); |
1259 | SvREFCNT_dec (hv); |
1104 | DEC_DEC_DEPTH; |
1260 | DEC_DEC_DEPTH; |
1105 | return 0; |
1261 | return 0; |
1106 | } |
1262 | } |
1107 | |
1263 | |
1108 | static SV * |
1264 | static SV * |
1109 | decode_sv (dec_t *dec) |
1265 | decode_sv (dec_t *dec) |
1110 | { |
1266 | { |
1111 | decode_ws (dec); |
|
|
1112 | |
|
|
1113 | // the beauty of JSON: you need exactly one character lookahead |
1267 | // the beauty of JSON: you need exactly one character lookahead |
1114 | // to parse anything. |
1268 | // to parse anything. |
1115 | switch (*dec->cur) |
1269 | switch (*dec->cur) |
1116 | { |
1270 | { |
1117 | case '"': ++dec->cur; return decode_str (dec); |
1271 | case '"': ++dec->cur; return decode_str (dec); |
… | |
… | |
1125 | |
1279 | |
1126 | case 't': |
1280 | case 't': |
1127 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1281 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1128 | { |
1282 | { |
1129 | dec->cur += 4; |
1283 | dec->cur += 4; |
|
|
1284 | #if JSON_SLOW |
|
|
1285 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
|
|
1286 | #endif |
1130 | return SvREFCNT_inc (json_true); |
1287 | return SvREFCNT_inc (json_true); |
1131 | } |
1288 | } |
1132 | else |
1289 | else |
1133 | ERR ("'true' expected"); |
1290 | ERR ("'true' expected"); |
1134 | |
1291 | |
… | |
… | |
1136 | |
1293 | |
1137 | case 'f': |
1294 | case 'f': |
1138 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1295 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1139 | { |
1296 | { |
1140 | dec->cur += 5; |
1297 | dec->cur += 5; |
|
|
1298 | #if JSON_SLOW |
|
|
1299 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
|
|
1300 | #endif |
1141 | return SvREFCNT_inc (json_false); |
1301 | return SvREFCNT_inc (json_false); |
1142 | } |
1302 | } |
1143 | else |
1303 | else |
1144 | ERR ("'false' expected"); |
1304 | ERR ("'false' expected"); |
1145 | |
1305 | |
… | |
… | |
1164 | fail: |
1324 | fail: |
1165 | return 0; |
1325 | return 0; |
1166 | } |
1326 | } |
1167 | |
1327 | |
1168 | static SV * |
1328 | static SV * |
1169 | decode_json (SV *string, U32 flags, UV *offset_return) |
1329 | decode_json (SV *string, JSON *json, UV *offset_return) |
1170 | { |
1330 | { |
1171 | dec_t dec; |
1331 | dec_t dec; |
1172 | UV offset; |
1332 | UV offset; |
1173 | SV *sv; |
1333 | SV *sv; |
1174 | |
1334 | |
1175 | SvGETMAGIC (string); |
1335 | SvGETMAGIC (string); |
1176 | SvUPGRADE (string, SVt_PV); |
1336 | SvUPGRADE (string, SVt_PV); |
1177 | |
1337 | |
1178 | if (flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (flags)) |
1338 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
1179 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1339 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1180 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (flags)); |
1340 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
1181 | |
1341 | |
1182 | if (flags & F_UTF8) |
1342 | if (json->flags & F_UTF8) |
1183 | sv_utf8_downgrade (string, 0); |
1343 | sv_utf8_downgrade (string, 0); |
1184 | else |
1344 | else |
1185 | sv_utf8_upgrade (string); |
1345 | sv_utf8_upgrade (string); |
1186 | |
1346 | |
1187 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1347 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1188 | |
1348 | |
1189 | dec.flags = flags; |
1349 | dec.json = *json; |
1190 | dec.cur = SvPVX (string); |
1350 | dec.cur = SvPVX (string); |
1191 | dec.end = SvEND (string); |
1351 | dec.end = SvEND (string); |
1192 | dec.err = 0; |
1352 | dec.err = 0; |
1193 | dec.depth = 0; |
1353 | dec.depth = 0; |
1194 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1354 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
|
|
1355 | |
|
|
1356 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1357 | dec.json.flags |= F_HOOK; |
1195 | |
1358 | |
1196 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1359 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1360 | |
|
|
1361 | decode_ws (&dec); |
1197 | sv = decode_sv (&dec); |
1362 | sv = decode_sv (&dec); |
1198 | |
1363 | |
1199 | if (!(offset_return || !sv)) |
1364 | if (!(offset_return || !sv)) |
1200 | { |
1365 | { |
1201 | // check for trailing garbage |
1366 | // check for trailing garbage |
… | |
… | |
1209 | } |
1374 | } |
1210 | } |
1375 | } |
1211 | |
1376 | |
1212 | if (offset_return || !sv) |
1377 | if (offset_return || !sv) |
1213 | { |
1378 | { |
1214 | offset = dec.flags & F_UTF8 |
1379 | offset = dec.json.flags & F_UTF8 |
1215 | ? dec.cur - SvPVX (string) |
1380 | ? dec.cur - SvPVX (string) |
1216 | : utf8_distance (dec.cur, SvPVX (string)); |
1381 | : utf8_distance (dec.cur, SvPVX (string)); |
1217 | |
1382 | |
1218 | if (offset_return) |
1383 | if (offset_return) |
1219 | *offset_return = offset; |
1384 | *offset_return = offset; |
… | |
… | |
1238 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1403 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1239 | } |
1404 | } |
1240 | |
1405 | |
1241 | sv = sv_2mortal (sv); |
1406 | sv = sv_2mortal (sv); |
1242 | |
1407 | |
1243 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1408 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1244 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1409 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1245 | |
1410 | |
1246 | return sv; |
1411 | return sv; |
1247 | } |
1412 | } |
1248 | |
1413 | |
… | |
… | |
1269 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1434 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1270 | } |
1435 | } |
1271 | |
1436 | |
1272 | PROTOTYPES: DISABLE |
1437 | PROTOTYPES: DISABLE |
1273 | |
1438 | |
1274 | SV *new (char *dummy) |
1439 | void CLONE (...) |
1275 | CODE: |
1440 | CODE: |
1276 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1441 | json_stash = 0; |
1277 | OUTPUT: |
1442 | json_boolean_stash = 0; |
1278 | RETVAL |
|
|
1279 | |
1443 | |
|
|
1444 | void new (char *klass) |
|
|
1445 | PPCODE: |
|
|
1446 | { |
|
|
1447 | SV *pv = NEWSV (0, sizeof (JSON)); |
|
|
1448 | SvPOK_only (pv); |
|
|
1449 | Zero (SvPVX (pv), 1, JSON); |
|
|
1450 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1451 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); |
|
|
1452 | } |
|
|
1453 | |
1280 | SV *ascii (SV *self, int enable = 1) |
1454 | void ascii (JSON *self, int enable = 1) |
1281 | ALIAS: |
1455 | ALIAS: |
1282 | ascii = F_ASCII |
1456 | ascii = F_ASCII |
1283 | latin1 = F_LATIN1 |
1457 | latin1 = F_LATIN1 |
1284 | utf8 = F_UTF8 |
1458 | utf8 = F_UTF8 |
1285 | indent = F_INDENT |
1459 | indent = F_INDENT |
… | |
… | |
1289 | pretty = F_PRETTY |
1463 | pretty = F_PRETTY |
1290 | allow_nonref = F_ALLOW_NONREF |
1464 | allow_nonref = F_ALLOW_NONREF |
1291 | shrink = F_SHRINK |
1465 | shrink = F_SHRINK |
1292 | allow_blessed = F_ALLOW_BLESSED |
1466 | allow_blessed = F_ALLOW_BLESSED |
1293 | convert_blessed = F_CONV_BLESSED |
1467 | convert_blessed = F_CONV_BLESSED |
|
|
1468 | relaxed = F_RELAXED |
1294 | CODE: |
1469 | PPCODE: |
1295 | { |
1470 | { |
1296 | UV *uv = SvJSON (self); |
|
|
1297 | if (enable) |
1471 | if (enable) |
1298 | *uv |= ix; |
1472 | self->flags |= ix; |
1299 | else |
1473 | else |
1300 | *uv &= ~ix; |
1474 | self->flags &= ~ix; |
1301 | |
1475 | |
1302 | RETVAL = newSVsv (self); |
1476 | XPUSHs (ST (0)); |
1303 | } |
1477 | } |
1304 | OUTPUT: |
|
|
1305 | RETVAL |
|
|
1306 | |
1478 | |
1307 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1479 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1308 | CODE: |
1480 | PPCODE: |
1309 | { |
1481 | { |
1310 | UV *uv = SvJSON (self); |
|
|
1311 | UV log2 = 0; |
1482 | UV log2 = 0; |
1312 | |
1483 | |
1313 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1484 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1314 | |
1485 | |
1315 | while ((1UL << log2) < max_depth) |
1486 | while ((1UL << log2) < max_depth) |
1316 | ++log2; |
1487 | ++log2; |
1317 | |
1488 | |
1318 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1489 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1319 | |
1490 | |
1320 | RETVAL = newSVsv (self); |
1491 | XPUSHs (ST (0)); |
1321 | } |
1492 | } |
1322 | OUTPUT: |
|
|
1323 | RETVAL |
|
|
1324 | |
1493 | |
1325 | SV *max_size (SV *self, UV max_size = 0) |
1494 | void max_size (JSON *self, UV max_size = 0) |
1326 | CODE: |
1495 | PPCODE: |
1327 | { |
1496 | { |
1328 | UV *uv = SvJSON (self); |
|
|
1329 | UV log2 = 0; |
1497 | UV log2 = 0; |
1330 | |
1498 | |
1331 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1499 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1332 | if (max_size == 1) max_size = 2; |
1500 | if (max_size == 1) max_size = 2; |
1333 | |
1501 | |
1334 | while ((1UL << log2) < max_size) |
1502 | while ((1UL << log2) < max_size) |
1335 | ++log2; |
1503 | ++log2; |
1336 | |
1504 | |
1337 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1505 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1338 | |
1506 | |
1339 | RETVAL = newSVsv (self); |
1507 | XPUSHs (ST (0)); |
1340 | } |
1508 | } |
1341 | OUTPUT: |
|
|
1342 | RETVAL |
|
|
1343 | |
1509 | |
1344 | void encode (SV *self, SV *scalar) |
1510 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1345 | PPCODE: |
1511 | PPCODE: |
1346 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1512 | { |
|
|
1513 | SvREFCNT_dec (self->cb_object); |
|
|
1514 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1347 | |
1515 | |
1348 | void decode (SV *self, SV *jsonstr) |
1516 | XPUSHs (ST (0)); |
|
|
1517 | } |
|
|
1518 | |
|
|
1519 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1349 | PPCODE: |
1520 | PPCODE: |
|
|
1521 | { |
|
|
1522 | if (!self->cb_sk_object) |
|
|
1523 | self->cb_sk_object = newHV (); |
|
|
1524 | |
|
|
1525 | if (SvOK (cb)) |
|
|
1526 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1527 | else |
|
|
1528 | { |
|
|
1529 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1530 | |
|
|
1531 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1532 | { |
|
|
1533 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1534 | self->cb_sk_object = 0; |
|
|
1535 | } |
|
|
1536 | } |
|
|
1537 | |
|
|
1538 | XPUSHs (ST (0)); |
|
|
1539 | } |
|
|
1540 | |
|
|
1541 | void encode (JSON *self, SV *scalar) |
|
|
1542 | PPCODE: |
|
|
1543 | XPUSHs (encode_json (scalar, self)); |
|
|
1544 | |
|
|
1545 | void decode (JSON *self, SV *jsonstr) |
|
|
1546 | PPCODE: |
1350 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1547 | XPUSHs (decode_json (jsonstr, self, 0)); |
1351 | |
1548 | |
1352 | void decode_prefix (SV *self, SV *jsonstr) |
1549 | void decode_prefix (JSON *self, SV *jsonstr) |
1353 | PPCODE: |
1550 | PPCODE: |
1354 | { |
1551 | { |
1355 | UV offset; |
1552 | UV offset; |
1356 | EXTEND (SP, 2); |
1553 | EXTEND (SP, 2); |
1357 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1554 | PUSHs (decode_json (jsonstr, self, &offset)); |
1358 | PUSHs (sv_2mortal (newSVuv (offset))); |
1555 | PUSHs (sv_2mortal (newSVuv (offset))); |
1359 | } |
1556 | } |
1360 | |
1557 | |
|
|
1558 | void DESTROY (JSON *self) |
|
|
1559 | CODE: |
|
|
1560 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1561 | SvREFCNT_dec (self->cb_object); |
|
|
1562 | |
1361 | PROTOTYPES: ENABLE |
1563 | PROTOTYPES: ENABLE |
1362 | |
1564 | |
1363 | void to_json (SV *scalar) |
1565 | void to_json (SV *scalar) |
1364 | ALIAS: |
|
|
1365 | objToJson = 0 |
|
|
1366 | PPCODE: |
1566 | PPCODE: |
|
|
1567 | { |
|
|
1568 | JSON json = { F_DEFAULT | F_UTF8 }; |
1367 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1569 | XPUSHs (encode_json (scalar, &json)); |
|
|
1570 | } |
1368 | |
1571 | |
1369 | void from_json (SV *jsonstr) |
1572 | void from_json (SV *jsonstr) |
1370 | ALIAS: |
|
|
1371 | jsonToObj = 0 |
|
|
1372 | PPCODE: |
1573 | PPCODE: |
|
|
1574 | { |
|
|
1575 | JSON json = { F_DEFAULT | F_UTF8 }; |
1373 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1576 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1577 | } |
1374 | |
1578 | |