… | |
… | |
71 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
71 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
72 | #else |
72 | #else |
73 | # define JSON_SLOW 0 |
73 | # define JSON_SLOW 0 |
74 | # define JSON_STASH json_stash |
74 | # define JSON_STASH json_stash |
75 | #endif |
75 | #endif |
|
|
76 | |
|
|
77 | // the amount of HEs to allocate on the stack, when sorting keys |
|
|
78 | #define STACK_HES 64 |
76 | |
79 | |
77 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
80 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
78 | static SV *json_true, *json_false; |
81 | static SV *json_true, *json_false; |
79 | |
82 | |
80 | enum { |
83 | enum { |
… | |
… | |
480 | |
483 | |
481 | if (enc->indent >= enc->json.max_depth) |
484 | if (enc->indent >= enc->json.max_depth) |
482 | croak (ERR_NESTING_EXCEEDED); |
485 | croak (ERR_NESTING_EXCEEDED); |
483 | |
486 | |
484 | encode_ch (enc, '['); |
487 | encode_ch (enc, '['); |
485 | |
488 | |
486 | if (len >= 0) |
489 | if (len >= 0) |
487 | { |
490 | { |
488 | encode_nl (enc); ++enc->indent; |
491 | encode_nl (enc); ++enc->indent; |
489 | |
492 | |
490 | for (i = 0; i <= len; ++i) |
493 | for (i = 0; i <= len; ++i) |
… | |
… | |
502 | encode_comma (enc); |
505 | encode_comma (enc); |
503 | } |
506 | } |
504 | |
507 | |
505 | encode_nl (enc); --enc->indent; encode_indent (enc); |
508 | encode_nl (enc); --enc->indent; encode_indent (enc); |
506 | } |
509 | } |
507 | |
510 | |
508 | encode_ch (enc, ']'); |
511 | encode_ch (enc, ']'); |
509 | } |
512 | } |
510 | |
513 | |
511 | static void |
514 | static void |
512 | encode_hk (enc_t *enc, HE *he) |
515 | encode_hk (enc_t *enc, HE *he) |
… | |
… | |
516 | if (HeKLEN (he) == HEf_SVKEY) |
519 | if (HeKLEN (he) == HEf_SVKEY) |
517 | { |
520 | { |
518 | SV *sv = HeSVKEY (he); |
521 | SV *sv = HeSVKEY (he); |
519 | STRLEN len; |
522 | STRLEN len; |
520 | char *str; |
523 | char *str; |
521 | |
524 | |
522 | SvGETMAGIC (sv); |
525 | SvGETMAGIC (sv); |
523 | str = SvPV (sv, len); |
526 | str = SvPV (sv, len); |
524 | |
527 | |
525 | encode_str (enc, str, len, SvUTF8 (sv)); |
528 | encode_str (enc, str, len, SvUTF8 (sv)); |
526 | } |
529 | } |
… | |
… | |
592 | } |
595 | } |
593 | |
596 | |
594 | if (count) |
597 | if (count) |
595 | { |
598 | { |
596 | int i, fast = 1; |
599 | int i, fast = 1; |
597 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
600 | HE *hes_stack [STACK_HES]; |
598 | HE **hes = _alloca (count * sizeof (HE)); |
601 | HE **hes = hes_stack; |
599 | #else |
602 | |
600 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
603 | // allocate larger arrays on the heap |
601 | #endif |
604 | if (count > STACK_HES) |
|
|
605 | { |
|
|
606 | SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes))); |
|
|
607 | hes = (HE **)SvPVX (sv); |
|
|
608 | } |
602 | |
609 | |
603 | i = 0; |
610 | i = 0; |
604 | while ((he = hv_iternext (hv))) |
611 | while ((he = hv_iternext (hv))) |
605 | { |
612 | { |
606 | hes [i++] = he; |
613 | hes [i++] = he; |
… | |
… | |
836 | else if (SvROK (sv)) |
843 | else if (SvROK (sv)) |
837 | encode_rv (enc, SvRV (sv)); |
844 | encode_rv (enc, SvRV (sv)); |
838 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
845 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
839 | encode_str (enc, "null", 4, 0); |
846 | encode_str (enc, "null", 4, 0); |
840 | else |
847 | else |
841 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
848 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data", |
842 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
849 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
843 | } |
850 | } |
844 | |
851 | |
845 | static SV * |
852 | static SV * |
846 | encode_json (SV *scalar, JSON *json) |
853 | encode_json (SV *scalar, JSON *json) |
… | |
… | |
1259 | if (*dec->cur == ']') |
1266 | if (*dec->cur == ']') |
1260 | { |
1267 | { |
1261 | ++dec->cur; |
1268 | ++dec->cur; |
1262 | break; |
1269 | break; |
1263 | } |
1270 | } |
1264 | |
1271 | |
1265 | if (*dec->cur != ',') |
1272 | if (*dec->cur != ',') |
1266 | ERR (", or ] expected while parsing array"); |
1273 | ERR (", or ] expected while parsing array"); |
1267 | |
1274 | |
1268 | ++dec->cur; |
1275 | ++dec->cur; |
1269 | |
1276 | |
… | |
… | |
1457 | { |
1464 | { |
1458 | // the beauty of JSON: you need exactly one character lookahead |
1465 | // the beauty of JSON: you need exactly one character lookahead |
1459 | // to parse everything. |
1466 | // to parse everything. |
1460 | switch (*dec->cur) |
1467 | switch (*dec->cur) |
1461 | { |
1468 | { |
1462 | case '"': ++dec->cur; return decode_str (dec); |
1469 | case '"': ++dec->cur; return decode_str (dec); |
1463 | case '[': ++dec->cur; return decode_av (dec); |
1470 | case '[': ++dec->cur; return decode_av (dec); |
1464 | case '{': ++dec->cur; return decode_hv (dec); |
1471 | case '{': ++dec->cur; return decode_hv (dec); |
1465 | |
1472 | |
1466 | case '-': |
1473 | case '-': |
1467 | case '0': case '1': case '2': case '3': case '4': |
1474 | case '0': case '1': case '2': case '3': case '4': |
1468 | case '5': case '6': case '7': case '8': case '9': |
1475 | case '5': case '6': case '7': case '8': case '9': |
… | |
… | |
1521 | { |
1528 | { |
1522 | dec_t dec; |
1529 | dec_t dec; |
1523 | SV *sv; |
1530 | SV *sv; |
1524 | |
1531 | |
1525 | /* work around bugs in 5.10 where manipulating magic values |
1532 | /* work around bugs in 5.10 where manipulating magic values |
1526 | * will perl ignore the magic in subsequent accesses. |
1533 | * makes perl ignore the magic in subsequent accesses. |
1527 | * also make a copy of non-PV values, to get them into a clean |
1534 | * also make a copy of non-PV values, to get them into a clean |
1528 | * state (SvPV should do that, but it's buggy, see below). |
1535 | * state (SvPV should do that, but it's buggy, see below). |
1529 | */ |
1536 | */ |
1530 | /*SvGETMAGIC (string);*/ |
1537 | /*SvGETMAGIC (string);*/ |
1531 | if (SvMAGICAL (string) || !SvPOK (string)) |
1538 | if (SvMAGICAL (string) || !SvPOK (string)) |
… | |
… | |
1805 | json_boolean_stash = 0; |
1812 | json_boolean_stash = 0; |
1806 | |
1813 | |
1807 | void new (char *klass) |
1814 | void new (char *klass) |
1808 | PPCODE: |
1815 | PPCODE: |
1809 | { |
1816 | { |
1810 | SV *pv = NEWSV (0, sizeof (JSON)); |
1817 | SV *pv = NEWSV (0, sizeof (JSON)); |
1811 | SvPOK_only (pv); |
1818 | SvPOK_only (pv); |
1812 | json_init ((JSON *)SvPVX (pv)); |
1819 | json_init ((JSON *)SvPVX (pv)); |
1813 | XPUSHs (sv_2mortal (sv_bless ( |
1820 | XPUSHs (sv_2mortal (sv_bless ( |
1814 | newRV_noinc (pv), |
1821 | newRV_noinc (pv), |
1815 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
1822 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
… | |
… | |
2064 | SvREFCNT_dec (self->incr_text); |
2071 | SvREFCNT_dec (self->incr_text); |
2065 | |
2072 | |
2066 | PROTOTYPES: ENABLE |
2073 | PROTOTYPES: ENABLE |
2067 | |
2074 | |
2068 | void encode_json (SV *scalar) |
2075 | void encode_json (SV *scalar) |
2069 | ALIAS: |
|
|
2070 | to_json_ = 0 |
|
|
2071 | encode_json = F_UTF8 |
|
|
2072 | PPCODE: |
2076 | PPCODE: |
2073 | { |
2077 | { |
2074 | JSON json; |
2078 | JSON json; |
2075 | json_init (&json); |
2079 | json_init (&json); |
2076 | json.flags |= ix; |
2080 | json.flags |= F_UTF8; |
2077 | PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
2081 | PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
2078 | XPUSHs (scalar); |
2082 | XPUSHs (scalar); |
2079 | } |
2083 | } |
2080 | |
2084 | |
2081 | void decode_json (SV *jsonstr) |
2085 | void decode_json (SV *jsonstr) |
2082 | ALIAS: |
|
|
2083 | from_json_ = 0 |
|
|
2084 | decode_json = F_UTF8 |
|
|
2085 | PPCODE: |
2086 | PPCODE: |
2086 | { |
2087 | { |
2087 | JSON json; |
2088 | JSON json; |
2088 | json_init (&json); |
2089 | json_init (&json); |
2089 | json.flags |= ix; |
2090 | json.flags |= F_UTF8; |
2090 | PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
2091 | PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
2091 | XPUSHs (jsonstr); |
2092 | XPUSHs (jsonstr); |
2092 | } |
2093 | } |
2093 | |
2094 | |