… | |
… | |
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 0x00000001UL |
20 | #define F_ASCII 0x00000001UL |
|
|
21 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002UL |
22 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004UL |
23 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008UL |
24 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010UL |
25 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100UL |
28 | #define F_SHRINK 0x00000200UL |
|
|
29 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
17 | #define F_MAXDEPTH 0xf8000000UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
18 | #define S_MAXDEPTH 27 |
32 | #define S_MAXDEPTH 27 |
|
|
33 | #define F_MAXSIZE 0x01f00000UL |
|
|
34 | #define S_MAXSIZE 20 |
19 | |
35 | |
20 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
21 | |
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
22 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
23 | // F_BLESSED? <=> { $__class__$ => } |
|
|
24 | |
38 | |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
26 | #define F_DEFAULT (13UL << S_MAXDEPTH) |
40 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
27 | |
41 | |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
29 | #define INDENT_STEP 3 // spaces per indentation level |
43 | #define INDENT_STEP 3 // spaces per indentation level |
30 | |
44 | |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
32 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
45 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
33 | |
46 | |
34 | #define SB do { |
47 | #define SB do { |
35 | #define SE } while (0) |
48 | #define SE } while (0) |
36 | |
49 | |
|
|
50 | #if __GNUC__ >= 3 |
|
|
51 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
52 | # define inline inline |
|
|
53 | #else |
|
|
54 | # define expect(expr,value) (expr) |
|
|
55 | # define inline static |
|
|
56 | #endif |
|
|
57 | |
|
|
58 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
60 | |
37 | static HV *json_stash; // JSON::XS:: |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
62 | static SV *json_true, *json_false; |
|
|
63 | |
|
|
64 | typedef struct json { |
|
|
65 | U32 flags; |
|
|
66 | } JSON__XS; |
38 | |
67 | |
39 | ///////////////////////////////////////////////////////////////////////////// |
68 | ///////////////////////////////////////////////////////////////////////////// |
40 | // utility functions |
69 | // utility functions |
41 | |
70 | |
42 | static UV * |
71 | static UV * |
… | |
… | |
65 | // decode an utf-8 character and return it, or (UV)-1 in |
94 | // decode an utf-8 character and return it, or (UV)-1 in |
66 | // case of an error. |
95 | // case of an error. |
67 | // we special-case "safe" characters from U+80 .. U+7FF, |
96 | // we special-case "safe" characters from U+80 .. U+7FF, |
68 | // but use the very good perl function to parse anything else. |
97 | // but use the very good perl function to parse anything else. |
69 | // note that we never call this function for a ascii codepoints |
98 | // note that we never call this function for a ascii codepoints |
70 | static UV |
99 | inline UV |
71 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
100 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
72 | { |
101 | { |
73 | if (s[0] > 0xdf || s[0] < 0xc2) |
102 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
74 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
103 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
75 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
104 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
76 | { |
105 | { |
77 | *clen = 2; |
106 | *clen = 2; |
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
107 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
79 | } |
108 | } |
80 | else |
109 | else |
|
|
110 | { |
|
|
111 | *clen = (STRLEN)-1; |
81 | return (UV)-1; |
112 | return (UV)-1; |
|
|
113 | } |
82 | } |
114 | } |
83 | |
115 | |
84 | ///////////////////////////////////////////////////////////////////////////// |
116 | ///////////////////////////////////////////////////////////////////////////// |
85 | // encoder |
117 | // encoder |
86 | |
118 | |
… | |
… | |
88 | typedef struct |
120 | typedef struct |
89 | { |
121 | { |
90 | char *cur; // SvPVX (sv) + current output position |
122 | char *cur; // SvPVX (sv) + current output position |
91 | char *end; // SvEND (sv) |
123 | char *end; // SvEND (sv) |
92 | SV *sv; // result scalar |
124 | SV *sv; // result scalar |
93 | U32 flags; // F_* |
125 | struct json json; |
94 | U32 indent; // indentation level |
126 | U32 indent; // indentation level |
95 | U32 maxdepth; // max. indentation/recursion level |
127 | U32 maxdepth; // max. indentation/recursion level |
96 | } enc_t; |
128 | } enc_t; |
97 | |
129 | |
98 | static void |
130 | inline void |
99 | need (enc_t *enc, STRLEN len) |
131 | need (enc_t *enc, STRLEN len) |
100 | { |
132 | { |
101 | if (enc->cur + len >= enc->end) |
133 | if (expect_false (enc->cur + len >= enc->end)) |
102 | { |
134 | { |
103 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
135 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
104 | SvGROW (enc->sv, cur + len + 1); |
136 | SvGROW (enc->sv, cur + len + 1); |
105 | enc->cur = SvPVX (enc->sv) + cur; |
137 | enc->cur = SvPVX (enc->sv) + cur; |
106 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
138 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
107 | } |
139 | } |
108 | } |
140 | } |
109 | |
141 | |
110 | static void |
142 | inline void |
111 | encode_ch (enc_t *enc, char ch) |
143 | encode_ch (enc_t *enc, char ch) |
112 | { |
144 | { |
113 | need (enc, 1); |
145 | need (enc, 1); |
114 | *enc->cur++ = ch; |
146 | *enc->cur++ = ch; |
115 | } |
147 | } |
… | |
… | |
123 | |
155 | |
124 | while (str < end) |
156 | while (str < end) |
125 | { |
157 | { |
126 | unsigned char ch = *(unsigned char *)str; |
158 | unsigned char ch = *(unsigned char *)str; |
127 | |
159 | |
128 | if (ch >= 0x20 && ch < 0x80) // most common case |
160 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
129 | { |
161 | { |
130 | if (ch == '"') // but with slow exceptions |
162 | if (expect_false (ch == '"')) // but with slow exceptions |
131 | { |
163 | { |
132 | need (enc, len += 1); |
164 | need (enc, len += 1); |
133 | *enc->cur++ = '\\'; |
165 | *enc->cur++ = '\\'; |
134 | *enc->cur++ = '"'; |
166 | *enc->cur++ = '"'; |
135 | } |
167 | } |
136 | else if (ch == '\\') |
168 | else if (expect_false (ch == '\\')) |
137 | { |
169 | { |
138 | need (enc, len += 1); |
170 | need (enc, len += 1); |
139 | *enc->cur++ = '\\'; |
171 | *enc->cur++ = '\\'; |
140 | *enc->cur++ = '\\'; |
172 | *enc->cur++ = '\\'; |
141 | } |
173 | } |
… | |
… | |
159 | STRLEN clen; |
191 | STRLEN clen; |
160 | UV uch; |
192 | UV uch; |
161 | |
193 | |
162 | if (is_utf8) |
194 | if (is_utf8) |
163 | { |
195 | { |
164 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
165 | uch = decode_utf8 (str, end - str, &clen); |
196 | uch = decode_utf8 (str, end - str, &clen); |
166 | if (clen == (STRLEN)-1) |
197 | if (clen == (STRLEN)-1) |
167 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
198 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
168 | } |
199 | } |
169 | else |
200 | else |
… | |
… | |
173 | } |
204 | } |
174 | |
205 | |
175 | if (uch > 0x10FFFFUL) |
206 | if (uch > 0x10FFFFUL) |
176 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
207 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
177 | |
208 | |
178 | if (uch < 0x80 || enc->flags & F_ASCII) |
209 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
179 | { |
210 | { |
180 | if (uch > 0xFFFFUL) |
211 | if (uch > 0xFFFFUL) |
181 | { |
212 | { |
182 | need (enc, len += 11); |
213 | need (enc, len += 11); |
183 | sprintf (enc->cur, "\\u%04x\\u%04x", |
214 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
197 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
228 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
198 | } |
229 | } |
199 | |
230 | |
200 | str += clen; |
231 | str += clen; |
201 | } |
232 | } |
|
|
233 | else if (enc->json.flags & F_LATIN1) |
|
|
234 | { |
|
|
235 | *enc->cur++ = uch; |
|
|
236 | str += clen; |
|
|
237 | } |
202 | else if (is_utf8) |
238 | else if (is_utf8) |
203 | { |
239 | { |
204 | need (enc, len += clen); |
240 | need (enc, len += clen); |
205 | do |
241 | do |
206 | { |
242 | { |
… | |
… | |
208 | } |
244 | } |
209 | while (--clen); |
245 | while (--clen); |
210 | } |
246 | } |
211 | else |
247 | else |
212 | { |
248 | { |
213 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
249 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
214 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
250 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
215 | ++str; |
251 | ++str; |
216 | } |
252 | } |
217 | } |
253 | } |
218 | } |
254 | } |
… | |
… | |
220 | |
256 | |
221 | --len; |
257 | --len; |
222 | } |
258 | } |
223 | } |
259 | } |
224 | |
260 | |
225 | static void |
261 | inline void |
226 | encode_indent (enc_t *enc) |
262 | encode_indent (enc_t *enc) |
227 | { |
263 | { |
228 | if (enc->flags & F_INDENT) |
264 | if (enc->json.flags & F_INDENT) |
229 | { |
265 | { |
230 | int spaces = enc->indent * INDENT_STEP; |
266 | int spaces = enc->indent * INDENT_STEP; |
231 | |
267 | |
232 | need (enc, spaces); |
268 | need (enc, spaces); |
233 | memset (enc->cur, ' ', spaces); |
269 | memset (enc->cur, ' ', spaces); |
234 | enc->cur += spaces; |
270 | enc->cur += spaces; |
235 | } |
271 | } |
236 | } |
272 | } |
237 | |
273 | |
238 | static void |
274 | inline void |
239 | encode_space (enc_t *enc) |
275 | encode_space (enc_t *enc) |
240 | { |
276 | { |
241 | need (enc, 1); |
277 | need (enc, 1); |
242 | encode_ch (enc, ' '); |
278 | encode_ch (enc, ' '); |
243 | } |
279 | } |
244 | |
280 | |
245 | static void |
281 | inline void |
246 | encode_nl (enc_t *enc) |
282 | encode_nl (enc_t *enc) |
247 | { |
283 | { |
248 | if (enc->flags & F_INDENT) |
284 | if (enc->json.flags & F_INDENT) |
249 | { |
285 | { |
250 | need (enc, 1); |
286 | need (enc, 1); |
251 | encode_ch (enc, '\n'); |
287 | encode_ch (enc, '\n'); |
252 | } |
288 | } |
253 | } |
289 | } |
254 | |
290 | |
255 | static void |
291 | inline void |
256 | encode_comma (enc_t *enc) |
292 | encode_comma (enc_t *enc) |
257 | { |
293 | { |
258 | encode_ch (enc, ','); |
294 | encode_ch (enc, ','); |
259 | |
295 | |
260 | if (enc->flags & F_INDENT) |
296 | if (enc->json.flags & F_INDENT) |
261 | encode_nl (enc); |
297 | encode_nl (enc); |
262 | else if (enc->flags & F_SPACE_AFTER) |
298 | else if (enc->json.flags & F_SPACE_AFTER) |
263 | encode_space (enc); |
299 | encode_space (enc); |
264 | } |
300 | } |
265 | |
301 | |
266 | static void encode_sv (enc_t *enc, SV *sv); |
302 | static void encode_sv (enc_t *enc, SV *sv); |
267 | |
303 | |
268 | static void |
304 | static void |
269 | encode_av (enc_t *enc, AV *av) |
305 | encode_av (enc_t *enc, AV *av) |
270 | { |
306 | { |
271 | int i, len = av_len (av); |
307 | int i, len = av_len (av); |
|
|
308 | |
|
|
309 | if (enc->indent >= enc->maxdepth) |
|
|
310 | croak ("data structure too deep (hit recursion limit)"); |
272 | |
311 | |
273 | encode_ch (enc, '['); encode_nl (enc); |
312 | encode_ch (enc, '['); encode_nl (enc); |
274 | ++enc->indent; |
313 | ++enc->indent; |
275 | |
314 | |
276 | for (i = 0; i <= len; ++i) |
315 | for (i = 0; i <= len; ++i) |
… | |
… | |
307 | else |
346 | else |
308 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
347 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
309 | |
348 | |
310 | encode_ch (enc, '"'); |
349 | encode_ch (enc, '"'); |
311 | |
350 | |
312 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
351 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
313 | encode_ch (enc, ':'); |
352 | encode_ch (enc, ':'); |
314 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
353 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
315 | encode_sv (enc, HeVAL (he)); |
354 | encode_sv (enc, HeVAL (he)); |
316 | } |
355 | } |
317 | |
356 | |
318 | // compare hash entries, used when all keys are bytestrings |
357 | // compare hash entries, used when all keys are bytestrings |
319 | static int |
358 | static int |
… | |
… | |
342 | |
381 | |
343 | static void |
382 | static void |
344 | encode_hv (enc_t *enc, HV *hv) |
383 | encode_hv (enc_t *enc, HV *hv) |
345 | { |
384 | { |
346 | int count, i; |
385 | int count, i; |
|
|
386 | |
|
|
387 | if (enc->indent >= enc->maxdepth) |
|
|
388 | croak ("data structure too deep (hit recursion limit)"); |
347 | |
389 | |
348 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
390 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
349 | |
391 | |
350 | if ((count = hv_iterinit (hv))) |
392 | if ((count = hv_iterinit (hv))) |
351 | { |
393 | { |
352 | // for canonical output we have to sort by keys first |
394 | // for canonical output we have to sort by keys first |
353 | // actually, this is mostly due to the stupid so-called |
395 | // actually, this is mostly due to the stupid so-called |
354 | // security workaround added somewhere in 5.8.x. |
396 | // security workaround added somewhere in 5.8.x. |
355 | // that randomises hash orderings |
397 | // that randomises hash orderings |
356 | if (enc->flags & F_CANONICAL) |
398 | if (enc->json.flags & F_CANONICAL) |
357 | { |
399 | { |
358 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
359 | int fast = 1; |
400 | int fast = 1; |
|
|
401 | HE *he; |
|
|
402 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
403 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
404 | #else |
|
|
405 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
406 | #endif |
360 | |
407 | |
361 | i = 0; |
408 | i = 0; |
362 | while ((he = hv_iternext (hv))) |
409 | while ((he = hv_iternext (hv))) |
363 | { |
410 | { |
364 | hes [i++] = he; |
411 | hes [i++] = he; |
… | |
… | |
399 | |
446 | |
400 | encode_nl (enc); |
447 | encode_nl (enc); |
401 | } |
448 | } |
402 | else |
449 | else |
403 | { |
450 | { |
404 | SV *sv; |
|
|
405 | HE *he = hv_iternext (hv); |
451 | HE *he = hv_iternext (hv); |
406 | |
452 | |
407 | for (;;) |
453 | for (;;) |
408 | { |
454 | { |
409 | encode_indent (enc); |
455 | encode_indent (enc); |
… | |
… | |
418 | encode_nl (enc); |
464 | encode_nl (enc); |
419 | } |
465 | } |
420 | } |
466 | } |
421 | |
467 | |
422 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
468 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
469 | } |
|
|
470 | |
|
|
471 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
472 | static void |
|
|
473 | encode_rv (enc_t *enc, SV *sv) |
|
|
474 | { |
|
|
475 | svtype svt; |
|
|
476 | |
|
|
477 | SvGETMAGIC (sv); |
|
|
478 | svt = SvTYPE (sv); |
|
|
479 | |
|
|
480 | if (expect_false (SvOBJECT (sv))) |
|
|
481 | { |
|
|
482 | if (SvSTASH (sv) == json_boolean_stash) |
|
|
483 | { |
|
|
484 | if (SvIV (sv) == 0) |
|
|
485 | encode_str (enc, "false", 5, 0); |
|
|
486 | else |
|
|
487 | encode_str (enc, "true", 4, 0); |
|
|
488 | } |
|
|
489 | else |
|
|
490 | { |
|
|
491 | #if 0 |
|
|
492 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
493 | { |
|
|
494 | // not yet |
|
|
495 | } |
|
|
496 | #endif |
|
|
497 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
498 | { |
|
|
499 | // we re-bless the reference to get overload and other niceties right |
|
|
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
|
|
501 | |
|
|
502 | if (to_json) |
|
|
503 | { |
|
|
504 | dSP; |
|
|
505 | ENTER; |
|
|
506 | SAVETMPS; |
|
|
507 | PUSHMARK (SP); |
|
|
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
509 | |
|
|
510 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
|
|
511 | // check anyways. |
|
|
512 | PUTBACK; |
|
|
513 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
|
|
514 | SPAGAIN; |
|
|
515 | |
|
|
516 | encode_sv (enc, POPs); |
|
|
517 | |
|
|
518 | FREETMPS; |
|
|
519 | LEAVE; |
|
|
520 | } |
|
|
521 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
522 | encode_str (enc, "null", 4, 0); |
|
|
523 | else |
|
|
524 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
525 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
526 | } |
|
|
527 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
528 | encode_str (enc, "null", 4, 0); |
|
|
529 | else |
|
|
530 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
531 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
532 | } |
|
|
533 | } |
|
|
534 | else if (svt == SVt_PVHV) |
|
|
535 | encode_hv (enc, (HV *)sv); |
|
|
536 | else if (svt == SVt_PVAV) |
|
|
537 | encode_av (enc, (AV *)sv); |
|
|
538 | else if (svt < SVt_PVAV) |
|
|
539 | { |
|
|
540 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
541 | encode_str (enc, "false", 5, 0); |
|
|
542 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
543 | encode_str (enc, "true", 4, 0); |
|
|
544 | else |
|
|
545 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
546 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
547 | } |
|
|
548 | else |
|
|
549 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
550 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
423 | } |
551 | } |
424 | |
552 | |
425 | static void |
553 | static void |
426 | encode_sv (enc_t *enc, SV *sv) |
554 | encode_sv (enc_t *enc, SV *sv) |
427 | { |
555 | { |
… | |
… | |
435 | encode_str (enc, str, len, SvUTF8 (sv)); |
563 | encode_str (enc, str, len, SvUTF8 (sv)); |
436 | encode_ch (enc, '"'); |
564 | encode_ch (enc, '"'); |
437 | } |
565 | } |
438 | else if (SvNOKp (sv)) |
566 | else if (SvNOKp (sv)) |
439 | { |
567 | { |
|
|
568 | // trust that perl will do the right thing w.r.t. JSON syntax. |
440 | need (enc, NV_DIG + 32); |
569 | need (enc, NV_DIG + 32); |
441 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
570 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
442 | enc->cur += strlen (enc->cur); |
571 | enc->cur += strlen (enc->cur); |
443 | } |
572 | } |
444 | else if (SvIOKp (sv)) |
573 | else if (SvIOKp (sv)) |
445 | { |
574 | { |
446 | need (enc, 64); |
575 | // we assume we can always read an IV as a UV |
|
|
576 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
577 | { |
|
|
578 | // large integer, use the (rather slow) snprintf way. |
|
|
579 | need (enc, sizeof (UV) * 3); |
447 | enc->cur += |
580 | enc->cur += |
448 | SvIsUV(sv) |
581 | SvIsUV(sv) |
449 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
582 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
450 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
583 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
584 | } |
|
|
585 | else |
|
|
586 | { |
|
|
587 | // optimise the "small number case" |
|
|
588 | // code will likely be branchless and use only a single multiplication |
|
|
589 | I32 i = SvIV (sv); |
|
|
590 | U32 u; |
|
|
591 | char digit, nz = 0; |
|
|
592 | |
|
|
593 | need (enc, 6); |
|
|
594 | |
|
|
595 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
596 | u = i < 0 ? -i : i; |
|
|
597 | |
|
|
598 | // convert to 4.28 fixed-point representation |
|
|
599 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
600 | |
|
|
601 | // now output digit by digit, each time masking out the integer part |
|
|
602 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
603 | // resulting in a net multiplication by 10. |
|
|
604 | // we always write the digit to memory but conditionally increment |
|
|
605 | // the pointer, to ease the usage of conditional move instructions. |
|
|
606 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
607 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
608 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
609 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
610 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
611 | } |
451 | } |
612 | } |
452 | else if (SvROK (sv)) |
613 | else if (SvROK (sv)) |
453 | { |
614 | encode_rv (enc, SvRV (sv)); |
454 | SV *rv = SvRV (sv); |
|
|
455 | |
|
|
456 | if (enc->indent >= enc->maxdepth) |
|
|
457 | croak ("data structure too deep (hit recursion limit)"); |
|
|
458 | |
|
|
459 | switch (SvTYPE (rv)) |
|
|
460 | { |
|
|
461 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
462 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
463 | |
|
|
464 | default: |
|
|
465 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
466 | SvPV_nolen (sv)); |
|
|
467 | } |
|
|
468 | } |
|
|
469 | else if (!SvOK (sv)) |
615 | else if (!SvOK (sv)) |
470 | encode_str (enc, "null", 4, 0); |
616 | encode_str (enc, "null", 4, 0); |
471 | else |
617 | else |
472 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
618 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
473 | SvPV_nolen (sv), SvFLAGS (sv)); |
619 | SvPV_nolen (sv), SvFLAGS (sv)); |
474 | } |
620 | } |
475 | |
621 | |
476 | static SV * |
622 | static SV * |
477 | encode_json (SV *scalar, U32 flags) |
623 | encode_json (SV *scalar, struct json *json) |
478 | { |
624 | { |
|
|
625 | enc_t enc; |
|
|
626 | |
479 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
627 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
480 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
628 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
481 | |
629 | |
482 | enc_t enc; |
630 | enc.json = *json; |
483 | enc.flags = flags; |
|
|
484 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
631 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
485 | enc.cur = SvPVX (enc.sv); |
632 | enc.cur = SvPVX (enc.sv); |
486 | enc.end = SvEND (enc.sv); |
633 | enc.end = SvEND (enc.sv); |
487 | enc.indent = 0; |
634 | enc.indent = 0; |
488 | enc.maxdepth = DEC_DEPTH (flags); |
635 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
489 | |
636 | |
490 | SvPOK_only (enc.sv); |
637 | SvPOK_only (enc.sv); |
491 | encode_sv (&enc, scalar); |
638 | encode_sv (&enc, scalar); |
492 | |
639 | |
|
|
640 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
641 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
642 | |
493 | if (!(flags & (F_ASCII | F_UTF8))) |
643 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
494 | SvUTF8_on (enc.sv); |
644 | SvUTF8_on (enc.sv); |
495 | |
645 | |
496 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
497 | |
|
|
498 | if (enc.flags & F_SHRINK) |
646 | if (enc.json.flags & F_SHRINK) |
499 | shrink (enc.sv); |
647 | shrink (enc.sv); |
500 | |
648 | |
501 | return enc.sv; |
649 | return enc.sv; |
502 | } |
650 | } |
503 | |
651 | |
… | |
… | |
508 | typedef struct |
656 | typedef struct |
509 | { |
657 | { |
510 | char *cur; // current parser pointer |
658 | char *cur; // current parser pointer |
511 | char *end; // end of input string |
659 | char *end; // end of input string |
512 | const char *err; // parse error, if != 0 |
660 | const char *err; // parse error, if != 0 |
513 | U32 flags; // F_* |
661 | struct json json; |
514 | U32 depth; // recursion depth |
662 | U32 depth; // recursion depth |
515 | U32 maxdepth; // recursion depth limit |
663 | U32 maxdepth; // recursion depth limit |
516 | } dec_t; |
664 | } dec_t; |
517 | |
665 | |
518 | static void |
666 | inline void |
519 | decode_ws (dec_t *dec) |
667 | decode_ws (dec_t *dec) |
520 | { |
668 | { |
521 | for (;;) |
669 | for (;;) |
522 | { |
670 | { |
523 | char ch = *dec->cur; |
671 | char ch = *dec->cur; |
… | |
… | |
549 | decode_4hex (dec_t *dec) |
697 | decode_4hex (dec_t *dec) |
550 | { |
698 | { |
551 | signed char d1, d2, d3, d4; |
699 | signed char d1, d2, d3, d4; |
552 | unsigned char *cur = (unsigned char *)dec->cur; |
700 | unsigned char *cur = (unsigned char *)dec->cur; |
553 | |
701 | |
554 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
702 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
555 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
703 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
556 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
704 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
557 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
705 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
558 | |
706 | |
559 | dec->cur += 4; |
707 | dec->cur += 4; |
560 | |
708 | |
561 | return ((UV)d1) << 12 |
709 | return ((UV)d1) << 12 |
562 | | ((UV)d2) << 8 |
710 | | ((UV)d2) << 8 |
… | |
… | |
570 | static SV * |
718 | static SV * |
571 | decode_str (dec_t *dec) |
719 | decode_str (dec_t *dec) |
572 | { |
720 | { |
573 | SV *sv = 0; |
721 | SV *sv = 0; |
574 | int utf8 = 0; |
722 | int utf8 = 0; |
|
|
723 | char *dec_cur = dec->cur; |
575 | |
724 | |
576 | do |
725 | do |
577 | { |
726 | { |
578 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
727 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
579 | char *cur = buf; |
728 | char *cur = buf; |
580 | |
729 | |
581 | do |
730 | do |
582 | { |
731 | { |
583 | unsigned char ch = *(unsigned char *)dec->cur++; |
732 | unsigned char ch = *(unsigned char *)dec_cur++; |
584 | |
733 | |
585 | if (ch == '"') |
734 | if (expect_false (ch == '"')) |
586 | { |
735 | { |
587 | --dec->cur; |
736 | --dec_cur; |
588 | break; |
737 | break; |
589 | } |
738 | } |
590 | else if (ch == '\\') |
739 | else if (expect_false (ch == '\\')) |
591 | { |
740 | { |
592 | switch (*dec->cur) |
741 | switch (*dec_cur) |
593 | { |
742 | { |
594 | case '\\': |
743 | case '\\': |
595 | case '/': |
744 | case '/': |
596 | case '"': *cur++ = *dec->cur++; break; |
745 | case '"': *cur++ = *dec_cur++; break; |
597 | |
746 | |
598 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
747 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
599 | case 't': ++dec->cur; *cur++ = '\011'; break; |
748 | case 't': ++dec_cur; *cur++ = '\011'; break; |
600 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
749 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
601 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
750 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
602 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
751 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
603 | |
752 | |
604 | case 'u': |
753 | case 'u': |
605 | { |
754 | { |
606 | UV lo, hi; |
755 | UV lo, hi; |
607 | ++dec->cur; |
756 | ++dec_cur; |
608 | |
757 | |
|
|
758 | dec->cur = dec_cur; |
609 | hi = decode_4hex (dec); |
759 | hi = decode_4hex (dec); |
|
|
760 | dec_cur = dec->cur; |
610 | if (hi == (UV)-1) |
761 | if (hi == (UV)-1) |
611 | goto fail; |
762 | goto fail; |
612 | |
763 | |
613 | // possibly a surrogate pair |
764 | // possibly a surrogate pair |
614 | if (hi >= 0xd800) |
765 | if (hi >= 0xd800) |
615 | if (hi < 0xdc00) |
766 | if (hi < 0xdc00) |
616 | { |
767 | { |
617 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
768 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
618 | ERR ("missing low surrogate character in surrogate pair"); |
769 | ERR ("missing low surrogate character in surrogate pair"); |
619 | |
770 | |
620 | dec->cur += 2; |
771 | dec_cur += 2; |
621 | |
772 | |
|
|
773 | dec->cur = dec_cur; |
622 | lo = decode_4hex (dec); |
774 | lo = decode_4hex (dec); |
|
|
775 | dec_cur = dec->cur; |
623 | if (lo == (UV)-1) |
776 | if (lo == (UV)-1) |
624 | goto fail; |
777 | goto fail; |
625 | |
778 | |
626 | if (lo < 0xdc00 || lo >= 0xe000) |
779 | if (lo < 0xdc00 || lo >= 0xe000) |
627 | ERR ("surrogate pair expected"); |
780 | ERR ("surrogate pair expected"); |
… | |
… | |
641 | *cur++ = hi; |
794 | *cur++ = hi; |
642 | } |
795 | } |
643 | break; |
796 | break; |
644 | |
797 | |
645 | default: |
798 | default: |
646 | --dec->cur; |
799 | --dec_cur; |
647 | ERR ("illegal backslash escape sequence in string"); |
800 | ERR ("illegal backslash escape sequence in string"); |
648 | } |
801 | } |
649 | } |
802 | } |
650 | else if (ch >= 0x20 && ch <= 0x7f) |
803 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
651 | *cur++ = ch; |
804 | *cur++ = ch; |
652 | else if (ch >= 0x80) |
805 | else if (ch >= 0x80) |
653 | { |
806 | { |
654 | --dec->cur; |
|
|
655 | |
|
|
656 | STRLEN clen; |
807 | STRLEN clen; |
|
|
808 | UV uch; |
|
|
809 | |
|
|
810 | --dec_cur; |
|
|
811 | |
657 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
812 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
658 | if (clen == (STRLEN)-1) |
813 | if (clen == (STRLEN)-1) |
659 | ERR ("malformed UTF-8 character in JSON string"); |
814 | ERR ("malformed UTF-8 character in JSON string"); |
660 | |
815 | |
661 | do |
816 | do |
662 | { |
|
|
663 | *cur++ = *dec->cur++; |
817 | *cur++ = *dec_cur++; |
664 | } |
|
|
665 | while (--clen); |
818 | while (--clen); |
666 | |
819 | |
667 | utf8 = 1; |
820 | utf8 = 1; |
668 | } |
821 | } |
669 | else if (!ch) |
|
|
670 | ERR ("unexpected end of string while parsing json string"); |
|
|
671 | else |
822 | else |
|
|
823 | { |
|
|
824 | --dec_cur; |
|
|
825 | |
|
|
826 | if (!ch) |
|
|
827 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
828 | else |
672 | ERR ("invalid character encountered"); |
829 | ERR ("invalid character encountered while parsing JSON string"); |
673 | |
830 | } |
674 | } |
831 | } |
675 | while (cur < buf + SHORT_STRING_LEN); |
832 | while (cur < buf + SHORT_STRING_LEN); |
676 | |
833 | |
|
|
834 | { |
677 | STRLEN len = cur - buf; |
835 | STRLEN len = cur - buf; |
678 | |
836 | |
679 | if (sv) |
837 | if (sv) |
680 | { |
838 | { |
681 | SvGROW (sv, SvCUR (sv) + len + 1); |
839 | SvGROW (sv, SvCUR (sv) + len + 1); |
682 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
840 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
683 | SvCUR_set (sv, SvCUR (sv) + len); |
841 | SvCUR_set (sv, SvCUR (sv) + len); |
684 | } |
842 | } |
685 | else |
843 | else |
686 | sv = newSVpvn (buf, len); |
844 | sv = newSVpvn (buf, len); |
687 | } |
845 | } |
|
|
846 | } |
688 | while (*dec->cur != '"'); |
847 | while (*dec_cur != '"'); |
689 | |
848 | |
690 | ++dec->cur; |
849 | ++dec_cur; |
691 | |
850 | |
692 | if (sv) |
851 | if (sv) |
693 | { |
852 | { |
694 | SvPOK_only (sv); |
853 | SvPOK_only (sv); |
695 | *SvEND (sv) = 0; |
854 | *SvEND (sv) = 0; |
… | |
… | |
698 | SvUTF8_on (sv); |
857 | SvUTF8_on (sv); |
699 | } |
858 | } |
700 | else |
859 | else |
701 | sv = newSVpvn ("", 0); |
860 | sv = newSVpvn ("", 0); |
702 | |
861 | |
|
|
862 | dec->cur = dec_cur; |
703 | return sv; |
863 | return sv; |
704 | |
864 | |
705 | fail: |
865 | fail: |
|
|
866 | dec->cur = dec_cur; |
706 | return 0; |
867 | return 0; |
707 | } |
868 | } |
708 | |
869 | |
709 | static SV * |
870 | static SV * |
710 | decode_num (dec_t *dec) |
871 | decode_num (dec_t *dec) |
… | |
… | |
768 | is_nv = 1; |
929 | is_nv = 1; |
769 | } |
930 | } |
770 | |
931 | |
771 | if (!is_nv) |
932 | if (!is_nv) |
772 | { |
933 | { |
773 | UV uv; |
934 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
774 | int numtype = grok_number (start, dec->cur - start, &uv); |
935 | if (*start == '-') |
775 | if (numtype & IS_NUMBER_IN_UV) |
936 | switch (dec->cur - start) |
776 | if (numtype & IS_NUMBER_NEG) |
|
|
777 | { |
937 | { |
778 | if (uv < (UV)IV_MIN) |
938 | case 2: return newSViv (-( start [1] - '0' * 1)); |
779 | return newSViv (-(IV)uv); |
939 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
940 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
941 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
780 | } |
942 | } |
|
|
943 | else |
|
|
944 | switch (dec->cur - start) |
|
|
945 | { |
|
|
946 | case 1: return newSViv ( start [0] - '0' * 1); |
|
|
947 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
948 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
949 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
950 | } |
|
|
951 | |
|
|
952 | { |
|
|
953 | UV uv; |
|
|
954 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
955 | if (numtype & IS_NUMBER_IN_UV) |
|
|
956 | if (numtype & IS_NUMBER_NEG) |
|
|
957 | { |
|
|
958 | if (uv < (UV)IV_MIN) |
|
|
959 | return newSViv (-(IV)uv); |
|
|
960 | } |
781 | else |
961 | else |
782 | return newSVuv (uv); |
962 | return newSVuv (uv); |
|
|
963 | |
|
|
964 | // here would likely be the place for bigint support |
783 | } |
965 | } |
|
|
966 | } |
784 | |
967 | |
|
|
968 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
785 | return newSVnv (Atof (start)); |
969 | return newSVnv (Atof (start)); |
786 | |
970 | |
787 | fail: |
971 | fail: |
788 | return 0; |
972 | return 0; |
789 | } |
973 | } |
… | |
… | |
843 | if (*dec->cur == '}') |
1027 | if (*dec->cur == '}') |
844 | ++dec->cur; |
1028 | ++dec->cur; |
845 | else |
1029 | else |
846 | for (;;) |
1030 | for (;;) |
847 | { |
1031 | { |
848 | SV *key, *value; |
|
|
849 | |
|
|
850 | decode_ws (dec); EXPECT_CH ('"'); |
1032 | decode_ws (dec); EXPECT_CH ('"'); |
851 | |
1033 | |
852 | key = decode_str (dec); |
1034 | // heuristic: assume that |
853 | if (!key) |
1035 | // a) decode_str + hv_store_ent are abysmally slow. |
854 | goto fail; |
1036 | // b) most hash keys are short, simple ascii text. |
|
|
1037 | // => try to "fast-match" such strings to avoid |
|
|
1038 | // the overhead of decode_str + hv_store_ent. |
|
|
1039 | { |
|
|
1040 | SV *value; |
|
|
1041 | char *p = dec->cur; |
|
|
1042 | char *e = p + 24; // only try up to 24 bytes |
855 | |
1043 | |
856 | decode_ws (dec); EXPECT_CH (':'); |
1044 | for (;;) |
857 | |
|
|
858 | value = decode_sv (dec); |
|
|
859 | if (!value) |
|
|
860 | { |
1045 | { |
|
|
1046 | // the >= 0x80 is true on most architectures |
|
|
1047 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1048 | { |
|
|
1049 | // slow path, back up and use decode_str |
|
|
1050 | SV *key = decode_str (dec); |
|
|
1051 | if (!key) |
|
|
1052 | goto fail; |
|
|
1053 | |
|
|
1054 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1055 | |
|
|
1056 | value = decode_sv (dec); |
|
|
1057 | if (!value) |
|
|
1058 | { |
|
|
1059 | SvREFCNT_dec (key); |
|
|
1060 | goto fail; |
|
|
1061 | } |
|
|
1062 | |
|
|
1063 | hv_store_ent (hv, key, value, 0); |
861 | SvREFCNT_dec (key); |
1064 | SvREFCNT_dec (key); |
|
|
1065 | |
|
|
1066 | break; |
|
|
1067 | } |
|
|
1068 | else if (*p == '"') |
|
|
1069 | { |
|
|
1070 | // fast path, got a simple key |
|
|
1071 | char *key = dec->cur; |
|
|
1072 | int len = p - key; |
|
|
1073 | dec->cur = p + 1; |
|
|
1074 | |
|
|
1075 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1076 | |
|
|
1077 | value = decode_sv (dec); |
|
|
1078 | if (!value) |
862 | goto fail; |
1079 | goto fail; |
|
|
1080 | |
|
|
1081 | hv_store (hv, key, len, value, 0); |
|
|
1082 | |
|
|
1083 | break; |
|
|
1084 | } |
|
|
1085 | |
|
|
1086 | ++p; |
863 | } |
1087 | } |
864 | |
1088 | } |
865 | hv_store_ent (hv, key, value, 0); |
|
|
866 | SvREFCNT_dec (key); |
|
|
867 | |
1089 | |
868 | decode_ws (dec); |
1090 | decode_ws (dec); |
869 | |
1091 | |
870 | if (*dec->cur == '}') |
1092 | if (*dec->cur == '}') |
871 | { |
1093 | { |
… | |
… | |
890 | |
1112 | |
891 | static SV * |
1113 | static SV * |
892 | decode_sv (dec_t *dec) |
1114 | decode_sv (dec_t *dec) |
893 | { |
1115 | { |
894 | decode_ws (dec); |
1116 | decode_ws (dec); |
|
|
1117 | |
|
|
1118 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1119 | // to parse anything. |
895 | switch (*dec->cur) |
1120 | switch (*dec->cur) |
896 | { |
1121 | { |
897 | case '"': ++dec->cur; return decode_str (dec); |
1122 | case '"': ++dec->cur; return decode_str (dec); |
898 | case '[': ++dec->cur; return decode_av (dec); |
1123 | case '[': ++dec->cur; return decode_av (dec); |
899 | case '{': ++dec->cur; return decode_hv (dec); |
1124 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
905 | |
1130 | |
906 | case 't': |
1131 | case 't': |
907 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1132 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
908 | { |
1133 | { |
909 | dec->cur += 4; |
1134 | dec->cur += 4; |
910 | return newSViv (1); |
1135 | return SvREFCNT_inc (json_true); |
911 | } |
1136 | } |
912 | else |
1137 | else |
913 | ERR ("'true' expected"); |
1138 | ERR ("'true' expected"); |
914 | |
1139 | |
915 | break; |
1140 | break; |
916 | |
1141 | |
917 | case 'f': |
1142 | case 'f': |
918 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1143 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
919 | { |
1144 | { |
920 | dec->cur += 5; |
1145 | dec->cur += 5; |
921 | return newSViv (0); |
1146 | return SvREFCNT_inc (json_false); |
922 | } |
1147 | } |
923 | else |
1148 | else |
924 | ERR ("'false' expected"); |
1149 | ERR ("'false' expected"); |
925 | |
1150 | |
926 | break; |
1151 | break; |
… | |
… | |
935 | ERR ("'null' expected"); |
1160 | ERR ("'null' expected"); |
936 | |
1161 | |
937 | break; |
1162 | break; |
938 | |
1163 | |
939 | default: |
1164 | default: |
940 | ERR ("malformed json string, neither array, object, number, string or atom"); |
1165 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
941 | break; |
1166 | break; |
942 | } |
1167 | } |
943 | |
1168 | |
944 | fail: |
1169 | fail: |
945 | return 0; |
1170 | return 0; |
946 | } |
1171 | } |
947 | |
1172 | |
948 | static SV * |
1173 | static SV * |
949 | decode_json (SV *string, U32 flags) |
1174 | decode_json (SV *string, struct json *json, UV *offset_return) |
950 | { |
1175 | { |
|
|
1176 | dec_t dec; |
|
|
1177 | UV offset; |
951 | SV *sv; |
1178 | SV *sv; |
952 | |
1179 | |
|
|
1180 | SvGETMAGIC (string); |
|
|
1181 | SvUPGRADE (string, SVt_PV); |
|
|
1182 | |
|
|
1183 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1184 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1185 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1186 | |
953 | if (flags & F_UTF8) |
1187 | if (json->flags & F_UTF8) |
954 | sv_utf8_downgrade (string, 0); |
1188 | sv_utf8_downgrade (string, 0); |
955 | else |
1189 | else |
956 | sv_utf8_upgrade (string); |
1190 | sv_utf8_upgrade (string); |
957 | |
1191 | |
958 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1192 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
959 | |
1193 | |
960 | dec_t dec; |
1194 | dec.json = *json; |
961 | dec.flags = flags; |
|
|
962 | dec.cur = SvPVX (string); |
1195 | dec.cur = SvPVX (string); |
963 | dec.end = SvEND (string); |
1196 | dec.end = SvEND (string); |
964 | dec.err = 0; |
1197 | dec.err = 0; |
965 | dec.depth = 0; |
1198 | dec.depth = 0; |
966 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1199 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
967 | |
1200 | |
968 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
1201 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
969 | sv = decode_sv (&dec); |
1202 | sv = decode_sv (&dec); |
970 | |
1203 | |
|
|
1204 | if (!(offset_return || !sv)) |
|
|
1205 | { |
|
|
1206 | // check for trailing garbage |
|
|
1207 | decode_ws (&dec); |
|
|
1208 | |
|
|
1209 | if (*dec.cur) |
|
|
1210 | { |
|
|
1211 | dec.err = "garbage after JSON object"; |
|
|
1212 | SvREFCNT_dec (sv); |
|
|
1213 | sv = 0; |
|
|
1214 | } |
|
|
1215 | } |
|
|
1216 | |
|
|
1217 | if (offset_return || !sv) |
|
|
1218 | { |
|
|
1219 | offset = dec.json.flags & F_UTF8 |
|
|
1220 | ? dec.cur - SvPVX (string) |
|
|
1221 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1222 | |
|
|
1223 | if (offset_return) |
|
|
1224 | *offset_return = offset; |
|
|
1225 | } |
|
|
1226 | |
971 | if (!sv) |
1227 | if (!sv) |
972 | { |
1228 | { |
973 | IV offset = dec.flags & F_UTF8 |
|
|
974 | ? dec.cur - SvPVX (string) |
|
|
975 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
976 | SV *uni = sv_newmortal (); |
1229 | SV *uni = sv_newmortal (); |
977 | |
1230 | |
978 | // horrible hack to silence warning inside pv_uni_display |
1231 | // horrible hack to silence warning inside pv_uni_display |
979 | COP cop = *PL_curcop; |
1232 | COP cop = *PL_curcop; |
980 | cop.cop_warnings = pWARN_NONE; |
1233 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
982 | SAVEVPTR (PL_curcop); |
1235 | SAVEVPTR (PL_curcop); |
983 | PL_curcop = &cop; |
1236 | PL_curcop = &cop; |
984 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1237 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
985 | LEAVE; |
1238 | LEAVE; |
986 | |
1239 | |
987 | croak ("%s, at character offset %d (%s)", |
1240 | croak ("%s, at character offset %d [\"%s\"]", |
988 | dec.err, |
1241 | dec.err, |
989 | (int)offset, |
1242 | (int)offset, |
990 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1243 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
991 | } |
1244 | } |
992 | |
1245 | |
993 | sv = sv_2mortal (sv); |
1246 | sv = sv_2mortal (sv); |
994 | |
1247 | |
995 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1248 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
996 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1249 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
997 | |
1250 | |
998 | return sv; |
1251 | return sv; |
999 | } |
1252 | } |
1000 | |
1253 | |
… | |
… | |
1004 | MODULE = JSON::XS PACKAGE = JSON::XS |
1257 | MODULE = JSON::XS PACKAGE = JSON::XS |
1005 | |
1258 | |
1006 | BOOT: |
1259 | BOOT: |
1007 | { |
1260 | { |
1008 | int i; |
1261 | int i; |
1009 | |
|
|
1010 | memset (decode_hexdigit, 0xff, 256); |
|
|
1011 | |
1262 | |
1012 | for (i = 0; i < 256; ++i) |
1263 | for (i = 0; i < 256; ++i) |
1013 | decode_hexdigit [i] = |
1264 | decode_hexdigit [i] = |
1014 | i >= '0' && i <= '9' ? i - '0' |
1265 | i >= '0' && i <= '9' ? i - '0' |
1015 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1266 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1016 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1267 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1017 | : -1; |
1268 | : -1; |
1018 | |
1269 | |
1019 | json_stash = gv_stashpv ("JSON::XS", 1); |
1270 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1271 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1272 | |
|
|
1273 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1274 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1020 | } |
1275 | } |
1021 | |
1276 | |
1022 | PROTOTYPES: DISABLE |
1277 | PROTOTYPES: DISABLE |
1023 | |
1278 | |
1024 | SV *new (char *dummy) |
1279 | SV *new (char *dummy) |
… | |
… | |
1027 | OUTPUT: |
1282 | OUTPUT: |
1028 | RETVAL |
1283 | RETVAL |
1029 | |
1284 | |
1030 | SV *ascii (SV *self, int enable = 1) |
1285 | SV *ascii (SV *self, int enable = 1) |
1031 | ALIAS: |
1286 | ALIAS: |
1032 | ascii = F_ASCII |
1287 | ascii = F_ASCII |
|
|
1288 | latin1 = F_LATIN1 |
1033 | utf8 = F_UTF8 |
1289 | utf8 = F_UTF8 |
1034 | indent = F_INDENT |
1290 | indent = F_INDENT |
1035 | canonical = F_CANONICAL |
1291 | canonical = F_CANONICAL |
1036 | space_before = F_SPACE_BEFORE |
1292 | space_before = F_SPACE_BEFORE |
1037 | space_after = F_SPACE_AFTER |
1293 | space_after = F_SPACE_AFTER |
1038 | pretty = F_PRETTY |
1294 | pretty = F_PRETTY |
1039 | allow_nonref = F_ALLOW_NONREF |
1295 | allow_nonref = F_ALLOW_NONREF |
1040 | shrink = F_SHRINK |
1296 | shrink = F_SHRINK |
|
|
1297 | allow_blessed = F_ALLOW_BLESSED |
|
|
1298 | convert_blessed = F_CONV_BLESSED |
1041 | CODE: |
1299 | CODE: |
1042 | { |
1300 | { |
1043 | UV *uv = SvJSON (self); |
1301 | UV *uv = SvJSON (self); |
1044 | if (enable) |
1302 | if (enable) |
1045 | *uv |= ix; |
1303 | *uv |= ix; |
… | |
… | |
1049 | RETVAL = newSVsv (self); |
1307 | RETVAL = newSVsv (self); |
1050 | } |
1308 | } |
1051 | OUTPUT: |
1309 | OUTPUT: |
1052 | RETVAL |
1310 | RETVAL |
1053 | |
1311 | |
1054 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
1312 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1055 | CODE: |
1313 | CODE: |
1056 | { |
1314 | { |
1057 | UV *uv = SvJSON (self); |
1315 | UV *uv = SvJSON (self); |
1058 | UV log2 = 0; |
1316 | UV log2 = 0; |
1059 | |
1317 | |
… | |
… | |
1067 | RETVAL = newSVsv (self); |
1325 | RETVAL = newSVsv (self); |
1068 | } |
1326 | } |
1069 | OUTPUT: |
1327 | OUTPUT: |
1070 | RETVAL |
1328 | RETVAL |
1071 | |
1329 | |
|
|
1330 | SV *max_size (SV *self, UV max_size = 0) |
|
|
1331 | CODE: |
|
|
1332 | { |
|
|
1333 | UV *uv = SvJSON (self); |
|
|
1334 | UV log2 = 0; |
|
|
1335 | |
|
|
1336 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1337 | if (max_size == 1) max_size = 2; |
|
|
1338 | |
|
|
1339 | while ((1UL << log2) < max_size) |
|
|
1340 | ++log2; |
|
|
1341 | |
|
|
1342 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1343 | |
|
|
1344 | RETVAL = newSVsv (self); |
|
|
1345 | } |
|
|
1346 | OUTPUT: |
|
|
1347 | RETVAL |
|
|
1348 | |
1072 | void encode (SV *self, SV *scalar) |
1349 | void encode (SV *self, SV *scalar) |
1073 | PPCODE: |
1350 | PPCODE: |
|
|
1351 | { |
|
|
1352 | struct json json = { *SvJSON (self) }; |
1074 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1353 | XPUSHs (encode_json (scalar, &json)); |
|
|
1354 | } |
1075 | |
1355 | |
1076 | void decode (SV *self, SV *jsonstr) |
1356 | void decode (SV *self, SV *jsonstr) |
1077 | PPCODE: |
1357 | PPCODE: |
|
|
1358 | { |
|
|
1359 | struct json json = { *SvJSON (self) }; |
1078 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1360 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1361 | } |
|
|
1362 | |
|
|
1363 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1364 | PPCODE: |
|
|
1365 | { |
|
|
1366 | UV offset; |
|
|
1367 | struct json json = { *SvJSON (self) }; |
|
|
1368 | EXTEND (SP, 2); |
|
|
1369 | PUSHs (decode_json (jsonstr, &json, &offset)); |
|
|
1370 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1371 | } |
1079 | |
1372 | |
1080 | PROTOTYPES: ENABLE |
1373 | PROTOTYPES: ENABLE |
1081 | |
1374 | |
1082 | void to_json (SV *scalar) |
1375 | void to_json (SV *scalar) |
1083 | ALIAS: |
|
|
1084 | objToJson = 0 |
|
|
1085 | PPCODE: |
1376 | PPCODE: |
|
|
1377 | { |
|
|
1378 | struct json json = { F_DEFAULT | F_UTF8 }; |
1086 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1379 | XPUSHs (encode_json (scalar, &json)); |
|
|
1380 | } |
1087 | |
1381 | |
1088 | void from_json (SV *jsonstr) |
1382 | void from_json (SV *jsonstr) |
1089 | ALIAS: |
|
|
1090 | jsonToObj = 0 |
|
|
1091 | PPCODE: |
1383 | PPCODE: |
|
|
1384 | { |
|
|
1385 | struct json json = { F_DEFAULT | F_UTF8 }; |
1092 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1386 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1387 | } |
1093 | |
1388 | |