… | |
… | |
50 | |
50 | |
51 | #define SB do { |
51 | #define SB do { |
52 | #define SE } while (0) |
52 | #define SE } while (0) |
53 | |
53 | |
54 | #if __GNUC__ >= 3 |
54 | #if __GNUC__ >= 3 |
55 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
55 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
56 | # define inline inline |
56 | # define INLINE static inline |
57 | #else |
57 | #else |
58 | # define expect(expr,value) (expr) |
58 | # define expect(expr,value) (expr) |
59 | # define inline static |
59 | # define INLINE static |
60 | #endif |
60 | #endif |
61 | |
61 | |
62 | #define expect_false(expr) expect ((expr) != 0, 0) |
62 | #define expect_false(expr) expect ((expr) != 0, 0) |
63 | #define expect_true(expr) expect ((expr) != 0, 1) |
63 | #define expect_true(expr) expect ((expr) != 0, 1) |
64 | |
64 | |
… | |
… | |
80 | } JSON; |
80 | } JSON; |
81 | |
81 | |
82 | ///////////////////////////////////////////////////////////////////////////// |
82 | ///////////////////////////////////////////////////////////////////////////// |
83 | // utility functions |
83 | // utility functions |
84 | |
84 | |
85 | inline void |
85 | INLINE void |
86 | shrink (SV *sv) |
86 | shrink (SV *sv) |
87 | { |
87 | { |
88 | sv_utf8_downgrade (sv, 1); |
88 | sv_utf8_downgrade (sv, 1); |
89 | if (SvLEN (sv) > SvCUR (sv) + 1) |
89 | if (SvLEN (sv) > SvCUR (sv) + 1) |
90 | { |
90 | { |
… | |
… | |
99 | // decode an utf-8 character and return it, or (UV)-1 in |
99 | // decode an utf-8 character and return it, or (UV)-1 in |
100 | // case of an error. |
100 | // case of an error. |
101 | // we special-case "safe" characters from U+80 .. U+7FF, |
101 | // we special-case "safe" characters from U+80 .. U+7FF, |
102 | // but use the very good perl function to parse anything else. |
102 | // but use the very good perl function to parse anything else. |
103 | // note that we never call this function for a ascii codepoints |
103 | // note that we never call this function for a ascii codepoints |
104 | inline UV |
104 | INLINE UV |
105 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
105 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
106 | { |
106 | { |
107 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
107 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
108 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
108 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
109 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
109 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
… | |
… | |
128 | char *end; // SvEND (sv) |
128 | char *end; // SvEND (sv) |
129 | SV *sv; // result scalar |
129 | SV *sv; // result scalar |
130 | JSON json; |
130 | JSON json; |
131 | U32 indent; // indentation level |
131 | U32 indent; // indentation level |
132 | U32 maxdepth; // max. indentation/recursion level |
132 | U32 maxdepth; // max. indentation/recursion level |
|
|
133 | UV limit; // escape character values >= this value when encoding |
133 | } enc_t; |
134 | } enc_t; |
134 | |
135 | |
135 | inline void |
136 | INLINE void |
136 | need (enc_t *enc, STRLEN len) |
137 | need (enc_t *enc, STRLEN len) |
137 | { |
138 | { |
138 | if (expect_false (enc->cur + len >= enc->end)) |
139 | if (expect_false (enc->cur + len >= enc->end)) |
139 | { |
140 | { |
140 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
141 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
… | |
… | |
142 | enc->cur = SvPVX (enc->sv) + cur; |
143 | enc->cur = SvPVX (enc->sv) + cur; |
143 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
144 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
144 | } |
145 | } |
145 | } |
146 | } |
146 | |
147 | |
147 | inline void |
148 | INLINE void |
148 | encode_ch (enc_t *enc, char ch) |
149 | encode_ch (enc_t *enc, char ch) |
149 | { |
150 | { |
150 | need (enc, 1); |
151 | need (enc, 1); |
151 | *enc->cur++ = ch; |
152 | *enc->cur++ = ch; |
152 | } |
153 | } |
… | |
… | |
206 | { |
207 | { |
207 | uch = ch; |
208 | uch = ch; |
208 | clen = 1; |
209 | clen = 1; |
209 | } |
210 | } |
210 | |
211 | |
211 | if (uch > 0x10FFFFUL) |
212 | if (uch < 0x20 || uch >= enc->limit) |
212 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
213 | |
|
|
214 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
|
|
215 | { |
213 | { |
216 | if (uch > 0xFFFFUL) |
214 | if (uch > 0xFFFFUL) |
217 | { |
215 | { |
|
|
216 | if (uch > 0x10FFFFUL) |
|
|
217 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
218 | |
218 | need (enc, len += 11); |
219 | need (enc, len += 11); |
219 | sprintf (enc->cur, "\\u%04x\\u%04x", |
220 | sprintf (enc->cur, "\\u%04x\\u%04x", |
220 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
221 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
221 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
222 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
222 | enc->cur += 12; |
223 | enc->cur += 12; |
… | |
… | |
261 | |
262 | |
262 | --len; |
263 | --len; |
263 | } |
264 | } |
264 | } |
265 | } |
265 | |
266 | |
266 | inline void |
267 | INLINE void |
267 | encode_indent (enc_t *enc) |
268 | encode_indent (enc_t *enc) |
268 | { |
269 | { |
269 | if (enc->json.flags & F_INDENT) |
270 | if (enc->json.flags & F_INDENT) |
270 | { |
271 | { |
271 | int spaces = enc->indent * INDENT_STEP; |
272 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
274 | memset (enc->cur, ' ', spaces); |
275 | memset (enc->cur, ' ', spaces); |
275 | enc->cur += spaces; |
276 | enc->cur += spaces; |
276 | } |
277 | } |
277 | } |
278 | } |
278 | |
279 | |
279 | inline void |
280 | INLINE void |
280 | encode_space (enc_t *enc) |
281 | encode_space (enc_t *enc) |
281 | { |
282 | { |
282 | need (enc, 1); |
283 | need (enc, 1); |
283 | encode_ch (enc, ' '); |
284 | encode_ch (enc, ' '); |
284 | } |
285 | } |
285 | |
286 | |
286 | inline void |
287 | INLINE void |
287 | encode_nl (enc_t *enc) |
288 | encode_nl (enc_t *enc) |
288 | { |
289 | { |
289 | if (enc->json.flags & F_INDENT) |
290 | if (enc->json.flags & F_INDENT) |
290 | { |
291 | { |
291 | need (enc, 1); |
292 | need (enc, 1); |
292 | encode_ch (enc, '\n'); |
293 | encode_ch (enc, '\n'); |
293 | } |
294 | } |
294 | } |
295 | } |
295 | |
296 | |
296 | inline void |
297 | INLINE void |
297 | encode_comma (enc_t *enc) |
298 | encode_comma (enc_t *enc) |
298 | { |
299 | { |
299 | encode_ch (enc, ','); |
300 | encode_ch (enc, ','); |
300 | |
301 | |
301 | if (enc->json.flags & F_INDENT) |
302 | if (enc->json.flags & F_INDENT) |
… | |
… | |
680 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
681 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
681 | enc.cur = SvPVX (enc.sv); |
682 | enc.cur = SvPVX (enc.sv); |
682 | enc.end = SvEND (enc.sv); |
683 | enc.end = SvEND (enc.sv); |
683 | enc.indent = 0; |
684 | enc.indent = 0; |
684 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
685 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
|
|
686 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
|
|
687 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
|
|
688 | : 0x10FFFFUL; |
685 | |
689 | |
686 | SvPOK_only (enc.sv); |
690 | SvPOK_only (enc.sv); |
687 | encode_sv (&enc, scalar); |
691 | encode_sv (&enc, scalar); |
688 | |
692 | |
689 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
693 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
… | |
… | |
710 | JSON json; |
714 | JSON json; |
711 | U32 depth; // recursion depth |
715 | U32 depth; // recursion depth |
712 | U32 maxdepth; // recursion depth limit |
716 | U32 maxdepth; // recursion depth limit |
713 | } dec_t; |
717 | } dec_t; |
714 | |
718 | |
715 | inline void |
719 | INLINE void |
716 | decode_comment (dec_t *dec) |
720 | decode_comment (dec_t *dec) |
717 | { |
721 | { |
718 | // only '#'-style comments allowed a.t.m. |
722 | // only '#'-style comments allowed a.t.m. |
719 | |
723 | |
720 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
724 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
721 | ++dec->cur; |
725 | ++dec->cur; |
722 | } |
726 | } |
723 | |
727 | |
724 | inline void |
728 | INLINE void |
725 | decode_ws (dec_t *dec) |
729 | decode_ws (dec_t *dec) |
726 | { |
730 | { |
727 | for (;;) |
731 | for (;;) |
728 | { |
732 | { |
729 | char ch = *dec->cur; |
733 | char ch = *dec->cur; |
… | |
… | |
1000 | |
1004 | |
1001 | if (!is_nv) |
1005 | if (!is_nv) |
1002 | { |
1006 | { |
1003 | int len = dec->cur - start; |
1007 | int len = dec->cur - start; |
1004 | |
1008 | |
1005 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
1009 | // special case the rather common 1..5-digit-int case |
1006 | if (*start == '-') |
1010 | if (*start == '-') |
1007 | switch (len) |
1011 | switch (len) |
1008 | { |
1012 | { |
1009 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1013 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1010 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1014 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1011 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1015 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1012 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1016 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1017 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1013 | } |
1018 | } |
1014 | else |
1019 | else |
1015 | switch (len) |
1020 | switch (len) |
1016 | { |
1021 | { |
1017 | case 1: return newSViv ( start [0] - '0' * 1); |
1022 | case 1: return newSViv ( start [0] - '0' * 1); |
1018 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1023 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1019 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1024 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1020 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1025 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1026 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1021 | } |
1027 | } |
1022 | |
1028 | |
1023 | { |
1029 | { |
1024 | UV uv; |
1030 | UV uv; |
1025 | int numtype = grok_number (start, len, &uv); |
1031 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1457 | { |
1463 | { |
1458 | SV *pv = NEWSV (0, sizeof (JSON)); |
1464 | SV *pv = NEWSV (0, sizeof (JSON)); |
1459 | SvPOK_only (pv); |
1465 | SvPOK_only (pv); |
1460 | Zero (SvPVX (pv), 1, JSON); |
1466 | Zero (SvPVX (pv), 1, JSON); |
1461 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1467 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1462 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); |
1468 | XPUSHs (sv_2mortal (sv_bless ( |
|
|
1469 | newRV_noinc (pv), |
|
|
1470 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
|
|
1471 | ))); |
1463 | } |
1472 | } |
1464 | |
1473 | |
1465 | void ascii (JSON *self, int enable = 1) |
1474 | void ascii (JSON *self, int enable = 1) |
1466 | ALIAS: |
1475 | ALIAS: |
1467 | ascii = F_ASCII |
1476 | ascii = F_ASCII |
… | |
… | |
1494 | get_utf8 = F_UTF8 |
1503 | get_utf8 = F_UTF8 |
1495 | get_indent = F_INDENT |
1504 | get_indent = F_INDENT |
1496 | get_canonical = F_CANONICAL |
1505 | get_canonical = F_CANONICAL |
1497 | get_space_before = F_SPACE_BEFORE |
1506 | get_space_before = F_SPACE_BEFORE |
1498 | get_space_after = F_SPACE_AFTER |
1507 | get_space_after = F_SPACE_AFTER |
1499 | get_pretty = F_PRETTY |
|
|
1500 | get_allow_nonref = F_ALLOW_NONREF |
1508 | get_allow_nonref = F_ALLOW_NONREF |
1501 | get_shrink = F_SHRINK |
1509 | get_shrink = F_SHRINK |
1502 | get_allow_blessed = F_ALLOW_BLESSED |
1510 | get_allow_blessed = F_ALLOW_BLESSED |
1503 | get_convert_blessed = F_CONV_BLESSED |
1511 | get_convert_blessed = F_CONV_BLESSED |
1504 | get_relaxed = F_RELAXED |
1512 | get_relaxed = F_RELAXED |
… | |
… | |
1601 | SvREFCNT_dec (self->cb_sk_object); |
1609 | SvREFCNT_dec (self->cb_sk_object); |
1602 | SvREFCNT_dec (self->cb_object); |
1610 | SvREFCNT_dec (self->cb_object); |
1603 | |
1611 | |
1604 | PROTOTYPES: ENABLE |
1612 | PROTOTYPES: ENABLE |
1605 | |
1613 | |
1606 | void to_json (SV *scalar) |
1614 | void encode_json (SV *scalar) |
1607 | PPCODE: |
1615 | PPCODE: |
1608 | { |
1616 | { |
1609 | JSON json = { F_DEFAULT | F_UTF8 }; |
1617 | JSON json = { F_DEFAULT | F_UTF8 }; |
1610 | XPUSHs (encode_json (scalar, &json)); |
1618 | XPUSHs (encode_json (scalar, &json)); |
1611 | } |
1619 | } |
1612 | |
1620 | |
1613 | void from_json (SV *jsonstr) |
1621 | void decode_json (SV *jsonstr) |
1614 | PPCODE: |
1622 | PPCODE: |
1615 | { |
1623 | { |
1616 | JSON json = { F_DEFAULT | F_UTF8 }; |
1624 | JSON json = { F_DEFAULT | F_UTF8 }; |
1617 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1625 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1618 | } |
1626 | } |