… | |
… | |
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 | |
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
|
|
13 | |
|
|
14 | // some old perls do not have this, try to make it work, no |
|
|
15 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
16 | #ifndef UTF8_MAXBYTES |
|
|
17 | # define UTF8_MAXBYTES 13 |
|
|
18 | #endif |
|
|
19 | |
9 | #define F_ASCII 0x00000001 |
20 | #define F_ASCII 0x00000001UL |
|
|
21 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002 |
22 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004 |
23 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008 |
24 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
25 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020 |
26 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
27 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100 |
28 | #define F_SHRINK 0x00000200UL |
|
|
29 | #define F_MAXDEPTH 0xf8000000UL |
|
|
30 | #define S_MAXDEPTH 27 |
|
|
31 | |
|
|
32 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
33 | |
|
|
34 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
35 | // F_BLESSED? <=> { $__class__$ => } |
17 | |
36 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
37 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
38 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
20 | |
39 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
40 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | #define INDENT_STEP 3 // spaces per indentation level |
41 | #define INDENT_STEP 3 // spaces per indentation level |
23 | |
42 | |
24 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
25 | #define SHORT_STRING_LEN 256 // special-case strings of up to this size |
43 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
26 | |
44 | |
27 | #define SB do { |
45 | #define SB do { |
28 | #define SE } while (0) |
46 | #define SE } while (0) |
29 | |
47 | |
|
|
48 | #if __GNUC__ >= 3 |
|
|
49 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
50 | # define inline inline |
|
|
51 | #else |
|
|
52 | # define expect(expr,value) (expr) |
|
|
53 | # define inline static |
|
|
54 | #endif |
|
|
55 | |
|
|
56 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
57 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
58 | |
30 | static HV *json_stash; // JSON::XS:: |
59 | static HV *json_stash; // JSON::XS:: |
|
|
60 | static SV *json_true, *json_false; |
31 | |
61 | |
32 | ///////////////////////////////////////////////////////////////////////////// |
62 | ///////////////////////////////////////////////////////////////////////////// |
33 | // utility functions |
63 | // utility functions |
34 | |
64 | |
35 | static UV * |
65 | static UV * |
… | |
… | |
53 | SvPV_renew (sv, SvCUR (sv) + 1); |
83 | SvPV_renew (sv, SvCUR (sv) + 1); |
54 | #endif |
84 | #endif |
55 | } |
85 | } |
56 | } |
86 | } |
57 | |
87 | |
|
|
88 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
89 | // case of an error. |
|
|
90 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
91 | // but use the very good perl function to parse anything else. |
|
|
92 | // note that we never call this function for a ascii codepoints |
|
|
93 | inline UV |
|
|
94 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
95 | { |
|
|
96 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
|
|
97 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
98 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
99 | { |
|
|
100 | *clen = 2; |
|
|
101 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
102 | } |
|
|
103 | else |
|
|
104 | { |
|
|
105 | *clen = (STRLEN)-1; |
|
|
106 | return (UV)-1; |
|
|
107 | } |
|
|
108 | } |
|
|
109 | |
58 | ///////////////////////////////////////////////////////////////////////////// |
110 | ///////////////////////////////////////////////////////////////////////////// |
59 | // encoder |
111 | // encoder |
60 | |
112 | |
61 | // structure used for encoding JSON |
113 | // structure used for encoding JSON |
62 | typedef struct |
114 | typedef struct |
63 | { |
115 | { |
64 | char *cur; // SvPVX (sv) + current output position |
116 | char *cur; // SvPVX (sv) + current output position |
65 | char *end; // SvEND (sv) |
117 | char *end; // SvEND (sv) |
66 | SV *sv; // result scalar |
118 | SV *sv; // result scalar |
67 | UV flags; // F_* |
119 | U32 flags; // F_* |
68 | int indent; // indentation level |
120 | U32 indent; // indentation level |
69 | int max_depth; // max. recursion level |
121 | U32 maxdepth; // max. indentation/recursion level |
70 | } enc_t; |
122 | } enc_t; |
71 | |
123 | |
72 | static void |
124 | inline void |
73 | need (enc_t *enc, STRLEN len) |
125 | need (enc_t *enc, STRLEN len) |
74 | { |
126 | { |
75 | if (enc->cur + len >= enc->end) |
127 | if (expect_false (enc->cur + len >= enc->end)) |
76 | { |
128 | { |
77 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
129 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
78 | SvGROW (enc->sv, cur + len + 1); |
130 | SvGROW (enc->sv, cur + len + 1); |
79 | enc->cur = SvPVX (enc->sv) + cur; |
131 | enc->cur = SvPVX (enc->sv) + cur; |
80 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
132 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
81 | } |
133 | } |
82 | } |
134 | } |
83 | |
135 | |
84 | static void |
136 | inline void |
85 | encode_ch (enc_t *enc, char ch) |
137 | encode_ch (enc_t *enc, char ch) |
86 | { |
138 | { |
87 | need (enc, 1); |
139 | need (enc, 1); |
88 | *enc->cur++ = ch; |
140 | *enc->cur++ = ch; |
89 | } |
141 | } |
… | |
… | |
97 | |
149 | |
98 | while (str < end) |
150 | while (str < end) |
99 | { |
151 | { |
100 | unsigned char ch = *(unsigned char *)str; |
152 | unsigned char ch = *(unsigned char *)str; |
101 | |
153 | |
102 | if (ch >= 0x20 && ch < 0x80) // most common case |
154 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
103 | { |
155 | { |
104 | if (ch == '"') // but with slow exceptions |
156 | if (expect_false (ch == '"')) // but with slow exceptions |
105 | { |
157 | { |
106 | need (enc, len += 1); |
158 | need (enc, len += 1); |
107 | *enc->cur++ = '\\'; |
159 | *enc->cur++ = '\\'; |
108 | *enc->cur++ = '"'; |
160 | *enc->cur++ = '"'; |
109 | } |
161 | } |
110 | else if (ch == '\\') |
162 | else if (expect_false (ch == '\\')) |
111 | { |
163 | { |
112 | need (enc, len += 1); |
164 | need (enc, len += 1); |
113 | *enc->cur++ = '\\'; |
165 | *enc->cur++ = '\\'; |
114 | *enc->cur++ = '\\'; |
166 | *enc->cur++ = '\\'; |
115 | } |
167 | } |
… | |
… | |
133 | STRLEN clen; |
185 | STRLEN clen; |
134 | UV uch; |
186 | UV uch; |
135 | |
187 | |
136 | if (is_utf8) |
188 | if (is_utf8) |
137 | { |
189 | { |
138 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
190 | uch = decode_utf8 (str, end - str, &clen); |
139 | if (clen == (STRLEN)-1) |
191 | if (clen == (STRLEN)-1) |
140 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
192 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
141 | } |
193 | } |
142 | else |
194 | else |
143 | { |
195 | { |
… | |
… | |
146 | } |
198 | } |
147 | |
199 | |
148 | if (uch > 0x10FFFFUL) |
200 | if (uch > 0x10FFFFUL) |
149 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
201 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
150 | |
202 | |
151 | if (uch < 0x80 || enc->flags & F_ASCII) |
203 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
152 | { |
204 | { |
153 | if (uch > 0xFFFFUL) |
205 | if (uch > 0xFFFFUL) |
154 | { |
206 | { |
155 | need (enc, len += 11); |
207 | need (enc, len += 11); |
156 | sprintf (enc->cur, "\\u%04x\\u%04x", |
208 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
170 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
222 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
171 | } |
223 | } |
172 | |
224 | |
173 | str += clen; |
225 | str += clen; |
174 | } |
226 | } |
|
|
227 | else if (enc->flags & F_LATIN1) |
|
|
228 | { |
|
|
229 | *enc->cur++ = uch; |
|
|
230 | str += clen; |
|
|
231 | } |
175 | else if (is_utf8) |
232 | else if (is_utf8) |
176 | { |
233 | { |
177 | need (enc, len += clen); |
234 | need (enc, len += clen); |
178 | do |
235 | do |
179 | { |
236 | { |
… | |
… | |
181 | } |
238 | } |
182 | while (--clen); |
239 | while (--clen); |
183 | } |
240 | } |
184 | else |
241 | else |
185 | { |
242 | { |
186 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
243 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
187 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
244 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
188 | ++str; |
245 | ++str; |
189 | } |
246 | } |
190 | } |
247 | } |
191 | } |
248 | } |
… | |
… | |
193 | |
250 | |
194 | --len; |
251 | --len; |
195 | } |
252 | } |
196 | } |
253 | } |
197 | |
254 | |
198 | static void |
255 | inline void |
199 | encode_indent (enc_t *enc) |
256 | encode_indent (enc_t *enc) |
200 | { |
257 | { |
201 | if (enc->flags & F_INDENT) |
258 | if (enc->flags & F_INDENT) |
202 | { |
259 | { |
203 | int spaces = enc->indent * INDENT_STEP; |
260 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
206 | memset (enc->cur, ' ', spaces); |
263 | memset (enc->cur, ' ', spaces); |
207 | enc->cur += spaces; |
264 | enc->cur += spaces; |
208 | } |
265 | } |
209 | } |
266 | } |
210 | |
267 | |
211 | static void |
268 | inline void |
212 | encode_space (enc_t *enc) |
269 | encode_space (enc_t *enc) |
213 | { |
270 | { |
214 | need (enc, 1); |
271 | need (enc, 1); |
215 | encode_ch (enc, ' '); |
272 | encode_ch (enc, ' '); |
216 | } |
273 | } |
217 | |
274 | |
218 | static void |
275 | inline void |
219 | encode_nl (enc_t *enc) |
276 | encode_nl (enc_t *enc) |
220 | { |
277 | { |
221 | if (enc->flags & F_INDENT) |
278 | if (enc->flags & F_INDENT) |
222 | { |
279 | { |
223 | need (enc, 1); |
280 | need (enc, 1); |
224 | encode_ch (enc, '\n'); |
281 | encode_ch (enc, '\n'); |
225 | } |
282 | } |
226 | } |
283 | } |
227 | |
284 | |
228 | static void |
285 | inline void |
229 | encode_comma (enc_t *enc) |
286 | encode_comma (enc_t *enc) |
230 | { |
287 | { |
231 | encode_ch (enc, ','); |
288 | encode_ch (enc, ','); |
232 | |
289 | |
233 | if (enc->flags & F_INDENT) |
290 | if (enc->flags & F_INDENT) |
… | |
… | |
240 | |
297 | |
241 | static void |
298 | static void |
242 | encode_av (enc_t *enc, AV *av) |
299 | encode_av (enc_t *enc, AV *av) |
243 | { |
300 | { |
244 | int i, len = av_len (av); |
301 | int i, len = av_len (av); |
|
|
302 | |
|
|
303 | if (enc->indent >= enc->maxdepth) |
|
|
304 | croak ("data structure too deep (hit recursion limit)"); |
245 | |
305 | |
246 | encode_ch (enc, '['); encode_nl (enc); |
306 | encode_ch (enc, '['); encode_nl (enc); |
247 | ++enc->indent; |
307 | ++enc->indent; |
248 | |
308 | |
249 | for (i = 0; i <= len; ++i) |
309 | for (i = 0; i <= len; ++i) |
… | |
… | |
316 | static void |
376 | static void |
317 | encode_hv (enc_t *enc, HV *hv) |
377 | encode_hv (enc_t *enc, HV *hv) |
318 | { |
378 | { |
319 | int count, i; |
379 | int count, i; |
320 | |
380 | |
|
|
381 | if (enc->indent >= enc->maxdepth) |
|
|
382 | croak ("data structure too deep (hit recursion limit)"); |
|
|
383 | |
321 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
384 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
322 | |
385 | |
323 | if ((count = hv_iterinit (hv))) |
386 | if ((count = hv_iterinit (hv))) |
324 | { |
387 | { |
325 | // for canonical output we have to sort by keys first |
388 | // for canonical output we have to sort by keys first |
326 | // actually, this is mostly due to the stupid so-called |
389 | // actually, this is mostly due to the stupid so-called |
327 | // security workaround added somewhere in 5.8.x. |
390 | // security workaround added somewhere in 5.8.x. |
328 | // that randomises hash orderings |
391 | // that randomises hash orderings |
329 | if (enc->flags & F_CANONICAL) |
392 | if (enc->flags & F_CANONICAL) |
330 | { |
393 | { |
331 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
332 | int fast = 1; |
394 | int fast = 1; |
|
|
395 | HE *he; |
|
|
396 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
397 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
398 | #else |
|
|
399 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
400 | #endif |
333 | |
401 | |
334 | i = 0; |
402 | i = 0; |
335 | while ((he = hv_iternext (hv))) |
403 | while ((he = hv_iternext (hv))) |
336 | { |
404 | { |
337 | hes [i++] = he; |
405 | hes [i++] = he; |
… | |
… | |
372 | |
440 | |
373 | encode_nl (enc); |
441 | encode_nl (enc); |
374 | } |
442 | } |
375 | else |
443 | else |
376 | { |
444 | { |
377 | SV *sv; |
|
|
378 | HE *he = hv_iternext (hv); |
445 | HE *he = hv_iternext (hv); |
379 | |
446 | |
380 | for (;;) |
447 | for (;;) |
381 | { |
448 | { |
382 | encode_indent (enc); |
449 | encode_indent (enc); |
… | |
… | |
391 | encode_nl (enc); |
458 | encode_nl (enc); |
392 | } |
459 | } |
393 | } |
460 | } |
394 | |
461 | |
395 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
462 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
463 | } |
|
|
464 | |
|
|
465 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
466 | static void |
|
|
467 | encode_rv (enc_t *enc, SV *sv) |
|
|
468 | { |
|
|
469 | svtype svt; |
|
|
470 | |
|
|
471 | SvGETMAGIC (sv); |
|
|
472 | svt = SvTYPE (sv); |
|
|
473 | |
|
|
474 | if (svt == SVt_PVHV) |
|
|
475 | encode_hv (enc, (HV *)sv); |
|
|
476 | else if (svt == SVt_PVAV) |
|
|
477 | encode_av (enc, (AV *)sv); |
|
|
478 | else if (svt < SVt_PVAV) |
|
|
479 | { |
|
|
480 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
481 | encode_str (enc, "false", 5, 0); |
|
|
482 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
483 | encode_str (enc, "true", 4, 0); |
|
|
484 | else |
|
|
485 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
486 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
487 | } |
|
|
488 | else |
|
|
489 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
490 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
396 | } |
491 | } |
397 | |
492 | |
398 | static void |
493 | static void |
399 | encode_sv (enc_t *enc, SV *sv) |
494 | encode_sv (enc_t *enc, SV *sv) |
400 | { |
495 | { |
… | |
… | |
408 | encode_str (enc, str, len, SvUTF8 (sv)); |
503 | encode_str (enc, str, len, SvUTF8 (sv)); |
409 | encode_ch (enc, '"'); |
504 | encode_ch (enc, '"'); |
410 | } |
505 | } |
411 | else if (SvNOKp (sv)) |
506 | else if (SvNOKp (sv)) |
412 | { |
507 | { |
|
|
508 | // trust that perl will do the right thing w.r.t. JSON syntax. |
413 | need (enc, NV_DIG + 32); |
509 | need (enc, NV_DIG + 32); |
414 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
510 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
415 | enc->cur += strlen (enc->cur); |
511 | enc->cur += strlen (enc->cur); |
416 | } |
512 | } |
417 | else if (SvIOKp (sv)) |
513 | else if (SvIOKp (sv)) |
418 | { |
514 | { |
419 | need (enc, 64); |
515 | // we assume we can always read an IV as a UV |
|
|
516 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
517 | { |
|
|
518 | // large integer, use the (rather slow) snprintf way. |
|
|
519 | need (enc, sizeof (UV) * 3); |
420 | enc->cur += |
520 | enc->cur += |
421 | SvIsUV(sv) |
521 | SvIsUV(sv) |
422 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
522 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
423 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
523 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
524 | } |
|
|
525 | else |
|
|
526 | { |
|
|
527 | // optimise the "small number case" |
|
|
528 | // code will likely be branchless and use only a single multiplication |
|
|
529 | I32 i = SvIV (sv); |
|
|
530 | U32 u; |
|
|
531 | char digit, nz = 0; |
|
|
532 | |
|
|
533 | need (enc, 6); |
|
|
534 | |
|
|
535 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
536 | u = i < 0 ? -i : i; |
|
|
537 | |
|
|
538 | // convert to 4.28 fixed-point representation |
|
|
539 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
540 | |
|
|
541 | // now output digit by digit, each time masking out the integer part |
|
|
542 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
543 | // resulting in a net multiplication by 10. |
|
|
544 | // we always write the digit to memory but conditionally increment |
|
|
545 | // the pointer, to ease the usage of conditional move instructions. |
|
|
546 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
547 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
548 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
549 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
550 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
551 | } |
424 | } |
552 | } |
425 | else if (SvROK (sv)) |
553 | else if (SvROK (sv)) |
426 | { |
554 | encode_rv (enc, SvRV (sv)); |
427 | SV *rv = SvRV (sv); |
|
|
428 | |
|
|
429 | if (enc->indent >= enc->max_depth) |
|
|
430 | croak ("data structure too deep (hit recursion limit)"); |
|
|
431 | |
|
|
432 | switch (SvTYPE (rv)) |
|
|
433 | { |
|
|
434 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
435 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
436 | |
|
|
437 | default: |
|
|
438 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
439 | SvPV_nolen (sv)); |
|
|
440 | } |
|
|
441 | } |
|
|
442 | else if (!SvOK (sv)) |
555 | else if (!SvOK (sv)) |
443 | encode_str (enc, "null", 4, 0); |
556 | encode_str (enc, "null", 4, 0); |
444 | else |
557 | else |
445 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
558 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
446 | SvPV_nolen (sv), SvFLAGS (sv)); |
559 | SvPV_nolen (sv), SvFLAGS (sv)); |
447 | } |
560 | } |
448 | |
561 | |
449 | static SV * |
562 | static SV * |
450 | encode_json (SV *scalar, UV flags) |
563 | encode_json (SV *scalar, U32 flags) |
451 | { |
564 | { |
|
|
565 | enc_t enc; |
|
|
566 | |
452 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
567 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
453 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
568 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
454 | |
569 | |
455 | enc_t enc; |
|
|
456 | enc.flags = flags; |
570 | enc.flags = flags; |
457 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
571 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
458 | enc.cur = SvPVX (enc.sv); |
572 | enc.cur = SvPVX (enc.sv); |
459 | enc.end = SvEND (enc.sv); |
573 | enc.end = SvEND (enc.sv); |
460 | enc.indent = 0; |
574 | enc.indent = 0; |
461 | enc.max_depth = 0x7fffffffUL; |
575 | enc.maxdepth = DEC_DEPTH (flags); |
462 | |
576 | |
463 | SvPOK_only (enc.sv); |
577 | SvPOK_only (enc.sv); |
464 | encode_sv (&enc, scalar); |
578 | encode_sv (&enc, scalar); |
465 | |
579 | |
|
|
580 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
581 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
582 | |
466 | if (!(flags & (F_ASCII | F_UTF8))) |
583 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
467 | SvUTF8_on (enc.sv); |
584 | SvUTF8_on (enc.sv); |
468 | |
|
|
469 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
470 | |
585 | |
471 | if (enc.flags & F_SHRINK) |
586 | if (enc.flags & F_SHRINK) |
472 | shrink (enc.sv); |
587 | shrink (enc.sv); |
473 | |
588 | |
474 | return enc.sv; |
589 | return enc.sv; |
… | |
… | |
481 | typedef struct |
596 | typedef struct |
482 | { |
597 | { |
483 | char *cur; // current parser pointer |
598 | char *cur; // current parser pointer |
484 | char *end; // end of input string |
599 | char *end; // end of input string |
485 | const char *err; // parse error, if != 0 |
600 | const char *err; // parse error, if != 0 |
486 | UV flags; // F_* |
601 | U32 flags; // F_* |
|
|
602 | U32 depth; // recursion depth |
|
|
603 | U32 maxdepth; // recursion depth limit |
487 | } dec_t; |
604 | } dec_t; |
488 | |
605 | |
489 | static void |
606 | inline void |
490 | decode_ws (dec_t *dec) |
607 | decode_ws (dec_t *dec) |
491 | { |
608 | { |
492 | for (;;) |
609 | for (;;) |
493 | { |
610 | { |
494 | char ch = *dec->cur; |
611 | char ch = *dec->cur; |
… | |
… | |
500 | ++dec->cur; |
617 | ++dec->cur; |
501 | } |
618 | } |
502 | } |
619 | } |
503 | |
620 | |
504 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
621 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
622 | |
505 | #define EXPECT_CH(ch) SB \ |
623 | #define EXPECT_CH(ch) SB \ |
506 | if (*dec->cur != ch) \ |
624 | if (*dec->cur != ch) \ |
507 | ERR (# ch " expected"); \ |
625 | ERR (# ch " expected"); \ |
508 | ++dec->cur; \ |
626 | ++dec->cur; \ |
509 | SE |
627 | SE |
510 | |
628 | |
|
|
629 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
630 | #define DEC_DEC_DEPTH --dec->depth |
|
|
631 | |
511 | static SV *decode_sv (dec_t *dec); |
632 | static SV *decode_sv (dec_t *dec); |
512 | |
633 | |
513 | static signed char decode_hexdigit[256]; |
634 | static signed char decode_hexdigit[256]; |
514 | |
635 | |
515 | static UV |
636 | static UV |
516 | decode_4hex (dec_t *dec) |
637 | decode_4hex (dec_t *dec) |
517 | { |
638 | { |
518 | signed char d1, d2, d3, d4; |
639 | signed char d1, d2, d3, d4; |
519 | unsigned char *cur = (unsigned char *)dec->cur; |
640 | unsigned char *cur = (unsigned char *)dec->cur; |
520 | |
641 | |
521 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
642 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
522 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
643 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
523 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
644 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
524 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
645 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
525 | |
646 | |
526 | dec->cur += 4; |
647 | dec->cur += 4; |
527 | |
648 | |
528 | return ((UV)d1) << 12 |
649 | return ((UV)d1) << 12 |
529 | | ((UV)d2) << 8 |
650 | | ((UV)d2) << 8 |
… | |
… | |
537 | static SV * |
658 | static SV * |
538 | decode_str (dec_t *dec) |
659 | decode_str (dec_t *dec) |
539 | { |
660 | { |
540 | SV *sv = 0; |
661 | SV *sv = 0; |
541 | int utf8 = 0; |
662 | int utf8 = 0; |
|
|
663 | char *dec_cur = dec->cur; |
542 | |
664 | |
543 | do |
665 | do |
544 | { |
666 | { |
545 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
667 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
546 | char *cur = buf; |
668 | char *cur = buf; |
547 | |
669 | |
548 | do |
670 | do |
549 | { |
671 | { |
550 | unsigned char ch = *(unsigned char *)dec->cur++; |
672 | unsigned char ch = *(unsigned char *)dec_cur++; |
551 | |
673 | |
552 | if (ch == '"') |
674 | if (expect_false (ch == '"')) |
553 | { |
675 | { |
554 | --dec->cur; |
676 | --dec_cur; |
555 | break; |
677 | break; |
556 | } |
678 | } |
557 | else if (ch == '\\') |
679 | else if (expect_false (ch == '\\')) |
558 | { |
680 | { |
559 | switch (*dec->cur) |
681 | switch (*dec_cur) |
560 | { |
682 | { |
561 | case '\\': |
683 | case '\\': |
562 | case '/': |
684 | case '/': |
563 | case '"': *cur++ = *dec->cur++; break; |
685 | case '"': *cur++ = *dec_cur++; break; |
564 | |
686 | |
565 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
687 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
566 | case 't': ++dec->cur; *cur++ = '\011'; break; |
688 | case 't': ++dec_cur; *cur++ = '\011'; break; |
567 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
689 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
568 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
690 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
569 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
691 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
570 | |
692 | |
571 | case 'u': |
693 | case 'u': |
572 | { |
694 | { |
573 | UV lo, hi; |
695 | UV lo, hi; |
574 | ++dec->cur; |
696 | ++dec_cur; |
575 | |
697 | |
|
|
698 | dec->cur = dec_cur; |
576 | hi = decode_4hex (dec); |
699 | hi = decode_4hex (dec); |
|
|
700 | dec_cur = dec->cur; |
577 | if (hi == (UV)-1) |
701 | if (hi == (UV)-1) |
578 | goto fail; |
702 | goto fail; |
579 | |
703 | |
580 | // possibly a surrogate pair |
704 | // possibly a surrogate pair |
581 | if (hi >= 0xd800) |
705 | if (hi >= 0xd800) |
582 | if (hi < 0xdc00) |
706 | if (hi < 0xdc00) |
583 | { |
707 | { |
584 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
708 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
585 | ERR ("missing low surrogate character in surrogate pair"); |
709 | ERR ("missing low surrogate character in surrogate pair"); |
586 | |
710 | |
587 | dec->cur += 2; |
711 | dec_cur += 2; |
588 | |
712 | |
|
|
713 | dec->cur = dec_cur; |
589 | lo = decode_4hex (dec); |
714 | lo = decode_4hex (dec); |
|
|
715 | dec_cur = dec->cur; |
590 | if (lo == (UV)-1) |
716 | if (lo == (UV)-1) |
591 | goto fail; |
717 | goto fail; |
592 | |
718 | |
593 | if (lo < 0xdc00 || lo >= 0xe000) |
719 | if (lo < 0xdc00 || lo >= 0xe000) |
594 | ERR ("surrogate pair expected"); |
720 | ERR ("surrogate pair expected"); |
… | |
… | |
608 | *cur++ = hi; |
734 | *cur++ = hi; |
609 | } |
735 | } |
610 | break; |
736 | break; |
611 | |
737 | |
612 | default: |
738 | default: |
613 | --dec->cur; |
739 | --dec_cur; |
614 | ERR ("illegal backslash escape sequence in string"); |
740 | ERR ("illegal backslash escape sequence in string"); |
615 | } |
741 | } |
616 | } |
742 | } |
617 | else if (ch >= 0x20 && ch <= 0x7f) |
743 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
618 | *cur++ = ch; |
744 | *cur++ = ch; |
619 | else if (ch >= 0x80) |
745 | else if (ch >= 0x80) |
620 | { |
746 | { |
621 | --dec->cur; |
|
|
622 | |
|
|
623 | STRLEN clen; |
747 | STRLEN clen; |
624 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
748 | UV uch; |
|
|
749 | |
|
|
750 | --dec_cur; |
|
|
751 | |
|
|
752 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
625 | if (clen == (STRLEN)-1) |
753 | if (clen == (STRLEN)-1) |
626 | ERR ("malformed UTF-8 character in JSON string"); |
754 | ERR ("malformed UTF-8 character in JSON string"); |
627 | |
755 | |
628 | do |
756 | do |
629 | { |
|
|
630 | *cur++ = *dec->cur++; |
757 | *cur++ = *dec_cur++; |
631 | } |
|
|
632 | while (--clen); |
758 | while (--clen); |
633 | |
759 | |
634 | utf8 = 1; |
760 | utf8 = 1; |
635 | } |
761 | } |
636 | else if (!ch) |
|
|
637 | ERR ("unexpected end of string while parsing json string"); |
|
|
638 | else |
762 | else |
|
|
763 | { |
|
|
764 | --dec_cur; |
|
|
765 | |
|
|
766 | if (!ch) |
|
|
767 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
768 | else |
639 | ERR ("invalid character encountered"); |
769 | ERR ("invalid character encountered while parsing JSON string"); |
640 | |
770 | } |
641 | } |
771 | } |
642 | while (cur < buf + SHORT_STRING_LEN); |
772 | while (cur < buf + SHORT_STRING_LEN); |
643 | |
773 | |
|
|
774 | { |
644 | STRLEN len = cur - buf; |
775 | STRLEN len = cur - buf; |
645 | |
776 | |
646 | if (sv) |
777 | if (sv) |
647 | { |
778 | { |
648 | SvGROW (sv, SvCUR (sv) + len + 1); |
779 | SvGROW (sv, SvCUR (sv) + len + 1); |
649 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
780 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
650 | SvCUR_set (sv, SvCUR (sv) + len); |
781 | SvCUR_set (sv, SvCUR (sv) + len); |
651 | } |
782 | } |
652 | else |
783 | else |
653 | sv = newSVpvn (buf, len); |
784 | sv = newSVpvn (buf, len); |
654 | } |
785 | } |
|
|
786 | } |
655 | while (*dec->cur != '"'); |
787 | while (*dec_cur != '"'); |
656 | |
788 | |
657 | ++dec->cur; |
789 | ++dec_cur; |
658 | |
790 | |
659 | if (sv) |
791 | if (sv) |
660 | { |
792 | { |
661 | SvPOK_only (sv); |
793 | SvPOK_only (sv); |
662 | *SvEND (sv) = 0; |
794 | *SvEND (sv) = 0; |
… | |
… | |
665 | SvUTF8_on (sv); |
797 | SvUTF8_on (sv); |
666 | } |
798 | } |
667 | else |
799 | else |
668 | sv = newSVpvn ("", 0); |
800 | sv = newSVpvn ("", 0); |
669 | |
801 | |
|
|
802 | dec->cur = dec_cur; |
670 | return sv; |
803 | return sv; |
671 | |
804 | |
672 | fail: |
805 | fail: |
|
|
806 | dec->cur = dec_cur; |
673 | return 0; |
807 | return 0; |
674 | } |
808 | } |
675 | |
809 | |
676 | static SV * |
810 | static SV * |
677 | decode_num (dec_t *dec) |
811 | decode_num (dec_t *dec) |
… | |
… | |
735 | is_nv = 1; |
869 | is_nv = 1; |
736 | } |
870 | } |
737 | |
871 | |
738 | if (!is_nv) |
872 | if (!is_nv) |
739 | { |
873 | { |
740 | UV uv; |
874 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
741 | int numtype = grok_number (start, dec->cur - start, &uv); |
875 | if (*start == '-') |
742 | if (numtype & IS_NUMBER_IN_UV) |
876 | switch (dec->cur - start) |
743 | if (numtype & IS_NUMBER_NEG) |
|
|
744 | { |
877 | { |
745 | if (uv < (UV)IV_MIN) |
878 | case 2: return newSViv (-( start [1] - '0' * 1)); |
746 | return newSViv (-(IV)uv); |
879 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
880 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
881 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
747 | } |
882 | } |
|
|
883 | else |
|
|
884 | switch (dec->cur - start) |
|
|
885 | { |
|
|
886 | case 1: return newSViv ( start [0] - '0' * 1); |
|
|
887 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
888 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
889 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
890 | } |
|
|
891 | |
|
|
892 | { |
|
|
893 | UV uv; |
|
|
894 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
895 | if (numtype & IS_NUMBER_IN_UV) |
|
|
896 | if (numtype & IS_NUMBER_NEG) |
|
|
897 | { |
|
|
898 | if (uv < (UV)IV_MIN) |
|
|
899 | return newSViv (-(IV)uv); |
|
|
900 | } |
748 | else |
901 | else |
749 | return newSVuv (uv); |
902 | return newSVuv (uv); |
|
|
903 | |
|
|
904 | // here would likely be the place for bigint support |
750 | } |
905 | } |
|
|
906 | } |
751 | |
907 | |
|
|
908 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
752 | return newSVnv (Atof (start)); |
909 | return newSVnv (Atof (start)); |
753 | |
910 | |
754 | fail: |
911 | fail: |
755 | return 0; |
912 | return 0; |
756 | } |
913 | } |
… | |
… | |
758 | static SV * |
915 | static SV * |
759 | decode_av (dec_t *dec) |
916 | decode_av (dec_t *dec) |
760 | { |
917 | { |
761 | AV *av = newAV (); |
918 | AV *av = newAV (); |
762 | |
919 | |
|
|
920 | DEC_INC_DEPTH; |
763 | decode_ws (dec); |
921 | decode_ws (dec); |
|
|
922 | |
764 | if (*dec->cur == ']') |
923 | if (*dec->cur == ']') |
765 | ++dec->cur; |
924 | ++dec->cur; |
766 | else |
925 | else |
767 | for (;;) |
926 | for (;;) |
768 | { |
927 | { |
… | |
… | |
786 | ERR (", or ] expected while parsing array"); |
945 | ERR (", or ] expected while parsing array"); |
787 | |
946 | |
788 | ++dec->cur; |
947 | ++dec->cur; |
789 | } |
948 | } |
790 | |
949 | |
|
|
950 | DEC_DEC_DEPTH; |
791 | return newRV_noinc ((SV *)av); |
951 | return newRV_noinc ((SV *)av); |
792 | |
952 | |
793 | fail: |
953 | fail: |
794 | SvREFCNT_dec (av); |
954 | SvREFCNT_dec (av); |
|
|
955 | DEC_DEC_DEPTH; |
795 | return 0; |
956 | return 0; |
796 | } |
957 | } |
797 | |
958 | |
798 | static SV * |
959 | static SV * |
799 | decode_hv (dec_t *dec) |
960 | decode_hv (dec_t *dec) |
800 | { |
961 | { |
801 | HV *hv = newHV (); |
962 | HV *hv = newHV (); |
802 | |
963 | |
|
|
964 | DEC_INC_DEPTH; |
803 | decode_ws (dec); |
965 | decode_ws (dec); |
|
|
966 | |
804 | if (*dec->cur == '}') |
967 | if (*dec->cur == '}') |
805 | ++dec->cur; |
968 | ++dec->cur; |
806 | else |
969 | else |
807 | for (;;) |
970 | for (;;) |
808 | { |
971 | { |
… | |
… | |
821 | { |
984 | { |
822 | SvREFCNT_dec (key); |
985 | SvREFCNT_dec (key); |
823 | goto fail; |
986 | goto fail; |
824 | } |
987 | } |
825 | |
988 | |
826 | //TODO: optimise |
|
|
827 | hv_store_ent (hv, key, value, 0); |
989 | hv_store_ent (hv, key, value, 0); |
|
|
990 | SvREFCNT_dec (key); |
828 | |
991 | |
829 | decode_ws (dec); |
992 | decode_ws (dec); |
830 | |
993 | |
831 | if (*dec->cur == '}') |
994 | if (*dec->cur == '}') |
832 | { |
995 | { |
… | |
… | |
838 | ERR (", or } expected while parsing object/hash"); |
1001 | ERR (", or } expected while parsing object/hash"); |
839 | |
1002 | |
840 | ++dec->cur; |
1003 | ++dec->cur; |
841 | } |
1004 | } |
842 | |
1005 | |
|
|
1006 | DEC_DEC_DEPTH; |
843 | return newRV_noinc ((SV *)hv); |
1007 | return newRV_noinc ((SV *)hv); |
844 | |
1008 | |
845 | fail: |
1009 | fail: |
846 | SvREFCNT_dec (hv); |
1010 | SvREFCNT_dec (hv); |
|
|
1011 | DEC_DEC_DEPTH; |
847 | return 0; |
1012 | return 0; |
848 | } |
1013 | } |
849 | |
1014 | |
850 | static SV * |
1015 | static SV * |
851 | decode_sv (dec_t *dec) |
1016 | decode_sv (dec_t *dec) |
852 | { |
1017 | { |
853 | decode_ws (dec); |
1018 | decode_ws (dec); |
|
|
1019 | |
|
|
1020 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1021 | // to parse anything. |
854 | switch (*dec->cur) |
1022 | switch (*dec->cur) |
855 | { |
1023 | { |
856 | case '"': ++dec->cur; return decode_str (dec); |
1024 | case '"': ++dec->cur; return decode_str (dec); |
857 | case '[': ++dec->cur; return decode_av (dec); |
1025 | case '[': ++dec->cur; return decode_av (dec); |
858 | case '{': ++dec->cur; return decode_hv (dec); |
1026 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
864 | |
1032 | |
865 | case 't': |
1033 | case 't': |
866 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1034 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
867 | { |
1035 | { |
868 | dec->cur += 4; |
1036 | dec->cur += 4; |
869 | return newSViv (1); |
1037 | return SvREFCNT_inc (json_true); |
870 | } |
1038 | } |
871 | else |
1039 | else |
872 | ERR ("'true' expected"); |
1040 | ERR ("'true' expected"); |
873 | |
1041 | |
874 | break; |
1042 | break; |
875 | |
1043 | |
876 | case 'f': |
1044 | case 'f': |
877 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1045 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
878 | { |
1046 | { |
879 | dec->cur += 5; |
1047 | dec->cur += 5; |
880 | return newSViv (0); |
1048 | return SvREFCNT_inc (json_false); |
881 | } |
1049 | } |
882 | else |
1050 | else |
883 | ERR ("'false' expected"); |
1051 | ERR ("'false' expected"); |
884 | |
1052 | |
885 | break; |
1053 | break; |
… | |
… | |
894 | ERR ("'null' expected"); |
1062 | ERR ("'null' expected"); |
895 | |
1063 | |
896 | break; |
1064 | break; |
897 | |
1065 | |
898 | default: |
1066 | default: |
899 | ERR ("malformed json string, neither array, object, number, string or atom"); |
1067 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
900 | break; |
1068 | break; |
901 | } |
1069 | } |
902 | |
1070 | |
903 | fail: |
1071 | fail: |
904 | return 0; |
1072 | return 0; |
905 | } |
1073 | } |
906 | |
1074 | |
907 | static SV * |
1075 | static SV * |
908 | decode_json (SV *string, UV flags) |
1076 | decode_json (SV *string, U32 flags, UV *offset_return) |
909 | { |
1077 | { |
|
|
1078 | dec_t dec; |
|
|
1079 | UV offset; |
910 | SV *sv; |
1080 | SV *sv; |
|
|
1081 | |
|
|
1082 | SvGETMAGIC (string); |
|
|
1083 | SvUPGRADE (string, SVt_PV); |
911 | |
1084 | |
912 | if (flags & F_UTF8) |
1085 | if (flags & F_UTF8) |
913 | sv_utf8_downgrade (string, 0); |
1086 | sv_utf8_downgrade (string, 0); |
914 | else |
1087 | else |
915 | sv_utf8_upgrade (string); |
1088 | sv_utf8_upgrade (string); |
916 | |
1089 | |
917 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1090 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
918 | |
1091 | |
919 | dec_t dec; |
|
|
920 | dec.flags = flags; |
1092 | dec.flags = flags; |
921 | dec.cur = SvPVX (string); |
1093 | dec.cur = SvPVX (string); |
922 | dec.end = SvEND (string); |
1094 | dec.end = SvEND (string); |
923 | dec.err = 0; |
1095 | dec.err = 0; |
|
|
1096 | dec.depth = 0; |
|
|
1097 | dec.maxdepth = DEC_DEPTH (dec.flags); |
924 | |
1098 | |
|
|
1099 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
925 | sv = decode_sv (&dec); |
1100 | sv = decode_sv (&dec); |
926 | |
1101 | |
|
|
1102 | if (!(offset_return || !sv)) |
|
|
1103 | { |
|
|
1104 | // check for trailing garbage |
|
|
1105 | decode_ws (&dec); |
|
|
1106 | |
|
|
1107 | if (*dec.cur) |
|
|
1108 | { |
|
|
1109 | dec.err = "garbage after JSON object"; |
|
|
1110 | SvREFCNT_dec (sv); |
|
|
1111 | sv = 0; |
|
|
1112 | } |
|
|
1113 | } |
|
|
1114 | |
|
|
1115 | if (offset_return || !sv) |
|
|
1116 | { |
|
|
1117 | offset = dec.flags & F_UTF8 |
|
|
1118 | ? dec.cur - SvPVX (string) |
|
|
1119 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1120 | |
|
|
1121 | if (offset_return) |
|
|
1122 | *offset_return = offset; |
|
|
1123 | } |
|
|
1124 | |
927 | if (!sv) |
1125 | if (!sv) |
928 | { |
1126 | { |
929 | IV offset = dec.flags & F_UTF8 |
|
|
930 | ? dec.cur - SvPVX (string) |
|
|
931 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
932 | SV *uni = sv_newmortal (); |
1127 | SV *uni = sv_newmortal (); |
933 | |
1128 | |
934 | // horrible hack to silence warning inside pv_uni_display |
1129 | // horrible hack to silence warning inside pv_uni_display |
935 | COP cop = *PL_curcop; |
1130 | COP cop = *PL_curcop; |
936 | cop.cop_warnings = pWARN_NONE; |
1131 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
938 | SAVEVPTR (PL_curcop); |
1133 | SAVEVPTR (PL_curcop); |
939 | PL_curcop = &cop; |
1134 | PL_curcop = &cop; |
940 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1135 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
941 | LEAVE; |
1136 | LEAVE; |
942 | |
1137 | |
943 | croak ("%s, at character offset %d (%s)", |
1138 | croak ("%s, at character offset %d [\"%s\"]", |
944 | dec.err, |
1139 | dec.err, |
945 | (int)offset, |
1140 | (int)offset, |
946 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1141 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
947 | } |
1142 | } |
948 | |
1143 | |
… | |
… | |
961 | |
1156 | |
962 | BOOT: |
1157 | BOOT: |
963 | { |
1158 | { |
964 | int i; |
1159 | int i; |
965 | |
1160 | |
966 | memset (decode_hexdigit, 0xff, 256); |
|
|
967 | for (i = 10; i--; ) |
1161 | for (i = 0; i < 256; ++i) |
968 | decode_hexdigit ['0' + i] = i; |
1162 | decode_hexdigit [i] = |
969 | |
1163 | i >= '0' && i <= '9' ? i - '0' |
970 | for (i = 7; i--; ) |
1164 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
971 | { |
1165 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
972 | decode_hexdigit ['a' + i] = 10 + i; |
1166 | : -1; |
973 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
974 | } |
|
|
975 | |
1167 | |
976 | json_stash = gv_stashpv ("JSON::XS", 1); |
1168 | json_stash = gv_stashpv ("JSON::XS", 1); |
|
|
1169 | |
|
|
1170 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1171 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
977 | } |
1172 | } |
978 | |
1173 | |
979 | PROTOTYPES: DISABLE |
1174 | PROTOTYPES: DISABLE |
980 | |
1175 | |
981 | SV *new (char *dummy) |
1176 | SV *new (char *dummy) |
… | |
… | |
985 | RETVAL |
1180 | RETVAL |
986 | |
1181 | |
987 | SV *ascii (SV *self, int enable = 1) |
1182 | SV *ascii (SV *self, int enable = 1) |
988 | ALIAS: |
1183 | ALIAS: |
989 | ascii = F_ASCII |
1184 | ascii = F_ASCII |
|
|
1185 | latin1 = F_LATIN1 |
990 | utf8 = F_UTF8 |
1186 | utf8 = F_UTF8 |
991 | indent = F_INDENT |
1187 | indent = F_INDENT |
992 | canonical = F_CANONICAL |
1188 | canonical = F_CANONICAL |
993 | space_before = F_SPACE_BEFORE |
1189 | space_before = F_SPACE_BEFORE |
994 | space_after = F_SPACE_AFTER |
1190 | space_after = F_SPACE_AFTER |
… | |
… | |
1006 | RETVAL = newSVsv (self); |
1202 | RETVAL = newSVsv (self); |
1007 | } |
1203 | } |
1008 | OUTPUT: |
1204 | OUTPUT: |
1009 | RETVAL |
1205 | RETVAL |
1010 | |
1206 | |
|
|
1207 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1208 | CODE: |
|
|
1209 | { |
|
|
1210 | UV *uv = SvJSON (self); |
|
|
1211 | UV log2 = 0; |
|
|
1212 | |
|
|
1213 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1214 | |
|
|
1215 | while ((1UL << log2) < max_depth) |
|
|
1216 | ++log2; |
|
|
1217 | |
|
|
1218 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1219 | |
|
|
1220 | RETVAL = newSVsv (self); |
|
|
1221 | } |
|
|
1222 | OUTPUT: |
|
|
1223 | RETVAL |
|
|
1224 | |
1011 | void encode (SV *self, SV *scalar) |
1225 | void encode (SV *self, SV *scalar) |
1012 | PPCODE: |
1226 | PPCODE: |
1013 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1227 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1014 | |
1228 | |
1015 | void decode (SV *self, SV *jsonstr) |
1229 | void decode (SV *self, SV *jsonstr) |
1016 | PPCODE: |
1230 | PPCODE: |
1017 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1231 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1232 | |
|
|
1233 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1234 | PPCODE: |
|
|
1235 | { |
|
|
1236 | UV offset; |
|
|
1237 | EXTEND (SP, 2); |
|
|
1238 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1239 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1240 | } |
1018 | |
1241 | |
1019 | PROTOTYPES: ENABLE |
1242 | PROTOTYPES: ENABLE |
1020 | |
1243 | |
1021 | void to_json (SV *scalar) |
1244 | void to_json (SV *scalar) |
|
|
1245 | ALIAS: |
|
|
1246 | objToJson = 0 |
1022 | PPCODE: |
1247 | PPCODE: |
1023 | XPUSHs (encode_json (scalar, F_UTF8)); |
1248 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1024 | |
1249 | |
1025 | void from_json (SV *jsonstr) |
1250 | void from_json (SV *jsonstr) |
|
|
1251 | ALIAS: |
|
|
1252 | jsonToObj = 0 |
1026 | PPCODE: |
1253 | PPCODE: |
1027 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1254 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1028 | |
1255 | |