… | |
… | |
312 | int i, len = av_len (av); |
312 | int i, len = av_len (av); |
313 | |
313 | |
314 | if (enc->indent >= enc->maxdepth) |
314 | if (enc->indent >= enc->maxdepth) |
315 | croak ("data structure too deep (hit recursion limit)"); |
315 | croak ("data structure too deep (hit recursion limit)"); |
316 | |
316 | |
317 | encode_ch (enc, '['); encode_nl (enc); |
317 | encode_ch (enc, '['); |
318 | ++enc->indent; |
318 | |
|
|
319 | if (len >= 0) |
|
|
320 | { |
|
|
321 | encode_nl (enc); ++enc->indent; |
319 | |
322 | |
320 | for (i = 0; i <= len; ++i) |
323 | for (i = 0; i <= len; ++i) |
321 | { |
324 | { |
322 | SV **svp = av_fetch (av, i, 0); |
325 | SV **svp = av_fetch (av, i, 0); |
323 | |
326 | |
324 | encode_indent (enc); |
327 | encode_indent (enc); |
325 | |
328 | |
326 | if (svp) |
329 | if (svp) |
327 | encode_sv (enc, *svp); |
330 | encode_sv (enc, *svp); |
328 | else |
331 | else |
329 | encode_str (enc, "null", 4, 0); |
332 | encode_str (enc, "null", 4, 0); |
330 | |
333 | |
331 | if (i < len) |
334 | if (i < len) |
332 | encode_comma (enc); |
335 | encode_comma (enc); |
333 | } |
336 | } |
334 | |
337 | |
|
|
338 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
339 | } |
|
|
340 | |
335 | encode_nl (enc); |
341 | encode_ch (enc, ']'); |
336 | |
|
|
337 | --enc->indent; |
|
|
338 | encode_indent (enc); encode_ch (enc, ']'); |
|
|
339 | } |
342 | } |
340 | |
343 | |
341 | static void |
344 | static void |
342 | encode_hk (enc_t *enc, HE *he) |
345 | encode_hk (enc_t *enc, HE *he) |
343 | { |
346 | { |
… | |
… | |
396 | int count; |
399 | int count; |
397 | |
400 | |
398 | if (enc->indent >= enc->maxdepth) |
401 | if (enc->indent >= enc->maxdepth) |
399 | croak ("data structure too deep (hit recursion limit)"); |
402 | croak ("data structure too deep (hit recursion limit)"); |
400 | |
403 | |
401 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
404 | encode_ch (enc, '{'); |
402 | |
405 | |
403 | // for canonical output we have to sort by keys first |
406 | // for canonical output we have to sort by keys first |
404 | // actually, this is mostly due to the stupid so-called |
407 | // actually, this is mostly due to the stupid so-called |
405 | // security workaround added somewhere in 5.8.x. |
408 | // security workaround added somewhere in 5.8.x. |
406 | // that randomises hash orderings |
409 | // that randomises hash orderings |
… | |
… | |
459 | |
462 | |
460 | FREETMPS; |
463 | FREETMPS; |
461 | LEAVE; |
464 | LEAVE; |
462 | } |
465 | } |
463 | |
466 | |
|
|
467 | encode_nl (enc); ++enc->indent; |
|
|
468 | |
464 | while (count--) |
469 | while (count--) |
465 | { |
470 | { |
466 | encode_indent (enc); |
471 | encode_indent (enc); |
467 | he = hes [count]; |
472 | he = hes [count]; |
468 | encode_hk (enc, he); |
473 | encode_hk (enc, he); |
469 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
474 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
470 | |
475 | |
471 | if (count) |
476 | if (count) |
472 | encode_comma (enc); |
477 | encode_comma (enc); |
473 | } |
478 | } |
|
|
479 | |
|
|
480 | encode_nl (enc); --enc->indent; encode_indent (enc); |
474 | } |
481 | } |
475 | } |
482 | } |
476 | else |
483 | else |
477 | { |
484 | { |
478 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
485 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
479 | if ((he = hv_iternext (hv))) |
486 | if ((he = hv_iternext (hv))) |
|
|
487 | { |
|
|
488 | encode_nl (enc); ++enc->indent; |
|
|
489 | |
480 | for (;;) |
490 | for (;;) |
481 | { |
491 | { |
482 | encode_indent (enc); |
492 | encode_indent (enc); |
483 | encode_hk (enc, he); |
493 | encode_hk (enc, he); |
484 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
494 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
485 | |
495 | |
486 | if (!(he = hv_iternext (hv))) |
496 | if (!(he = hv_iternext (hv))) |
487 | break; |
497 | break; |
488 | |
498 | |
489 | encode_comma (enc); |
499 | encode_comma (enc); |
490 | } |
500 | } |
491 | } |
|
|
492 | |
501 | |
|
|
502 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
503 | } |
|
|
504 | } |
|
|
505 | |
493 | encode_nl (enc); |
506 | encode_ch (enc, '}'); |
494 | |
|
|
495 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
496 | } |
507 | } |
497 | |
508 | |
498 | // encode objects, arrays and special \0=false and \1=true values. |
509 | // encode objects, arrays and special \0=false and \1=true values. |
499 | static void |
510 | static void |
500 | encode_rv (enc_t *enc, SV *sv) |
511 | encode_rv (enc_t *enc, SV *sv) |
… | |
… | |
1474 | self->flags &= ~ix; |
1485 | self->flags &= ~ix; |
1475 | |
1486 | |
1476 | XPUSHs (ST (0)); |
1487 | XPUSHs (ST (0)); |
1477 | } |
1488 | } |
1478 | |
1489 | |
|
|
1490 | void get_ascii (JSON *self) |
|
|
1491 | ALIAS: |
|
|
1492 | get_ascii = F_ASCII |
|
|
1493 | get_latin1 = F_LATIN1 |
|
|
1494 | get_utf8 = F_UTF8 |
|
|
1495 | get_indent = F_INDENT |
|
|
1496 | get_canonical = F_CANONICAL |
|
|
1497 | get_space_before = F_SPACE_BEFORE |
|
|
1498 | get_space_after = F_SPACE_AFTER |
|
|
1499 | get_pretty = F_PRETTY |
|
|
1500 | get_allow_nonref = F_ALLOW_NONREF |
|
|
1501 | get_shrink = F_SHRINK |
|
|
1502 | get_allow_blessed = F_ALLOW_BLESSED |
|
|
1503 | get_convert_blessed = F_CONV_BLESSED |
|
|
1504 | get_relaxed = F_RELAXED |
|
|
1505 | PPCODE: |
|
|
1506 | XPUSHs (boolSV (self->flags & ix)); |
|
|
1507 | |
1479 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1508 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1480 | PPCODE: |
1509 | PPCODE: |
1481 | { |
1510 | { |
1482 | UV log2 = 0; |
1511 | UV log2 = 0; |
1483 | |
1512 | |
… | |
… | |
1489 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1518 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1490 | |
1519 | |
1491 | XPUSHs (ST (0)); |
1520 | XPUSHs (ST (0)); |
1492 | } |
1521 | } |
1493 | |
1522 | |
|
|
1523 | U32 get_max_depth (JSON *self) |
|
|
1524 | CODE: |
|
|
1525 | RETVAL = DEC_DEPTH (self->flags); |
|
|
1526 | OUTPUT: |
|
|
1527 | RETVAL |
|
|
1528 | |
1494 | void max_size (JSON *self, UV max_size = 0) |
1529 | void max_size (JSON *self, UV max_size = 0) |
1495 | PPCODE: |
1530 | PPCODE: |
1496 | { |
1531 | { |
1497 | UV log2 = 0; |
1532 | UV log2 = 0; |
1498 | |
1533 | |
… | |
… | |
1504 | |
1539 | |
1505 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1540 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1506 | |
1541 | |
1507 | XPUSHs (ST (0)); |
1542 | XPUSHs (ST (0)); |
1508 | } |
1543 | } |
|
|
1544 | |
|
|
1545 | int get_max_size (JSON *self) |
|
|
1546 | CODE: |
|
|
1547 | RETVAL = DEC_SIZE (self->flags); |
|
|
1548 | OUTPUT: |
|
|
1549 | RETVAL |
1509 | |
1550 | |
1510 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1551 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1511 | PPCODE: |
1552 | PPCODE: |
1512 | { |
1553 | { |
1513 | SvREFCNT_dec (self->cb_object); |
1554 | SvREFCNT_dec (self->cb_object); |