1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
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 | #include <stdio.h> |
|
|
9 | #include <limits.h> |
|
|
10 | #include <float.h> |
9 | |
11 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
14 | #endif |
13 | |
15 | |
14 | // some old perls do not have this, try to make it work, no |
16 | // 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. |
17 | // guarentees, though. if it breaks, you get to keep the pieces. |
16 | #ifndef UTF8_MAXBYTES |
18 | #ifndef UTF8_MAXBYTES |
17 | # define UTF8_MAXBYTES 13 |
19 | # define UTF8_MAXBYTES 13 |
18 | #endif |
20 | #endif |
|
|
21 | |
|
|
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
19 | |
23 | |
20 | #define F_ASCII 0x00000001UL |
24 | #define F_ASCII 0x00000001UL |
21 | #define F_LATIN1 0x00000002UL |
25 | #define F_LATIN1 0x00000002UL |
22 | #define F_UTF8 0x00000004UL |
26 | #define F_UTF8 0x00000004UL |
23 | #define F_INDENT 0x00000008UL |
27 | #define F_INDENT 0x00000008UL |
… | |
… | |
26 | #define F_SPACE_AFTER 0x00000040UL |
30 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
31 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_SHRINK 0x00000200UL |
32 | #define F_SHRINK 0x00000200UL |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
33 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_CONV_BLESSED 0x00000800UL |
34 | #define F_CONV_BLESSED 0x00000800UL |
|
|
35 | #define F_RELAXED 0x00001000UL |
|
|
36 | |
31 | #define F_MAXDEPTH 0xf8000000UL |
37 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define S_MAXDEPTH 27 |
38 | #define S_MAXDEPTH 27 |
33 | #define F_MAXSIZE 0x01f00000UL |
39 | #define F_MAXSIZE 0x01f00000UL |
34 | #define S_MAXSIZE 20 |
40 | #define S_MAXSIZE 20 |
35 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
41 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
… | |
… | |
47 | |
53 | |
48 | #define SB do { |
54 | #define SB do { |
49 | #define SE } while (0) |
55 | #define SE } while (0) |
50 | |
56 | |
51 | #if __GNUC__ >= 3 |
57 | #if __GNUC__ >= 3 |
52 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
58 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
53 | # define inline inline |
59 | # define INLINE static inline |
54 | #else |
60 | #else |
55 | # define expect(expr,value) (expr) |
61 | # define expect(expr,value) (expr) |
56 | # define inline static |
62 | # define INLINE static |
57 | #endif |
63 | #endif |
58 | |
64 | |
59 | #define expect_false(expr) expect ((expr) != 0, 0) |
65 | #define expect_false(expr) expect ((expr) != 0, 0) |
60 | #define expect_true(expr) expect ((expr) != 0, 1) |
66 | #define expect_true(expr) expect ((expr) != 0, 1) |
61 | |
67 | |
|
|
68 | #define IN_RANGE_INC(type,val,beg,end) \ |
|
|
69 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
|
|
70 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
|
|
71 | |
|
|
72 | #ifdef USE_ITHREADS |
|
|
73 | # define JSON_SLOW 1 |
|
|
74 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
75 | #else |
|
|
76 | # define JSON_SLOW 0 |
|
|
77 | # define JSON_STASH json_stash |
|
|
78 | #endif |
|
|
79 | |
62 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
80 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
63 | static SV *json_true, *json_false; |
81 | static SV *json_true, *json_false; |
|
|
82 | |
|
|
83 | enum { |
|
|
84 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
|
|
85 | INCR_M_STR, // inside string |
|
|
86 | INCR_M_BS, // inside backslash |
|
|
87 | INCR_M_JSON // outside anything, count nesting |
|
|
88 | }; |
|
|
89 | |
|
|
90 | #define INCR_DONE(json) (!(json)->incr_nest && (json)->incr_mode == INCR_M_JSON) |
64 | |
91 | |
65 | typedef struct { |
92 | typedef struct { |
66 | U32 flags; |
93 | U32 flags; |
67 | SV *cb_object; |
94 | SV *cb_object; |
68 | HV *cb_sk_object; |
95 | HV *cb_sk_object; |
|
|
96 | |
|
|
97 | // for the incremental parser |
|
|
98 | SV *incr_text; // the source text so far |
|
|
99 | STRLEN incr_pos; // the current offset into the text |
|
|
100 | int incr_nest; // {[]}-nesting level |
|
|
101 | int incr_mode; |
69 | } JSON; |
102 | } JSON; |
70 | |
103 | |
71 | ///////////////////////////////////////////////////////////////////////////// |
104 | ///////////////////////////////////////////////////////////////////////////// |
72 | // utility functions |
105 | // utility functions |
73 | |
106 | |
74 | inline void |
107 | INLINE void |
75 | shrink (SV *sv) |
108 | shrink (SV *sv) |
76 | { |
109 | { |
77 | sv_utf8_downgrade (sv, 1); |
110 | sv_utf8_downgrade (sv, 1); |
78 | if (SvLEN (sv) > SvCUR (sv) + 1) |
111 | if (SvLEN (sv) > SvCUR (sv) + 1) |
79 | { |
112 | { |
… | |
… | |
88 | // decode an utf-8 character and return it, or (UV)-1 in |
121 | // decode an utf-8 character and return it, or (UV)-1 in |
89 | // case of an error. |
122 | // case of an error. |
90 | // we special-case "safe" characters from U+80 .. U+7FF, |
123 | // we special-case "safe" characters from U+80 .. U+7FF, |
91 | // but use the very good perl function to parse anything else. |
124 | // but use the very good perl function to parse anything else. |
92 | // note that we never call this function for a ascii codepoints |
125 | // note that we never call this function for a ascii codepoints |
93 | inline UV |
126 | INLINE UV |
94 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
127 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
95 | { |
128 | { |
96 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
129 | if (expect_true (len >= 2 |
97 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
130 | && IN_RANGE_INC (char, s[0], 0xc2, 0xdf) |
98 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
131 | && IN_RANGE_INC (char, s[1], 0x80, 0xbf))) |
99 | { |
132 | { |
100 | *clen = 2; |
133 | *clen = 2; |
101 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
134 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
102 | } |
135 | } |
103 | else |
136 | else |
104 | { |
137 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
105 | *clen = (STRLEN)-1; |
138 | } |
106 | return (UV)-1; |
139 | |
107 | } |
140 | // likewise for encoding, also never called for ascii codepoints |
|
|
141 | // this function takes advantage of this fact, although current gccs |
|
|
142 | // seem to optimise the check for >= 0x80 away anyways |
|
|
143 | INLINE unsigned char * |
|
|
144 | encode_utf8 (unsigned char *s, UV ch) |
|
|
145 | { |
|
|
146 | if (expect_false (ch < 0x000080)) |
|
|
147 | *s++ = ch; |
|
|
148 | else if (expect_true (ch < 0x000800)) |
|
|
149 | *s++ = 0xc0 | ( ch >> 6), |
|
|
150 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
151 | else if ( ch < 0x010000) |
|
|
152 | *s++ = 0xe0 | ( ch >> 12), |
|
|
153 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
154 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
155 | else if ( ch < 0x110000) |
|
|
156 | *s++ = 0xf0 | ( ch >> 18), |
|
|
157 | *s++ = 0x80 | ((ch >> 12) & 0x3f), |
|
|
158 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
159 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
160 | |
|
|
161 | return s; |
108 | } |
162 | } |
109 | |
163 | |
110 | ///////////////////////////////////////////////////////////////////////////// |
164 | ///////////////////////////////////////////////////////////////////////////// |
111 | // encoder |
165 | // encoder |
112 | |
166 | |
… | |
… | |
117 | char *end; // SvEND (sv) |
171 | char *end; // SvEND (sv) |
118 | SV *sv; // result scalar |
172 | SV *sv; // result scalar |
119 | JSON json; |
173 | JSON json; |
120 | U32 indent; // indentation level |
174 | U32 indent; // indentation level |
121 | U32 maxdepth; // max. indentation/recursion level |
175 | U32 maxdepth; // max. indentation/recursion level |
|
|
176 | UV limit; // escape character values >= this value when encoding |
122 | } enc_t; |
177 | } enc_t; |
123 | |
178 | |
124 | inline void |
179 | INLINE void |
125 | need (enc_t *enc, STRLEN len) |
180 | need (enc_t *enc, STRLEN len) |
126 | { |
181 | { |
127 | if (expect_false (enc->cur + len >= enc->end)) |
182 | if (expect_false (enc->cur + len >= enc->end)) |
128 | { |
183 | { |
129 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
184 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
… | |
… | |
131 | enc->cur = SvPVX (enc->sv) + cur; |
186 | enc->cur = SvPVX (enc->sv) + cur; |
132 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
187 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
133 | } |
188 | } |
134 | } |
189 | } |
135 | |
190 | |
136 | inline void |
191 | INLINE void |
137 | encode_ch (enc_t *enc, char ch) |
192 | encode_ch (enc_t *enc, char ch) |
138 | { |
193 | { |
139 | need (enc, 1); |
194 | need (enc, 1); |
140 | *enc->cur++ = ch; |
195 | *enc->cur++ = ch; |
141 | } |
196 | } |
… | |
… | |
195 | { |
250 | { |
196 | uch = ch; |
251 | uch = ch; |
197 | clen = 1; |
252 | clen = 1; |
198 | } |
253 | } |
199 | |
254 | |
200 | if (uch > 0x10FFFFUL) |
255 | if (uch < 0x80/*0x20*/ || uch >= enc->limit) |
201 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
202 | |
|
|
203 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
|
|
204 | { |
256 | { |
205 | if (uch > 0xFFFFUL) |
257 | if (uch >= 0x10000UL) |
206 | { |
258 | { |
|
|
259 | if (uch >= 0x110000UL) |
|
|
260 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
261 | |
207 | need (enc, len += 11); |
262 | need (enc, len += 11); |
208 | sprintf (enc->cur, "\\u%04x\\u%04x", |
263 | sprintf (enc->cur, "\\u%04x\\u%04x", |
209 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
264 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
210 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
265 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
211 | enc->cur += 12; |
266 | enc->cur += 12; |
… | |
… | |
239 | while (--clen); |
294 | while (--clen); |
240 | } |
295 | } |
241 | else |
296 | else |
242 | { |
297 | { |
243 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
298 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
244 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
299 | enc->cur = encode_utf8 (enc->cur, uch); |
245 | ++str; |
300 | ++str; |
246 | } |
301 | } |
247 | } |
302 | } |
248 | } |
303 | } |
249 | } |
304 | } |
250 | |
305 | |
251 | --len; |
306 | --len; |
252 | } |
307 | } |
253 | } |
308 | } |
254 | |
309 | |
255 | inline void |
310 | INLINE void |
256 | encode_indent (enc_t *enc) |
311 | encode_indent (enc_t *enc) |
257 | { |
312 | { |
258 | if (enc->json.flags & F_INDENT) |
313 | if (enc->json.flags & F_INDENT) |
259 | { |
314 | { |
260 | int spaces = enc->indent * INDENT_STEP; |
315 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
263 | memset (enc->cur, ' ', spaces); |
318 | memset (enc->cur, ' ', spaces); |
264 | enc->cur += spaces; |
319 | enc->cur += spaces; |
265 | } |
320 | } |
266 | } |
321 | } |
267 | |
322 | |
268 | inline void |
323 | INLINE void |
269 | encode_space (enc_t *enc) |
324 | encode_space (enc_t *enc) |
270 | { |
325 | { |
271 | need (enc, 1); |
326 | need (enc, 1); |
272 | encode_ch (enc, ' '); |
327 | encode_ch (enc, ' '); |
273 | } |
328 | } |
274 | |
329 | |
275 | inline void |
330 | INLINE void |
276 | encode_nl (enc_t *enc) |
331 | encode_nl (enc_t *enc) |
277 | { |
332 | { |
278 | if (enc->json.flags & F_INDENT) |
333 | if (enc->json.flags & F_INDENT) |
279 | { |
334 | { |
280 | need (enc, 1); |
335 | need (enc, 1); |
281 | encode_ch (enc, '\n'); |
336 | encode_ch (enc, '\n'); |
282 | } |
337 | } |
283 | } |
338 | } |
284 | |
339 | |
285 | inline void |
340 | INLINE void |
286 | encode_comma (enc_t *enc) |
341 | encode_comma (enc_t *enc) |
287 | { |
342 | { |
288 | encode_ch (enc, ','); |
343 | encode_ch (enc, ','); |
289 | |
344 | |
290 | if (enc->json.flags & F_INDENT) |
345 | if (enc->json.flags & F_INDENT) |
… | |
… | |
301 | int i, len = av_len (av); |
356 | int i, len = av_len (av); |
302 | |
357 | |
303 | if (enc->indent >= enc->maxdepth) |
358 | if (enc->indent >= enc->maxdepth) |
304 | croak ("data structure too deep (hit recursion limit)"); |
359 | croak ("data structure too deep (hit recursion limit)"); |
305 | |
360 | |
306 | encode_ch (enc, '['); encode_nl (enc); |
361 | encode_ch (enc, '['); |
307 | ++enc->indent; |
362 | |
|
|
363 | if (len >= 0) |
|
|
364 | { |
|
|
365 | encode_nl (enc); ++enc->indent; |
308 | |
366 | |
309 | for (i = 0; i <= len; ++i) |
367 | for (i = 0; i <= len; ++i) |
310 | { |
368 | { |
|
|
369 | SV **svp = av_fetch (av, i, 0); |
|
|
370 | |
311 | encode_indent (enc); |
371 | encode_indent (enc); |
312 | encode_sv (enc, *av_fetch (av, i, 0)); |
|
|
313 | |
372 | |
|
|
373 | if (svp) |
|
|
374 | encode_sv (enc, *svp); |
|
|
375 | else |
|
|
376 | encode_str (enc, "null", 4, 0); |
|
|
377 | |
314 | if (i < len) |
378 | if (i < len) |
315 | encode_comma (enc); |
379 | encode_comma (enc); |
316 | } |
380 | } |
317 | |
381 | |
|
|
382 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
383 | } |
|
|
384 | |
318 | encode_nl (enc); |
385 | encode_ch (enc, ']'); |
319 | |
|
|
320 | --enc->indent; |
|
|
321 | encode_indent (enc); encode_ch (enc, ']'); |
|
|
322 | } |
386 | } |
323 | |
387 | |
324 | static void |
388 | static void |
325 | encode_he (enc_t *enc, HE *he) |
389 | encode_hk (enc_t *enc, HE *he) |
326 | { |
390 | { |
327 | encode_ch (enc, '"'); |
391 | encode_ch (enc, '"'); |
328 | |
392 | |
329 | if (HeKLEN (he) == HEf_SVKEY) |
393 | if (HeKLEN (he) == HEf_SVKEY) |
330 | { |
394 | { |
… | |
… | |
343 | encode_ch (enc, '"'); |
407 | encode_ch (enc, '"'); |
344 | |
408 | |
345 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
409 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
346 | encode_ch (enc, ':'); |
410 | encode_ch (enc, ':'); |
347 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
411 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
348 | encode_sv (enc, HeVAL (he)); |
|
|
349 | } |
412 | } |
350 | |
413 | |
351 | // compare hash entries, used when all keys are bytestrings |
414 | // compare hash entries, used when all keys are bytestrings |
352 | static int |
415 | static int |
353 | he_cmp_fast (const void *a_, const void *b_) |
416 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
358 | HE *b = *(HE **)b_; |
421 | HE *b = *(HE **)b_; |
359 | |
422 | |
360 | STRLEN la = HeKLEN (a); |
423 | STRLEN la = HeKLEN (a); |
361 | STRLEN lb = HeKLEN (b); |
424 | STRLEN lb = HeKLEN (b); |
362 | |
425 | |
363 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
426 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
364 | cmp = la - lb; |
427 | cmp = lb - la; |
365 | |
428 | |
366 | return cmp; |
429 | return cmp; |
367 | } |
430 | } |
368 | |
431 | |
369 | // compare hash entries, used when some keys are sv's or utf-x |
432 | // compare hash entries, used when some keys are sv's or utf-x |
370 | static int |
433 | static int |
371 | he_cmp_slow (const void *a, const void *b) |
434 | he_cmp_slow (const void *a, const void *b) |
372 | { |
435 | { |
373 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
436 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
374 | } |
437 | } |
375 | |
438 | |
376 | static void |
439 | static void |
377 | encode_hv (enc_t *enc, HV *hv) |
440 | encode_hv (enc_t *enc, HV *hv) |
378 | { |
441 | { |
|
|
442 | HE *he; |
379 | int count, i; |
443 | int count; |
380 | |
444 | |
381 | if (enc->indent >= enc->maxdepth) |
445 | if (enc->indent >= enc->maxdepth) |
382 | croak ("data structure too deep (hit recursion limit)"); |
446 | croak ("data structure too deep (hit recursion limit)"); |
383 | |
447 | |
384 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
448 | encode_ch (enc, '{'); |
385 | |
449 | |
386 | if ((count = hv_iterinit (hv))) |
|
|
387 | { |
|
|
388 | // for canonical output we have to sort by keys first |
450 | // for canonical output we have to sort by keys first |
389 | // actually, this is mostly due to the stupid so-called |
451 | // actually, this is mostly due to the stupid so-called |
390 | // security workaround added somewhere in 5.8.x. |
452 | // security workaround added somewhere in 5.8.x. |
391 | // that randomises hash orderings |
453 | // that randomises hash orderings |
392 | if (enc->json.flags & F_CANONICAL) |
454 | if (enc->json.flags & F_CANONICAL) |
|
|
455 | { |
|
|
456 | int count = hv_iterinit (hv); |
|
|
457 | |
|
|
458 | if (SvMAGICAL (hv)) |
393 | { |
459 | { |
|
|
460 | // need to count by iterating. could improve by dynamically building the vector below |
|
|
461 | // but I don't care for the speed of this special case. |
|
|
462 | // note also that we will run into undefined behaviour when the two iterations |
|
|
463 | // do not result in the same count, something I might care for in some later release. |
|
|
464 | |
|
|
465 | count = 0; |
|
|
466 | while (hv_iternext (hv)) |
|
|
467 | ++count; |
|
|
468 | |
|
|
469 | hv_iterinit (hv); |
|
|
470 | } |
|
|
471 | |
|
|
472 | if (count) |
|
|
473 | { |
394 | int fast = 1; |
474 | int i, fast = 1; |
395 | HE *he; |
|
|
396 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
475 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
397 | HE **hes = _alloca (count * sizeof (HE)); |
476 | HE **hes = _alloca (count * sizeof (HE)); |
398 | #else |
477 | #else |
399 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
478 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
400 | #endif |
479 | #endif |
… | |
… | |
427 | |
506 | |
428 | FREETMPS; |
507 | FREETMPS; |
429 | LEAVE; |
508 | LEAVE; |
430 | } |
509 | } |
431 | |
510 | |
432 | for (i = 0; i < count; ++i) |
511 | encode_nl (enc); ++enc->indent; |
|
|
512 | |
|
|
513 | while (count--) |
433 | { |
514 | { |
434 | encode_indent (enc); |
515 | encode_indent (enc); |
|
|
516 | he = hes [count]; |
435 | encode_he (enc, hes [i]); |
517 | encode_hk (enc, he); |
|
|
518 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
436 | |
519 | |
437 | if (i < count - 1) |
520 | if (count) |
438 | encode_comma (enc); |
521 | encode_comma (enc); |
439 | } |
522 | } |
440 | |
523 | |
441 | encode_nl (enc); |
524 | encode_nl (enc); --enc->indent; encode_indent (enc); |
442 | } |
525 | } |
|
|
526 | } |
443 | else |
527 | else |
|
|
528 | { |
|
|
529 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
|
|
530 | if ((he = hv_iternext (hv))) |
444 | { |
531 | { |
445 | HE *he = hv_iternext (hv); |
532 | encode_nl (enc); ++enc->indent; |
446 | |
533 | |
447 | for (;;) |
534 | for (;;) |
448 | { |
535 | { |
449 | encode_indent (enc); |
536 | encode_indent (enc); |
450 | encode_he (enc, he); |
537 | encode_hk (enc, he); |
|
|
538 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
451 | |
539 | |
452 | if (!(he = hv_iternext (hv))) |
540 | if (!(he = hv_iternext (hv))) |
453 | break; |
541 | break; |
454 | |
542 | |
455 | encode_comma (enc); |
543 | encode_comma (enc); |
456 | } |
544 | } |
457 | |
545 | |
458 | encode_nl (enc); |
546 | encode_nl (enc); --enc->indent; encode_indent (enc); |
459 | } |
547 | } |
460 | } |
548 | } |
461 | |
549 | |
462 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
550 | encode_ch (enc, '}'); |
463 | } |
551 | } |
464 | |
552 | |
465 | // encode objects, arrays and special \0=false and \1=true values. |
553 | // encode objects, arrays and special \0=false and \1=true values. |
466 | static void |
554 | static void |
467 | encode_rv (enc_t *enc, SV *sv) |
555 | encode_rv (enc_t *enc, SV *sv) |
… | |
… | |
471 | SvGETMAGIC (sv); |
559 | SvGETMAGIC (sv); |
472 | svt = SvTYPE (sv); |
560 | svt = SvTYPE (sv); |
473 | |
561 | |
474 | if (expect_false (SvOBJECT (sv))) |
562 | if (expect_false (SvOBJECT (sv))) |
475 | { |
563 | { |
|
|
564 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
565 | ? json_boolean_stash |
|
|
566 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
567 | |
476 | if (SvSTASH (sv) == json_boolean_stash) |
568 | if (SvSTASH (sv) == stash) |
477 | { |
569 | { |
478 | if (SvIV (sv)) |
570 | if (SvIV (sv)) |
479 | encode_str (enc, "true", 4, 0); |
571 | encode_str (enc, "true", 4, 0); |
480 | else |
572 | else |
481 | encode_str (enc, "false", 5, 0); |
573 | encode_str (enc, "false", 5, 0); |
… | |
… | |
493 | // we re-bless the reference to get overload and other niceties right |
585 | // we re-bless the reference to get overload and other niceties right |
494 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
586 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
495 | |
587 | |
496 | if (to_json) |
588 | if (to_json) |
497 | { |
589 | { |
498 | int count; |
|
|
499 | dSP; |
590 | dSP; |
500 | |
591 | |
501 | ENTER; SAVETMPS; PUSHMARK (SP); |
592 | ENTER; SAVETMPS; PUSHMARK (SP); |
502 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
593 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
503 | |
594 | |
… | |
… | |
572 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
663 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
573 | enc->cur += strlen (enc->cur); |
664 | enc->cur += strlen (enc->cur); |
574 | } |
665 | } |
575 | else if (SvIOKp (sv)) |
666 | else if (SvIOKp (sv)) |
576 | { |
667 | { |
577 | // we assume we can always read an IV as a UV |
668 | // we assume we can always read an IV as a UV and vice versa |
578 | if (SvUV (sv) & ~(UV)0x7fff) |
669 | // we assume two's complement |
579 | { |
670 | // we assume no aliasing issues in the union |
580 | // large integer, use the (rather slow) snprintf way. |
671 | if (SvIsUV (sv) ? SvUVX (sv) <= 59000 |
581 | need (enc, sizeof (UV) * 3); |
672 | : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000) |
582 | enc->cur += |
|
|
583 | SvIsUV(sv) |
|
|
584 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
|
|
585 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
586 | } |
|
|
587 | else |
|
|
588 | { |
673 | { |
589 | // optimise the "small number case" |
674 | // optimise the "small number case" |
590 | // code will likely be branchless and use only a single multiplication |
675 | // code will likely be branchless and use only a single multiplication |
|
|
676 | // works for numbers up to 59074 |
591 | I32 i = SvIV (sv); |
677 | I32 i = SvIVX (sv); |
592 | U32 u; |
678 | U32 u; |
593 | char digit, nz = 0; |
679 | char digit, nz = 0; |
594 | |
680 | |
595 | need (enc, 6); |
681 | need (enc, 6); |
596 | |
682 | |
… | |
… | |
602 | |
688 | |
603 | // now output digit by digit, each time masking out the integer part |
689 | // now output digit by digit, each time masking out the integer part |
604 | // and multiplying by 5 while moving the decimal point one to the right, |
690 | // and multiplying by 5 while moving the decimal point one to the right, |
605 | // resulting in a net multiplication by 10. |
691 | // resulting in a net multiplication by 10. |
606 | // we always write the digit to memory but conditionally increment |
692 | // we always write the digit to memory but conditionally increment |
607 | // the pointer, to ease the usage of conditional move instructions. |
693 | // the pointer, to enable the use of conditional move instructions. |
608 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
694 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; |
609 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
695 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; |
610 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
696 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; |
611 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
697 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; |
612 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
698 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
699 | } |
|
|
700 | else |
|
|
701 | { |
|
|
702 | // large integer, use the (rather slow) snprintf way. |
|
|
703 | need (enc, IVUV_MAXCHARS); |
|
|
704 | enc->cur += |
|
|
705 | SvIsUV(sv) |
|
|
706 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
|
|
707 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
613 | } |
708 | } |
614 | } |
709 | } |
615 | else if (SvROK (sv)) |
710 | else if (SvROK (sv)) |
616 | encode_rv (enc, SvRV (sv)); |
711 | encode_rv (enc, SvRV (sv)); |
617 | else if (!SvOK (sv)) |
712 | else if (!SvOK (sv)) |
… | |
… | |
633 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
728 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
634 | enc.cur = SvPVX (enc.sv); |
729 | enc.cur = SvPVX (enc.sv); |
635 | enc.end = SvEND (enc.sv); |
730 | enc.end = SvEND (enc.sv); |
636 | enc.indent = 0; |
731 | enc.indent = 0; |
637 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
732 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
|
|
733 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
|
|
734 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
|
|
735 | : 0x110000UL; |
638 | |
736 | |
639 | SvPOK_only (enc.sv); |
737 | SvPOK_only (enc.sv); |
640 | encode_sv (&enc, scalar); |
738 | encode_sv (&enc, scalar); |
641 | |
739 | |
642 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
740 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
… | |
… | |
663 | JSON json; |
761 | JSON json; |
664 | U32 depth; // recursion depth |
762 | U32 depth; // recursion depth |
665 | U32 maxdepth; // recursion depth limit |
763 | U32 maxdepth; // recursion depth limit |
666 | } dec_t; |
764 | } dec_t; |
667 | |
765 | |
668 | inline void |
766 | INLINE void |
|
|
767 | decode_comment (dec_t *dec) |
|
|
768 | { |
|
|
769 | // only '#'-style comments allowed a.t.m. |
|
|
770 | |
|
|
771 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
|
|
772 | ++dec->cur; |
|
|
773 | } |
|
|
774 | |
|
|
775 | INLINE void |
669 | decode_ws (dec_t *dec) |
776 | decode_ws (dec_t *dec) |
670 | { |
777 | { |
671 | for (;;) |
778 | for (;;) |
672 | { |
779 | { |
673 | char ch = *dec->cur; |
780 | char ch = *dec->cur; |
674 | |
781 | |
675 | if (ch > 0x20 |
782 | if (ch > 0x20) |
|
|
783 | { |
|
|
784 | if (expect_false (ch == '#')) |
|
|
785 | { |
|
|
786 | if (dec->json.flags & F_RELAXED) |
|
|
787 | decode_comment (dec); |
|
|
788 | else |
|
|
789 | break; |
|
|
790 | } |
|
|
791 | else |
|
|
792 | break; |
|
|
793 | } |
676 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
794 | else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
677 | break; |
795 | break; // parse error, but let higher level handle it, gives better error messages |
678 | |
796 | |
679 | ++dec->cur; |
797 | ++dec->cur; |
680 | } |
798 | } |
681 | } |
799 | } |
682 | |
800 | |
… | |
… | |
788 | |
906 | |
789 | if (hi >= 0x80) |
907 | if (hi >= 0x80) |
790 | { |
908 | { |
791 | utf8 = 1; |
909 | utf8 = 1; |
792 | |
910 | |
793 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
911 | cur = encode_utf8 (cur, hi); |
794 | } |
912 | } |
795 | else |
913 | else |
796 | *cur++ = hi; |
914 | *cur++ = hi; |
797 | } |
915 | } |
798 | break; |
916 | break; |
… | |
… | |
800 | default: |
918 | default: |
801 | --dec_cur; |
919 | --dec_cur; |
802 | ERR ("illegal backslash escape sequence in string"); |
920 | ERR ("illegal backslash escape sequence in string"); |
803 | } |
921 | } |
804 | } |
922 | } |
805 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
923 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
806 | *cur++ = ch; |
924 | *cur++ = ch; |
807 | else if (ch >= 0x80) |
925 | else if (ch >= 0x80) |
808 | { |
926 | { |
809 | STRLEN clen; |
927 | STRLEN clen; |
810 | UV uch; |
928 | UV uch; |
… | |
… | |
931 | is_nv = 1; |
1049 | is_nv = 1; |
932 | } |
1050 | } |
933 | |
1051 | |
934 | if (!is_nv) |
1052 | if (!is_nv) |
935 | { |
1053 | { |
|
|
1054 | int len = dec->cur - start; |
|
|
1055 | |
936 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
1056 | // special case the rather common 1..5-digit-int case |
937 | if (*start == '-') |
1057 | if (*start == '-') |
938 | switch (dec->cur - start) |
1058 | switch (len) |
939 | { |
1059 | { |
940 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1060 | case 2: return newSViv (-( start [1] - '0' * 1)); |
941 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1061 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
942 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1062 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
943 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1063 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1064 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
944 | } |
1065 | } |
945 | else |
1066 | else |
946 | switch (dec->cur - start) |
1067 | switch (len) |
947 | { |
1068 | { |
948 | case 1: return newSViv ( start [0] - '0' * 1); |
1069 | case 1: return newSViv ( start [0] - '0' * 1); |
949 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1070 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
950 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1071 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
951 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1072 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1073 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
952 | } |
1074 | } |
953 | |
1075 | |
954 | { |
1076 | { |
955 | UV uv; |
1077 | UV uv; |
956 | int numtype = grok_number (start, dec->cur - start, &uv); |
1078 | int numtype = grok_number (start, len, &uv); |
957 | if (numtype & IS_NUMBER_IN_UV) |
1079 | if (numtype & IS_NUMBER_IN_UV) |
958 | if (numtype & IS_NUMBER_NEG) |
1080 | if (numtype & IS_NUMBER_NEG) |
959 | { |
1081 | { |
960 | if (uv < (UV)IV_MIN) |
1082 | if (uv < (UV)IV_MIN) |
961 | return newSViv (-(IV)uv); |
1083 | return newSViv (-(IV)uv); |
962 | } |
1084 | } |
963 | else |
1085 | else |
964 | return newSVuv (uv); |
1086 | return newSVuv (uv); |
965 | |
|
|
966 | // here would likely be the place for bigint support |
|
|
967 | } |
1087 | } |
968 | } |
|
|
969 | |
1088 | |
970 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
1089 | len -= *start == '-' ? 1 : 0; |
|
|
1090 | |
|
|
1091 | // does not fit into IV or UV, try NV |
|
|
1092 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
1093 | #if defined (LDBL_DIG) |
|
|
1094 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1095 | #endif |
|
|
1096 | ) |
|
|
1097 | // fits into NV without loss of precision |
|
|
1098 | return newSVnv (Atof (start)); |
|
|
1099 | |
|
|
1100 | // everything else fails, convert it to a string |
|
|
1101 | return newSVpvn (start, dec->cur - start); |
|
|
1102 | } |
|
|
1103 | |
|
|
1104 | // loss of precision here |
971 | return newSVnv (Atof (start)); |
1105 | return newSVnv (Atof (start)); |
972 | |
1106 | |
973 | fail: |
1107 | fail: |
974 | return 0; |
1108 | return 0; |
975 | } |
1109 | } |
… | |
… | |
1005 | |
1139 | |
1006 | if (*dec->cur != ',') |
1140 | if (*dec->cur != ',') |
1007 | ERR (", or ] expected while parsing array"); |
1141 | ERR (", or ] expected while parsing array"); |
1008 | |
1142 | |
1009 | ++dec->cur; |
1143 | ++dec->cur; |
|
|
1144 | |
|
|
1145 | decode_ws (dec); |
|
|
1146 | |
|
|
1147 | if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
|
|
1148 | { |
|
|
1149 | ++dec->cur; |
|
|
1150 | break; |
|
|
1151 | } |
1010 | } |
1152 | } |
1011 | |
1153 | |
1012 | DEC_DEC_DEPTH; |
1154 | DEC_DEC_DEPTH; |
1013 | return newRV_noinc ((SV *)av); |
1155 | return newRV_noinc ((SV *)av); |
1014 | |
1156 | |
… | |
… | |
1030 | if (*dec->cur == '}') |
1172 | if (*dec->cur == '}') |
1031 | ++dec->cur; |
1173 | ++dec->cur; |
1032 | else |
1174 | else |
1033 | for (;;) |
1175 | for (;;) |
1034 | { |
1176 | { |
1035 | decode_ws (dec); EXPECT_CH ('"'); |
1177 | EXPECT_CH ('"'); |
1036 | |
1178 | |
1037 | // heuristic: assume that |
1179 | // heuristic: assume that |
1038 | // a) decode_str + hv_store_ent are abysmally slow. |
1180 | // a) decode_str + hv_store_ent are abysmally slow. |
1039 | // b) most hash keys are short, simple ascii text. |
1181 | // b) most hash keys are short, simple ascii text. |
1040 | // => try to "fast-match" such strings to avoid |
1182 | // => try to "fast-match" such strings to avoid |
… | |
… | |
1044 | char *p = dec->cur; |
1186 | char *p = dec->cur; |
1045 | char *e = p + 24; // only try up to 24 bytes |
1187 | char *e = p + 24; // only try up to 24 bytes |
1046 | |
1188 | |
1047 | for (;;) |
1189 | for (;;) |
1048 | { |
1190 | { |
1049 | // the >= 0x80 is true on most architectures |
1191 | // the >= 0x80 is false on most architectures |
1050 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1192 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1051 | { |
1193 | { |
1052 | // slow path, back up and use decode_str |
1194 | // slow path, back up and use decode_str |
1053 | SV *key = decode_str (dec); |
1195 | SV *key = decode_str (dec); |
1054 | if (!key) |
1196 | if (!key) |
1055 | goto fail; |
1197 | goto fail; |
1056 | |
1198 | |
1057 | decode_ws (dec); EXPECT_CH (':'); |
1199 | decode_ws (dec); EXPECT_CH (':'); |
1058 | |
1200 | |
|
|
1201 | decode_ws (dec); |
1059 | value = decode_sv (dec); |
1202 | value = decode_sv (dec); |
1060 | if (!value) |
1203 | if (!value) |
1061 | { |
1204 | { |
1062 | SvREFCNT_dec (key); |
1205 | SvREFCNT_dec (key); |
1063 | goto fail; |
1206 | goto fail; |
… | |
… | |
1075 | int len = p - key; |
1218 | int len = p - key; |
1076 | dec->cur = p + 1; |
1219 | dec->cur = p + 1; |
1077 | |
1220 | |
1078 | decode_ws (dec); EXPECT_CH (':'); |
1221 | decode_ws (dec); EXPECT_CH (':'); |
1079 | |
1222 | |
|
|
1223 | decode_ws (dec); |
1080 | value = decode_sv (dec); |
1224 | value = decode_sv (dec); |
1081 | if (!value) |
1225 | if (!value) |
1082 | goto fail; |
1226 | goto fail; |
1083 | |
1227 | |
1084 | hv_store (hv, key, len, value, 0); |
1228 | hv_store (hv, key, len, value, 0); |
… | |
… | |
1100 | |
1244 | |
1101 | if (*dec->cur != ',') |
1245 | if (*dec->cur != ',') |
1102 | ERR (", or } expected while parsing object/hash"); |
1246 | ERR (", or } expected while parsing object/hash"); |
1103 | |
1247 | |
1104 | ++dec->cur; |
1248 | ++dec->cur; |
|
|
1249 | |
|
|
1250 | decode_ws (dec); |
|
|
1251 | |
|
|
1252 | if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
|
|
1253 | { |
|
|
1254 | ++dec->cur; |
|
|
1255 | break; |
|
|
1256 | } |
1105 | } |
1257 | } |
1106 | |
1258 | |
1107 | DEC_DEC_DEPTH; |
1259 | DEC_DEC_DEPTH; |
1108 | sv = newRV_noinc ((SV *)hv); |
1260 | sv = newRV_noinc ((SV *)hv); |
1109 | |
1261 | |
… | |
… | |
1174 | } |
1326 | } |
1175 | |
1327 | |
1176 | static SV * |
1328 | static SV * |
1177 | decode_sv (dec_t *dec) |
1329 | decode_sv (dec_t *dec) |
1178 | { |
1330 | { |
1179 | decode_ws (dec); |
|
|
1180 | |
|
|
1181 | // the beauty of JSON: you need exactly one character lookahead |
1331 | // the beauty of JSON: you need exactly one character lookahead |
1182 | // to parse anything. |
1332 | // to parse everything. |
1183 | switch (*dec->cur) |
1333 | switch (*dec->cur) |
1184 | { |
1334 | { |
1185 | case '"': ++dec->cur; return decode_str (dec); |
1335 | case '"': ++dec->cur; return decode_str (dec); |
1186 | case '[': ++dec->cur; return decode_av (dec); |
1336 | case '[': ++dec->cur; return decode_av (dec); |
1187 | case '{': ++dec->cur; return decode_hv (dec); |
1337 | case '{': ++dec->cur; return decode_hv (dec); |
1188 | |
1338 | |
1189 | case '-': |
1339 | case '-': |
1190 | case '0': case '1': case '2': case '3': case '4': |
1340 | case '0': case '1': case '2': case '3': case '4': |
1191 | case '5': case '6': case '7': case '8': case '9': |
1341 | case '5': case '6': case '7': case '8': case '9': |
1192 | return decode_num (dec); |
1342 | return decode_num (dec); |
1193 | |
1343 | |
1194 | case 't': |
1344 | case 't': |
1195 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1345 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1196 | { |
1346 | { |
1197 | dec->cur += 4; |
1347 | dec->cur += 4; |
|
|
1348 | #if JSON_SLOW |
|
|
1349 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
|
|
1350 | #endif |
1198 | return SvREFCNT_inc (json_true); |
1351 | return SvREFCNT_inc (json_true); |
1199 | } |
1352 | } |
1200 | else |
1353 | else |
1201 | ERR ("'true' expected"); |
1354 | ERR ("'true' expected"); |
1202 | |
1355 | |
… | |
… | |
1204 | |
1357 | |
1205 | case 'f': |
1358 | case 'f': |
1206 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1359 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1207 | { |
1360 | { |
1208 | dec->cur += 5; |
1361 | dec->cur += 5; |
|
|
1362 | #if JSON_SLOW |
|
|
1363 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
|
|
1364 | #endif |
1209 | return SvREFCNT_inc (json_false); |
1365 | return SvREFCNT_inc (json_false); |
1210 | } |
1366 | } |
1211 | else |
1367 | else |
1212 | ERR ("'false' expected"); |
1368 | ERR ("'false' expected"); |
1213 | |
1369 | |
… | |
… | |
1232 | fail: |
1388 | fail: |
1233 | return 0; |
1389 | return 0; |
1234 | } |
1390 | } |
1235 | |
1391 | |
1236 | static SV * |
1392 | static SV * |
1237 | decode_json (SV *string, JSON *json, UV *offset_return) |
1393 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1238 | { |
1394 | { |
1239 | dec_t dec; |
1395 | dec_t dec; |
1240 | UV offset; |
1396 | STRLEN offset; |
1241 | SV *sv; |
1397 | SV *sv; |
1242 | |
1398 | |
1243 | SvGETMAGIC (string); |
1399 | SvGETMAGIC (string); |
1244 | SvUPGRADE (string, SVt_PV); |
1400 | SvUPGRADE (string, SVt_PV); |
1245 | |
1401 | |
… | |
… | |
1263 | |
1419 | |
1264 | if (dec.json.cb_object || dec.json.cb_sk_object) |
1420 | if (dec.json.cb_object || dec.json.cb_sk_object) |
1265 | dec.json.flags |= F_HOOK; |
1421 | dec.json.flags |= F_HOOK; |
1266 | |
1422 | |
1267 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1423 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1424 | |
|
|
1425 | decode_ws (&dec); |
1268 | sv = decode_sv (&dec); |
1426 | sv = decode_sv (&dec); |
1269 | |
1427 | |
1270 | if (!(offset_return || !sv)) |
1428 | if (!(offset_return || !sv)) |
1271 | { |
1429 | { |
1272 | // check for trailing garbage |
1430 | // check for trailing garbage |
… | |
… | |
1316 | |
1474 | |
1317 | return sv; |
1475 | return sv; |
1318 | } |
1476 | } |
1319 | |
1477 | |
1320 | ///////////////////////////////////////////////////////////////////////////// |
1478 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1479 | // incremental parser |
|
|
1480 | |
|
|
1481 | static void |
|
|
1482 | incr_parse (JSON *self) |
|
|
1483 | { |
|
|
1484 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1485 | |
|
|
1486 | for (;;) |
|
|
1487 | { |
|
|
1488 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
|
|
1489 | switch (self->incr_mode) |
|
|
1490 | { |
|
|
1491 | // only used for intiial whitespace skipping |
|
|
1492 | case INCR_M_WS: |
|
|
1493 | for (;;) |
|
|
1494 | { |
|
|
1495 | if (*p > 0x20) |
|
|
1496 | { |
|
|
1497 | self->incr_mode = INCR_M_JSON; |
|
|
1498 | goto incr_m_json; |
|
|
1499 | } |
|
|
1500 | else if (!*p) |
|
|
1501 | goto interrupt; |
|
|
1502 | |
|
|
1503 | ++p; |
|
|
1504 | } |
|
|
1505 | |
|
|
1506 | // skip a single char inside a string (for \\-processing) |
|
|
1507 | case INCR_M_BS: |
|
|
1508 | if (!*p) |
|
|
1509 | goto interrupt; |
|
|
1510 | |
|
|
1511 | ++p; |
|
|
1512 | self->incr_mode = INCR_M_STR; |
|
|
1513 | goto incr_m_str; |
|
|
1514 | |
|
|
1515 | // inside a string |
|
|
1516 | case INCR_M_STR: |
|
|
1517 | incr_m_str: |
|
|
1518 | for (;;) |
|
|
1519 | { |
|
|
1520 | if (*p == '"') |
|
|
1521 | { |
|
|
1522 | ++p; |
|
|
1523 | self->incr_mode = INCR_M_JSON; |
|
|
1524 | |
|
|
1525 | if (!self->incr_nest) |
|
|
1526 | goto interrupt; |
|
|
1527 | |
|
|
1528 | goto incr_m_json; |
|
|
1529 | } |
|
|
1530 | else if (*p == '\\') |
|
|
1531 | { |
|
|
1532 | ++p; // "virtually" consumes character after \ |
|
|
1533 | |
|
|
1534 | if (!*p) // if at end of string we have to switch modes |
|
|
1535 | { |
|
|
1536 | self->incr_mode = INCR_M_BS; |
|
|
1537 | goto interrupt; |
|
|
1538 | } |
|
|
1539 | } |
|
|
1540 | else if (!*p) |
|
|
1541 | goto interrupt; |
|
|
1542 | |
|
|
1543 | ++p; |
|
|
1544 | } |
|
|
1545 | |
|
|
1546 | // after initial ws, outside string |
|
|
1547 | case INCR_M_JSON: |
|
|
1548 | incr_m_json: |
|
|
1549 | for (;;) |
|
|
1550 | { |
|
|
1551 | switch (*p++) |
|
|
1552 | { |
|
|
1553 | case 0: |
|
|
1554 | --p; |
|
|
1555 | goto interrupt; |
|
|
1556 | |
|
|
1557 | case 0x09: |
|
|
1558 | case 0x0a: |
|
|
1559 | case 0x0d: |
|
|
1560 | case 0x20: |
|
|
1561 | if (!self->incr_nest) |
|
|
1562 | { |
|
|
1563 | --p; // do not eat the whitespace, let the next round do it |
|
|
1564 | goto interrupt; |
|
|
1565 | } |
|
|
1566 | break; |
|
|
1567 | |
|
|
1568 | case '"': |
|
|
1569 | self->incr_mode = INCR_M_STR; |
|
|
1570 | goto incr_m_str; |
|
|
1571 | |
|
|
1572 | case '[': |
|
|
1573 | case '{': |
|
|
1574 | ++self->incr_nest; |
|
|
1575 | break; |
|
|
1576 | |
|
|
1577 | case ']': |
|
|
1578 | case '}': |
|
|
1579 | if (!--self->incr_nest) |
|
|
1580 | goto interrupt; |
|
|
1581 | } |
|
|
1582 | } |
|
|
1583 | } |
|
|
1584 | |
|
|
1585 | modechange: |
|
|
1586 | ; |
|
|
1587 | } |
|
|
1588 | |
|
|
1589 | interrupt: |
|
|
1590 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1591 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
|
|
1592 | } |
|
|
1593 | |
|
|
1594 | ///////////////////////////////////////////////////////////////////////////// |
1321 | // XS interface functions |
1595 | // XS interface functions |
1322 | |
1596 | |
1323 | MODULE = JSON::XS PACKAGE = JSON::XS |
1597 | MODULE = JSON::XS PACKAGE = JSON::XS |
1324 | |
1598 | |
1325 | BOOT: |
1599 | BOOT: |
… | |
… | |
1340 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1614 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1341 | } |
1615 | } |
1342 | |
1616 | |
1343 | PROTOTYPES: DISABLE |
1617 | PROTOTYPES: DISABLE |
1344 | |
1618 | |
|
|
1619 | void CLONE (...) |
|
|
1620 | CODE: |
|
|
1621 | json_stash = 0; |
|
|
1622 | json_boolean_stash = 0; |
|
|
1623 | |
1345 | void new (char *klass) |
1624 | void new (char *klass) |
1346 | PPCODE: |
1625 | PPCODE: |
1347 | { |
1626 | { |
1348 | SV *pv = NEWSV (0, sizeof (JSON)); |
1627 | SV *pv = NEWSV (0, sizeof (JSON)); |
1349 | SvPOK_only (pv); |
1628 | SvPOK_only (pv); |
1350 | Zero (SvPVX (pv), 1, JSON); |
1629 | Zero (SvPVX (pv), 1, JSON); |
1351 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1630 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1352 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
1631 | XPUSHs (sv_2mortal (sv_bless ( |
|
|
1632 | newRV_noinc (pv), |
|
|
1633 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
|
|
1634 | ))); |
1353 | } |
1635 | } |
1354 | |
1636 | |
1355 | void ascii (JSON *self, int enable = 1) |
1637 | void ascii (JSON *self, int enable = 1) |
1356 | ALIAS: |
1638 | ALIAS: |
1357 | ascii = F_ASCII |
1639 | ascii = F_ASCII |
… | |
… | |
1364 | pretty = F_PRETTY |
1646 | pretty = F_PRETTY |
1365 | allow_nonref = F_ALLOW_NONREF |
1647 | allow_nonref = F_ALLOW_NONREF |
1366 | shrink = F_SHRINK |
1648 | shrink = F_SHRINK |
1367 | allow_blessed = F_ALLOW_BLESSED |
1649 | allow_blessed = F_ALLOW_BLESSED |
1368 | convert_blessed = F_CONV_BLESSED |
1650 | convert_blessed = F_CONV_BLESSED |
|
|
1651 | relaxed = F_RELAXED |
1369 | PPCODE: |
1652 | PPCODE: |
1370 | { |
1653 | { |
1371 | if (enable) |
1654 | if (enable) |
1372 | self->flags |= ix; |
1655 | self->flags |= ix; |
1373 | else |
1656 | else |
1374 | self->flags &= ~ix; |
1657 | self->flags &= ~ix; |
1375 | |
1658 | |
1376 | XPUSHs (ST (0)); |
1659 | XPUSHs (ST (0)); |
1377 | } |
1660 | } |
1378 | |
1661 | |
|
|
1662 | void get_ascii (JSON *self) |
|
|
1663 | ALIAS: |
|
|
1664 | get_ascii = F_ASCII |
|
|
1665 | get_latin1 = F_LATIN1 |
|
|
1666 | get_utf8 = F_UTF8 |
|
|
1667 | get_indent = F_INDENT |
|
|
1668 | get_canonical = F_CANONICAL |
|
|
1669 | get_space_before = F_SPACE_BEFORE |
|
|
1670 | get_space_after = F_SPACE_AFTER |
|
|
1671 | get_allow_nonref = F_ALLOW_NONREF |
|
|
1672 | get_shrink = F_SHRINK |
|
|
1673 | get_allow_blessed = F_ALLOW_BLESSED |
|
|
1674 | get_convert_blessed = F_CONV_BLESSED |
|
|
1675 | get_relaxed = F_RELAXED |
|
|
1676 | PPCODE: |
|
|
1677 | XPUSHs (boolSV (self->flags & ix)); |
|
|
1678 | |
1379 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1679 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1380 | PPCODE: |
1680 | PPCODE: |
1381 | { |
1681 | { |
1382 | UV log2 = 0; |
1682 | UV log2 = 0; |
1383 | |
1683 | |
… | |
… | |
1389 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1689 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1390 | |
1690 | |
1391 | XPUSHs (ST (0)); |
1691 | XPUSHs (ST (0)); |
1392 | } |
1692 | } |
1393 | |
1693 | |
|
|
1694 | U32 get_max_depth (JSON *self) |
|
|
1695 | CODE: |
|
|
1696 | RETVAL = DEC_DEPTH (self->flags); |
|
|
1697 | OUTPUT: |
|
|
1698 | RETVAL |
|
|
1699 | |
1394 | void max_size (JSON *self, UV max_size = 0) |
1700 | void max_size (JSON *self, UV max_size = 0) |
1395 | PPCODE: |
1701 | PPCODE: |
1396 | { |
1702 | { |
1397 | UV log2 = 0; |
1703 | UV log2 = 0; |
1398 | |
1704 | |
… | |
… | |
1404 | |
1710 | |
1405 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1711 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1406 | |
1712 | |
1407 | XPUSHs (ST (0)); |
1713 | XPUSHs (ST (0)); |
1408 | } |
1714 | } |
|
|
1715 | |
|
|
1716 | int get_max_size (JSON *self) |
|
|
1717 | CODE: |
|
|
1718 | RETVAL = DEC_SIZE (self->flags); |
|
|
1719 | OUTPUT: |
|
|
1720 | RETVAL |
1409 | |
1721 | |
1410 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1722 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1411 | PPCODE: |
1723 | PPCODE: |
1412 | { |
1724 | { |
1413 | SvREFCNT_dec (self->cb_object); |
1725 | SvREFCNT_dec (self->cb_object); |
… | |
… | |
1447 | XPUSHs (decode_json (jsonstr, self, 0)); |
1759 | XPUSHs (decode_json (jsonstr, self, 0)); |
1448 | |
1760 | |
1449 | void decode_prefix (JSON *self, SV *jsonstr) |
1761 | void decode_prefix (JSON *self, SV *jsonstr) |
1450 | PPCODE: |
1762 | PPCODE: |
1451 | { |
1763 | { |
1452 | UV offset; |
1764 | STRLEN offset; |
1453 | EXTEND (SP, 2); |
1765 | EXTEND (SP, 2); |
1454 | PUSHs (decode_json (jsonstr, self, &offset)); |
1766 | PUSHs (decode_json (jsonstr, self, &offset)); |
1455 | PUSHs (sv_2mortal (newSVuv (offset))); |
1767 | PUSHs (sv_2mortal (newSVuv (offset))); |
1456 | } |
1768 | } |
|
|
1769 | |
|
|
1770 | void incr_parse (JSON *self, SV *jsonstr = 0) |
|
|
1771 | PPCODE: |
|
|
1772 | { |
|
|
1773 | if (!self->incr_text) |
|
|
1774 | self->incr_text = newSVpvn ("", 0); |
|
|
1775 | |
|
|
1776 | // append data, if any |
|
|
1777 | if (jsonstr) |
|
|
1778 | { |
|
|
1779 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
|
|
1780 | { |
|
|
1781 | /* utf-8-ness differs, need to upgrade */ |
|
|
1782 | sv_utf8_upgrade (self->incr_text); |
|
|
1783 | |
|
|
1784 | if (self->incr_pos) |
|
|
1785 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1786 | - (U8 *)SvPVX (self->incr_text); |
|
|
1787 | } |
|
|
1788 | |
|
|
1789 | { |
|
|
1790 | STRLEN len; |
|
|
1791 | const char *str = SvPV (jsonstr, len); |
|
|
1792 | SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); |
|
|
1793 | Move (str, SvEND (self->incr_text), len, char); |
|
|
1794 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
|
|
1795 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1796 | } |
|
|
1797 | } |
|
|
1798 | |
|
|
1799 | if (GIMME_V != G_VOID) |
|
|
1800 | do |
|
|
1801 | { |
|
|
1802 | STRLEN offset; |
|
|
1803 | |
|
|
1804 | incr_parse (self); |
|
|
1805 | |
|
|
1806 | if (!INCR_DONE (self)) |
|
|
1807 | break; |
|
|
1808 | |
|
|
1809 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
|
|
1810 | |
|
|
1811 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1812 | self->incr_pos -= offset; |
|
|
1813 | self->incr_nest = 0; |
|
|
1814 | self->incr_mode = 0; |
|
|
1815 | } |
|
|
1816 | while (GIMME_V == G_ARRAY); |
|
|
1817 | } |
|
|
1818 | |
|
|
1819 | SV *incr_text (JSON *self) |
|
|
1820 | ATTRS: lvalue |
|
|
1821 | CODE: |
|
|
1822 | { |
|
|
1823 | if (self->incr_pos) |
|
|
1824 | croak ("incr_text can only be called after a successful incr_parse call in scalar context %d", self->incr_pos);//D |
|
|
1825 | |
|
|
1826 | RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef; |
|
|
1827 | } |
|
|
1828 | OUTPUT: |
|
|
1829 | RETVAL |
1457 | |
1830 | |
1458 | void DESTROY (JSON *self) |
1831 | void DESTROY (JSON *self) |
1459 | CODE: |
1832 | CODE: |
1460 | SvREFCNT_dec (self->cb_sk_object); |
1833 | SvREFCNT_dec (self->cb_sk_object); |
1461 | SvREFCNT_dec (self->cb_object); |
1834 | SvREFCNT_dec (self->cb_object); |
|
|
1835 | SvREFCNT_dec (self->incr_text); |
1462 | |
1836 | |
1463 | PROTOTYPES: ENABLE |
1837 | PROTOTYPES: ENABLE |
1464 | |
1838 | |
1465 | void to_json (SV *scalar) |
1839 | void encode_json (SV *scalar) |
|
|
1840 | ALIAS: |
|
|
1841 | to_json_ = 0 |
|
|
1842 | encode_json = F_UTF8 |
1466 | PPCODE: |
1843 | PPCODE: |
1467 | { |
1844 | { |
1468 | JSON json = { F_DEFAULT | F_UTF8 }; |
1845 | JSON json = { F_DEFAULT | ix }; |
1469 | XPUSHs (encode_json (scalar, &json)); |
1846 | XPUSHs (encode_json (scalar, &json)); |
1470 | } |
1847 | } |
1471 | |
1848 | |
1472 | void from_json (SV *jsonstr) |
1849 | void decode_json (SV *jsonstr) |
|
|
1850 | ALIAS: |
|
|
1851 | from_json_ = 0 |
|
|
1852 | decode_json = F_UTF8 |
1473 | PPCODE: |
1853 | PPCODE: |
1474 | { |
1854 | { |
1475 | JSON json = { F_DEFAULT | F_UTF8 }; |
1855 | JSON json = { F_DEFAULT | ix }; |
1476 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1856 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1477 | } |
1857 | } |
1478 | |
1858 | |
|
|
1859 | |