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 <float.h> |
9 | |
10 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
13 | #endif |
13 | |
14 | |
… | |
… | |
25 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_SHRINK 0x00000200UL |
29 | #define F_SHRINK 0x00000200UL |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
31 | #define F_CONV_BLESSED 0x00000800UL |
|
|
32 | #define F_RELAXED 0x00001000UL |
|
|
33 | |
31 | #define F_MAXDEPTH 0xf8000000UL |
34 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define S_MAXDEPTH 27 |
35 | #define S_MAXDEPTH 27 |
33 | #define F_MAXSIZE 0x01f00000UL |
36 | #define F_MAXSIZE 0x01f00000UL |
34 | #define S_MAXSIZE 20 |
37 | #define S_MAXSIZE 20 |
|
|
38 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
35 | |
39 | |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
40 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
41 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
38 | |
42 | |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
43 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
… | |
… | |
46 | |
50 | |
47 | #define SB do { |
51 | #define SB do { |
48 | #define SE } while (0) |
52 | #define SE } while (0) |
49 | |
53 | |
50 | #if __GNUC__ >= 3 |
54 | #if __GNUC__ >= 3 |
51 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
55 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
52 | # define inline inline |
56 | # define INLINE static inline |
53 | #else |
57 | #else |
54 | # define expect(expr,value) (expr) |
58 | # define expect(expr,value) (expr) |
55 | # define inline static |
59 | # define INLINE static |
56 | #endif |
60 | #endif |
57 | |
61 | |
58 | #define expect_false(expr) expect ((expr) != 0, 0) |
62 | #define expect_false(expr) expect ((expr) != 0, 0) |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
63 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | |
64 | |
|
|
65 | #define IN_RANGE_INC(type,val,beg,end) \ |
|
|
66 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
|
|
67 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
|
|
68 | |
|
|
69 | #ifdef USE_ITHREADS |
|
|
70 | # define JSON_SLOW 1 |
|
|
71 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
72 | #else |
|
|
73 | # define JSON_SLOW 0 |
|
|
74 | # define JSON_STASH json_stash |
|
|
75 | #endif |
|
|
76 | |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
77 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
62 | static SV *json_true, *json_false; |
78 | static SV *json_true, *json_false; |
63 | |
79 | |
64 | typedef struct { |
80 | typedef struct { |
65 | U32 flags; |
81 | U32 flags; |
|
|
82 | SV *cb_object; |
|
|
83 | HV *cb_sk_object; |
66 | } JSON; |
84 | } JSON; |
67 | |
85 | |
68 | ///////////////////////////////////////////////////////////////////////////// |
86 | ///////////////////////////////////////////////////////////////////////////// |
69 | // utility functions |
87 | // utility functions |
70 | |
88 | |
71 | inline void |
89 | INLINE void |
72 | shrink (SV *sv) |
90 | shrink (SV *sv) |
73 | { |
91 | { |
74 | sv_utf8_downgrade (sv, 1); |
92 | sv_utf8_downgrade (sv, 1); |
75 | if (SvLEN (sv) > SvCUR (sv) + 1) |
93 | if (SvLEN (sv) > SvCUR (sv) + 1) |
76 | { |
94 | { |
… | |
… | |
85 | // decode an utf-8 character and return it, or (UV)-1 in |
103 | // decode an utf-8 character and return it, or (UV)-1 in |
86 | // case of an error. |
104 | // case of an error. |
87 | // we special-case "safe" characters from U+80 .. U+7FF, |
105 | // we special-case "safe" characters from U+80 .. U+7FF, |
88 | // but use the very good perl function to parse anything else. |
106 | // but use the very good perl function to parse anything else. |
89 | // note that we never call this function for a ascii codepoints |
107 | // note that we never call this function for a ascii codepoints |
90 | inline UV |
108 | INLINE UV |
91 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
109 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
92 | { |
110 | { |
93 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
111 | if (expect_true (len >= 2 |
94 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
112 | && IN_RANGE_INC (char, s[0], 0xc2, 0xdf) |
95 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
113 | && IN_RANGE_INC (char, s[1], 0x80, 0xbf))) |
96 | { |
114 | { |
97 | *clen = 2; |
115 | *clen = 2; |
98 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
116 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
99 | } |
117 | } |
100 | else |
118 | else |
101 | { |
119 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
102 | *clen = (STRLEN)-1; |
120 | } |
103 | return (UV)-1; |
121 | |
|
|
122 | // likewise for encoding, also never called for ascii codepoints |
|
|
123 | // this function takes advantage of this fact, although current gccs |
|
|
124 | // seem to optimise the check for >= 0x80 away anyways |
|
|
125 | INLINE unsigned char * |
|
|
126 | encode_utf8 (unsigned char *s, UV ch) |
|
|
127 | { |
|
|
128 | if (ch <= 0x7FF) |
104 | } |
129 | { |
|
|
130 | *s++ = (ch >> 6) | 0xc0; |
|
|
131 | *s++ = (ch & 0x3f) | 0x80; |
|
|
132 | } |
|
|
133 | else |
|
|
134 | s = uvuni_to_utf8_flags (s, ch, 0); |
|
|
135 | |
|
|
136 | return s; |
105 | } |
137 | } |
106 | |
138 | |
107 | ///////////////////////////////////////////////////////////////////////////// |
139 | ///////////////////////////////////////////////////////////////////////////// |
108 | // encoder |
140 | // encoder |
109 | |
141 | |
… | |
… | |
114 | char *end; // SvEND (sv) |
146 | char *end; // SvEND (sv) |
115 | SV *sv; // result scalar |
147 | SV *sv; // result scalar |
116 | JSON json; |
148 | JSON json; |
117 | U32 indent; // indentation level |
149 | U32 indent; // indentation level |
118 | U32 maxdepth; // max. indentation/recursion level |
150 | U32 maxdepth; // max. indentation/recursion level |
|
|
151 | UV limit; // escape character values >= this value when encoding |
119 | } enc_t; |
152 | } enc_t; |
120 | |
153 | |
121 | inline void |
154 | INLINE void |
122 | need (enc_t *enc, STRLEN len) |
155 | need (enc_t *enc, STRLEN len) |
123 | { |
156 | { |
124 | if (expect_false (enc->cur + len >= enc->end)) |
157 | if (expect_false (enc->cur + len >= enc->end)) |
125 | { |
158 | { |
126 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
159 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
… | |
… | |
128 | enc->cur = SvPVX (enc->sv) + cur; |
161 | enc->cur = SvPVX (enc->sv) + cur; |
129 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
162 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
130 | } |
163 | } |
131 | } |
164 | } |
132 | |
165 | |
133 | inline void |
166 | INLINE void |
134 | encode_ch (enc_t *enc, char ch) |
167 | encode_ch (enc_t *enc, char ch) |
135 | { |
168 | { |
136 | need (enc, 1); |
169 | need (enc, 1); |
137 | *enc->cur++ = ch; |
170 | *enc->cur++ = ch; |
138 | } |
171 | } |
… | |
… | |
192 | { |
225 | { |
193 | uch = ch; |
226 | uch = ch; |
194 | clen = 1; |
227 | clen = 1; |
195 | } |
228 | } |
196 | |
229 | |
197 | if (uch > 0x10FFFFUL) |
230 | if (uch < 0x80/*0x20*/ || uch >= enc->limit) |
198 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
199 | |
|
|
200 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
|
|
201 | { |
231 | { |
202 | if (uch > 0xFFFFUL) |
232 | if (uch > 0xFFFFUL) |
203 | { |
233 | { |
|
|
234 | if (uch > 0x10FFFFUL) |
|
|
235 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
236 | |
204 | need (enc, len += 11); |
237 | need (enc, len += 11); |
205 | sprintf (enc->cur, "\\u%04x\\u%04x", |
238 | sprintf (enc->cur, "\\u%04x\\u%04x", |
206 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
239 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
207 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
240 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
208 | enc->cur += 12; |
241 | enc->cur += 12; |
… | |
… | |
236 | while (--clen); |
269 | while (--clen); |
237 | } |
270 | } |
238 | else |
271 | else |
239 | { |
272 | { |
240 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
273 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
241 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
274 | enc->cur = encode_utf8 (enc->cur, uch); |
242 | ++str; |
275 | ++str; |
243 | } |
276 | } |
244 | } |
277 | } |
245 | } |
278 | } |
246 | } |
279 | } |
247 | |
280 | |
248 | --len; |
281 | --len; |
249 | } |
282 | } |
250 | } |
283 | } |
251 | |
284 | |
252 | inline void |
285 | INLINE void |
253 | encode_indent (enc_t *enc) |
286 | encode_indent (enc_t *enc) |
254 | { |
287 | { |
255 | if (enc->json.flags & F_INDENT) |
288 | if (enc->json.flags & F_INDENT) |
256 | { |
289 | { |
257 | int spaces = enc->indent * INDENT_STEP; |
290 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
260 | memset (enc->cur, ' ', spaces); |
293 | memset (enc->cur, ' ', spaces); |
261 | enc->cur += spaces; |
294 | enc->cur += spaces; |
262 | } |
295 | } |
263 | } |
296 | } |
264 | |
297 | |
265 | inline void |
298 | INLINE void |
266 | encode_space (enc_t *enc) |
299 | encode_space (enc_t *enc) |
267 | { |
300 | { |
268 | need (enc, 1); |
301 | need (enc, 1); |
269 | encode_ch (enc, ' '); |
302 | encode_ch (enc, ' '); |
270 | } |
303 | } |
271 | |
304 | |
272 | inline void |
305 | INLINE void |
273 | encode_nl (enc_t *enc) |
306 | encode_nl (enc_t *enc) |
274 | { |
307 | { |
275 | if (enc->json.flags & F_INDENT) |
308 | if (enc->json.flags & F_INDENT) |
276 | { |
309 | { |
277 | need (enc, 1); |
310 | need (enc, 1); |
278 | encode_ch (enc, '\n'); |
311 | encode_ch (enc, '\n'); |
279 | } |
312 | } |
280 | } |
313 | } |
281 | |
314 | |
282 | inline void |
315 | INLINE void |
283 | encode_comma (enc_t *enc) |
316 | encode_comma (enc_t *enc) |
284 | { |
317 | { |
285 | encode_ch (enc, ','); |
318 | encode_ch (enc, ','); |
286 | |
319 | |
287 | if (enc->json.flags & F_INDENT) |
320 | if (enc->json.flags & F_INDENT) |
… | |
… | |
298 | int i, len = av_len (av); |
331 | int i, len = av_len (av); |
299 | |
332 | |
300 | if (enc->indent >= enc->maxdepth) |
333 | if (enc->indent >= enc->maxdepth) |
301 | croak ("data structure too deep (hit recursion limit)"); |
334 | croak ("data structure too deep (hit recursion limit)"); |
302 | |
335 | |
303 | encode_ch (enc, '['); encode_nl (enc); |
336 | encode_ch (enc, '['); |
304 | ++enc->indent; |
337 | |
|
|
338 | if (len >= 0) |
|
|
339 | { |
|
|
340 | encode_nl (enc); ++enc->indent; |
305 | |
341 | |
306 | for (i = 0; i <= len; ++i) |
342 | for (i = 0; i <= len; ++i) |
307 | { |
343 | { |
|
|
344 | SV **svp = av_fetch (av, i, 0); |
|
|
345 | |
308 | encode_indent (enc); |
346 | encode_indent (enc); |
309 | encode_sv (enc, *av_fetch (av, i, 0)); |
|
|
310 | |
347 | |
|
|
348 | if (svp) |
|
|
349 | encode_sv (enc, *svp); |
|
|
350 | else |
|
|
351 | encode_str (enc, "null", 4, 0); |
|
|
352 | |
311 | if (i < len) |
353 | if (i < len) |
312 | encode_comma (enc); |
354 | encode_comma (enc); |
313 | } |
355 | } |
314 | |
356 | |
|
|
357 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
358 | } |
|
|
359 | |
315 | encode_nl (enc); |
360 | encode_ch (enc, ']'); |
316 | |
|
|
317 | --enc->indent; |
|
|
318 | encode_indent (enc); encode_ch (enc, ']'); |
|
|
319 | } |
361 | } |
320 | |
362 | |
321 | static void |
363 | static void |
322 | encode_he (enc_t *enc, HE *he) |
364 | encode_hk (enc_t *enc, HE *he) |
323 | { |
365 | { |
324 | encode_ch (enc, '"'); |
366 | encode_ch (enc, '"'); |
325 | |
367 | |
326 | if (HeKLEN (he) == HEf_SVKEY) |
368 | if (HeKLEN (he) == HEf_SVKEY) |
327 | { |
369 | { |
… | |
… | |
340 | encode_ch (enc, '"'); |
382 | encode_ch (enc, '"'); |
341 | |
383 | |
342 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
384 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
343 | encode_ch (enc, ':'); |
385 | encode_ch (enc, ':'); |
344 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
386 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
345 | encode_sv (enc, HeVAL (he)); |
|
|
346 | } |
387 | } |
347 | |
388 | |
348 | // compare hash entries, used when all keys are bytestrings |
389 | // compare hash entries, used when all keys are bytestrings |
349 | static int |
390 | static int |
350 | he_cmp_fast (const void *a_, const void *b_) |
391 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
355 | HE *b = *(HE **)b_; |
396 | HE *b = *(HE **)b_; |
356 | |
397 | |
357 | STRLEN la = HeKLEN (a); |
398 | STRLEN la = HeKLEN (a); |
358 | STRLEN lb = HeKLEN (b); |
399 | STRLEN lb = HeKLEN (b); |
359 | |
400 | |
360 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
401 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
361 | cmp = la - lb; |
402 | cmp = lb - la; |
362 | |
403 | |
363 | return cmp; |
404 | return cmp; |
364 | } |
405 | } |
365 | |
406 | |
366 | // compare hash entries, used when some keys are sv's or utf-x |
407 | // compare hash entries, used when some keys are sv's or utf-x |
367 | static int |
408 | static int |
368 | he_cmp_slow (const void *a, const void *b) |
409 | he_cmp_slow (const void *a, const void *b) |
369 | { |
410 | { |
370 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
411 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
371 | } |
412 | } |
372 | |
413 | |
373 | static void |
414 | static void |
374 | encode_hv (enc_t *enc, HV *hv) |
415 | encode_hv (enc_t *enc, HV *hv) |
375 | { |
416 | { |
|
|
417 | HE *he; |
376 | int count, i; |
418 | int count; |
377 | |
419 | |
378 | if (enc->indent >= enc->maxdepth) |
420 | if (enc->indent >= enc->maxdepth) |
379 | croak ("data structure too deep (hit recursion limit)"); |
421 | croak ("data structure too deep (hit recursion limit)"); |
380 | |
422 | |
381 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
423 | encode_ch (enc, '{'); |
382 | |
424 | |
383 | if ((count = hv_iterinit (hv))) |
|
|
384 | { |
|
|
385 | // for canonical output we have to sort by keys first |
425 | // for canonical output we have to sort by keys first |
386 | // actually, this is mostly due to the stupid so-called |
426 | // actually, this is mostly due to the stupid so-called |
387 | // security workaround added somewhere in 5.8.x. |
427 | // security workaround added somewhere in 5.8.x. |
388 | // that randomises hash orderings |
428 | // that randomises hash orderings |
389 | if (enc->json.flags & F_CANONICAL) |
429 | if (enc->json.flags & F_CANONICAL) |
|
|
430 | { |
|
|
431 | int count = hv_iterinit (hv); |
|
|
432 | |
|
|
433 | if (SvMAGICAL (hv)) |
390 | { |
434 | { |
|
|
435 | // need to count by iterating. could improve by dynamically building the vector below |
|
|
436 | // but I don't care for the speed of this special case. |
|
|
437 | // note also that we will run into undefined behaviour when the two iterations |
|
|
438 | // do not result in the same count, something I might care for in some later release. |
|
|
439 | |
|
|
440 | count = 0; |
|
|
441 | while (hv_iternext (hv)) |
|
|
442 | ++count; |
|
|
443 | |
|
|
444 | hv_iterinit (hv); |
|
|
445 | } |
|
|
446 | |
|
|
447 | if (count) |
|
|
448 | { |
391 | int fast = 1; |
449 | int i, fast = 1; |
392 | HE *he; |
|
|
393 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
450 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
394 | HE **hes = _alloca (count * sizeof (HE)); |
451 | HE **hes = _alloca (count * sizeof (HE)); |
395 | #else |
452 | #else |
396 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
453 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
397 | #endif |
454 | #endif |
… | |
… | |
424 | |
481 | |
425 | FREETMPS; |
482 | FREETMPS; |
426 | LEAVE; |
483 | LEAVE; |
427 | } |
484 | } |
428 | |
485 | |
429 | for (i = 0; i < count; ++i) |
486 | encode_nl (enc); ++enc->indent; |
|
|
487 | |
|
|
488 | while (count--) |
430 | { |
489 | { |
431 | encode_indent (enc); |
490 | encode_indent (enc); |
|
|
491 | he = hes [count]; |
432 | encode_he (enc, hes [i]); |
492 | encode_hk (enc, he); |
|
|
493 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
433 | |
494 | |
434 | if (i < count - 1) |
495 | if (count) |
435 | encode_comma (enc); |
496 | encode_comma (enc); |
436 | } |
497 | } |
437 | |
498 | |
438 | encode_nl (enc); |
499 | encode_nl (enc); --enc->indent; encode_indent (enc); |
439 | } |
500 | } |
|
|
501 | } |
440 | else |
502 | else |
|
|
503 | { |
|
|
504 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
|
|
505 | if ((he = hv_iternext (hv))) |
441 | { |
506 | { |
442 | HE *he = hv_iternext (hv); |
507 | encode_nl (enc); ++enc->indent; |
443 | |
508 | |
444 | for (;;) |
509 | for (;;) |
445 | { |
510 | { |
446 | encode_indent (enc); |
511 | encode_indent (enc); |
447 | encode_he (enc, he); |
512 | encode_hk (enc, he); |
|
|
513 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
448 | |
514 | |
449 | if (!(he = hv_iternext (hv))) |
515 | if (!(he = hv_iternext (hv))) |
450 | break; |
516 | break; |
451 | |
517 | |
452 | encode_comma (enc); |
518 | encode_comma (enc); |
453 | } |
519 | } |
454 | |
520 | |
455 | encode_nl (enc); |
521 | encode_nl (enc); --enc->indent; encode_indent (enc); |
456 | } |
522 | } |
457 | } |
523 | } |
458 | |
524 | |
459 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
525 | encode_ch (enc, '}'); |
460 | } |
526 | } |
461 | |
527 | |
462 | // encode objects, arrays and special \0=false and \1=true values. |
528 | // encode objects, arrays and special \0=false and \1=true values. |
463 | static void |
529 | static void |
464 | encode_rv (enc_t *enc, SV *sv) |
530 | encode_rv (enc_t *enc, SV *sv) |
… | |
… | |
468 | SvGETMAGIC (sv); |
534 | SvGETMAGIC (sv); |
469 | svt = SvTYPE (sv); |
535 | svt = SvTYPE (sv); |
470 | |
536 | |
471 | if (expect_false (SvOBJECT (sv))) |
537 | if (expect_false (SvOBJECT (sv))) |
472 | { |
538 | { |
|
|
539 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
540 | ? json_boolean_stash |
|
|
541 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
542 | |
473 | if (SvSTASH (sv) == json_boolean_stash) |
543 | if (SvSTASH (sv) == stash) |
474 | { |
544 | { |
475 | if (SvIV (sv) == 0) |
545 | if (SvIV (sv)) |
|
|
546 | encode_str (enc, "true", 4, 0); |
|
|
547 | else |
476 | encode_str (enc, "false", 5, 0); |
548 | encode_str (enc, "false", 5, 0); |
477 | else |
|
|
478 | encode_str (enc, "true", 4, 0); |
|
|
479 | } |
549 | } |
480 | else |
550 | else |
481 | { |
551 | { |
482 | #if 0 |
552 | #if 0 |
483 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
553 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
… | |
… | |
486 | } |
556 | } |
487 | #endif |
557 | #endif |
488 | if (enc->json.flags & F_CONV_BLESSED) |
558 | if (enc->json.flags & F_CONV_BLESSED) |
489 | { |
559 | { |
490 | // we re-bless the reference to get overload and other niceties right |
560 | // we re-bless the reference to get overload and other niceties right |
491 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
561 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
492 | |
562 | |
493 | if (to_json) |
563 | if (to_json) |
494 | { |
564 | { |
495 | dSP; |
565 | dSP; |
496 | ENTER; |
566 | |
497 | SAVETMPS; |
|
|
498 | PUSHMARK (SP); |
567 | ENTER; SAVETMPS; PUSHMARK (SP); |
499 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
568 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
500 | |
569 | |
501 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
570 | // calling with G_SCALAR ensures that we always get a 1 return value |
502 | // check anyways. |
|
|
503 | PUTBACK; |
571 | PUTBACK; |
504 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
572 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
505 | SPAGAIN; |
573 | SPAGAIN; |
506 | |
574 | |
|
|
575 | // catch this surprisingly common error |
|
|
576 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
577 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
578 | |
|
|
579 | sv = POPs; |
|
|
580 | PUTBACK; |
|
|
581 | |
507 | encode_sv (enc, POPs); |
582 | encode_sv (enc, sv); |
508 | |
583 | |
509 | FREETMPS; |
584 | FREETMPS; LEAVE; |
510 | LEAVE; |
|
|
511 | } |
585 | } |
512 | else if (enc->json.flags & F_ALLOW_BLESSED) |
586 | else if (enc->json.flags & F_ALLOW_BLESSED) |
513 | encode_str (enc, "null", 4, 0); |
587 | encode_str (enc, "null", 4, 0); |
514 | else |
588 | else |
515 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
589 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
… | |
… | |
526 | encode_hv (enc, (HV *)sv); |
600 | encode_hv (enc, (HV *)sv); |
527 | else if (svt == SVt_PVAV) |
601 | else if (svt == SVt_PVAV) |
528 | encode_av (enc, (AV *)sv); |
602 | encode_av (enc, (AV *)sv); |
529 | else if (svt < SVt_PVAV) |
603 | else if (svt < SVt_PVAV) |
530 | { |
604 | { |
531 | if (SvNIOK (sv) && SvIV (sv) == 0) |
605 | STRLEN len = 0; |
|
|
606 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
607 | |
|
|
608 | if (len == 1 && *pv == '1') |
|
|
609 | encode_str (enc, "true", 4, 0); |
|
|
610 | else if (len == 1 && *pv == '0') |
532 | encode_str (enc, "false", 5, 0); |
611 | encode_str (enc, "false", 5, 0); |
533 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
534 | encode_str (enc, "true", 4, 0); |
|
|
535 | else |
612 | else |
536 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
613 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
537 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
614 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
538 | } |
615 | } |
539 | else |
616 | else |
… | |
… | |
622 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
699 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
623 | enc.cur = SvPVX (enc.sv); |
700 | enc.cur = SvPVX (enc.sv); |
624 | enc.end = SvEND (enc.sv); |
701 | enc.end = SvEND (enc.sv); |
625 | enc.indent = 0; |
702 | enc.indent = 0; |
626 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
703 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
|
|
704 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
|
|
705 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
|
|
706 | : 0x10FFFFUL; |
627 | |
707 | |
628 | SvPOK_only (enc.sv); |
708 | SvPOK_only (enc.sv); |
629 | encode_sv (&enc, scalar); |
709 | encode_sv (&enc, scalar); |
630 | |
710 | |
631 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
711 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
… | |
… | |
652 | JSON json; |
732 | JSON json; |
653 | U32 depth; // recursion depth |
733 | U32 depth; // recursion depth |
654 | U32 maxdepth; // recursion depth limit |
734 | U32 maxdepth; // recursion depth limit |
655 | } dec_t; |
735 | } dec_t; |
656 | |
736 | |
657 | inline void |
737 | INLINE void |
|
|
738 | decode_comment (dec_t *dec) |
|
|
739 | { |
|
|
740 | // only '#'-style comments allowed a.t.m. |
|
|
741 | |
|
|
742 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
|
|
743 | ++dec->cur; |
|
|
744 | } |
|
|
745 | |
|
|
746 | INLINE void |
658 | decode_ws (dec_t *dec) |
747 | decode_ws (dec_t *dec) |
659 | { |
748 | { |
660 | for (;;) |
749 | for (;;) |
661 | { |
750 | { |
662 | char ch = *dec->cur; |
751 | char ch = *dec->cur; |
663 | |
752 | |
664 | if (ch > 0x20 |
753 | if (ch > 0x20) |
|
|
754 | { |
|
|
755 | if (expect_false (ch == '#')) |
|
|
756 | { |
|
|
757 | if (dec->json.flags & F_RELAXED) |
|
|
758 | decode_comment (dec); |
|
|
759 | else |
|
|
760 | break; |
|
|
761 | } |
|
|
762 | else |
|
|
763 | break; |
|
|
764 | } |
665 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
765 | else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
666 | break; |
766 | break; // parse error, but let higher level handle it, gives better error messages |
667 | |
767 | |
668 | ++dec->cur; |
768 | ++dec->cur; |
669 | } |
769 | } |
670 | } |
770 | } |
671 | |
771 | |
… | |
… | |
777 | |
877 | |
778 | if (hi >= 0x80) |
878 | if (hi >= 0x80) |
779 | { |
879 | { |
780 | utf8 = 1; |
880 | utf8 = 1; |
781 | |
881 | |
782 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
882 | cur = encode_utf8 (cur, hi); |
783 | } |
883 | } |
784 | else |
884 | else |
785 | *cur++ = hi; |
885 | *cur++ = hi; |
786 | } |
886 | } |
787 | break; |
887 | break; |
… | |
… | |
920 | is_nv = 1; |
1020 | is_nv = 1; |
921 | } |
1021 | } |
922 | |
1022 | |
923 | if (!is_nv) |
1023 | if (!is_nv) |
924 | { |
1024 | { |
|
|
1025 | int len = dec->cur - start; |
|
|
1026 | |
925 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
1027 | // special case the rather common 1..5-digit-int case |
926 | if (*start == '-') |
1028 | if (*start == '-') |
927 | switch (dec->cur - start) |
1029 | switch (len) |
928 | { |
1030 | { |
929 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1031 | case 2: return newSViv (-( start [1] - '0' * 1)); |
930 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1032 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
931 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1033 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
932 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1034 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1035 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
933 | } |
1036 | } |
934 | else |
1037 | else |
935 | switch (dec->cur - start) |
1038 | switch (len) |
936 | { |
1039 | { |
937 | case 1: return newSViv ( start [0] - '0' * 1); |
1040 | case 1: return newSViv ( start [0] - '0' * 1); |
938 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1041 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
939 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1042 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
940 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1043 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1044 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
941 | } |
1045 | } |
942 | |
1046 | |
943 | { |
1047 | { |
944 | UV uv; |
1048 | UV uv; |
945 | int numtype = grok_number (start, dec->cur - start, &uv); |
1049 | int numtype = grok_number (start, len, &uv); |
946 | if (numtype & IS_NUMBER_IN_UV) |
1050 | if (numtype & IS_NUMBER_IN_UV) |
947 | if (numtype & IS_NUMBER_NEG) |
1051 | if (numtype & IS_NUMBER_NEG) |
948 | { |
1052 | { |
949 | if (uv < (UV)IV_MIN) |
1053 | if (uv < (UV)IV_MIN) |
950 | return newSViv (-(IV)uv); |
1054 | return newSViv (-(IV)uv); |
951 | } |
1055 | } |
952 | else |
1056 | else |
953 | return newSVuv (uv); |
1057 | return newSVuv (uv); |
954 | |
|
|
955 | // here would likely be the place for bigint support |
|
|
956 | } |
1058 | } |
957 | } |
|
|
958 | |
1059 | |
959 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
1060 | len -= *start == '-' ? 1 : 0; |
|
|
1061 | |
|
|
1062 | // does not fit into IV or UV, try NV |
|
|
1063 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
1064 | #if defined (LDBL_DIG) |
|
|
1065 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1066 | #endif |
|
|
1067 | ) |
|
|
1068 | // fits into NV without loss of precision |
|
|
1069 | return newSVnv (Atof (start)); |
|
|
1070 | |
|
|
1071 | // everything else fails, convert it to a string |
|
|
1072 | return newSVpvn (start, dec->cur - start); |
|
|
1073 | } |
|
|
1074 | |
|
|
1075 | // loss of precision here |
960 | return newSVnv (Atof (start)); |
1076 | return newSVnv (Atof (start)); |
961 | |
1077 | |
962 | fail: |
1078 | fail: |
963 | return 0; |
1079 | return 0; |
964 | } |
1080 | } |
… | |
… | |
994 | |
1110 | |
995 | if (*dec->cur != ',') |
1111 | if (*dec->cur != ',') |
996 | ERR (", or ] expected while parsing array"); |
1112 | ERR (", or ] expected while parsing array"); |
997 | |
1113 | |
998 | ++dec->cur; |
1114 | ++dec->cur; |
|
|
1115 | |
|
|
1116 | decode_ws (dec); |
|
|
1117 | |
|
|
1118 | if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
|
|
1119 | { |
|
|
1120 | ++dec->cur; |
|
|
1121 | break; |
|
|
1122 | } |
999 | } |
1123 | } |
1000 | |
1124 | |
1001 | DEC_DEC_DEPTH; |
1125 | DEC_DEC_DEPTH; |
1002 | return newRV_noinc ((SV *)av); |
1126 | return newRV_noinc ((SV *)av); |
1003 | |
1127 | |
… | |
… | |
1008 | } |
1132 | } |
1009 | |
1133 | |
1010 | static SV * |
1134 | static SV * |
1011 | decode_hv (dec_t *dec) |
1135 | decode_hv (dec_t *dec) |
1012 | { |
1136 | { |
|
|
1137 | SV *sv; |
1013 | HV *hv = newHV (); |
1138 | HV *hv = newHV (); |
1014 | |
1139 | |
1015 | DEC_INC_DEPTH; |
1140 | DEC_INC_DEPTH; |
1016 | decode_ws (dec); |
1141 | decode_ws (dec); |
1017 | |
1142 | |
1018 | if (*dec->cur == '}') |
1143 | if (*dec->cur == '}') |
1019 | ++dec->cur; |
1144 | ++dec->cur; |
1020 | else |
1145 | else |
1021 | for (;;) |
1146 | for (;;) |
1022 | { |
1147 | { |
1023 | decode_ws (dec); EXPECT_CH ('"'); |
1148 | EXPECT_CH ('"'); |
1024 | |
1149 | |
1025 | // heuristic: assume that |
1150 | // heuristic: assume that |
1026 | // a) decode_str + hv_store_ent are abysmally slow. |
1151 | // a) decode_str + hv_store_ent are abysmally slow. |
1027 | // b) most hash keys are short, simple ascii text. |
1152 | // b) most hash keys are short, simple ascii text. |
1028 | // => try to "fast-match" such strings to avoid |
1153 | // => try to "fast-match" such strings to avoid |
… | |
… | |
1042 | if (!key) |
1167 | if (!key) |
1043 | goto fail; |
1168 | goto fail; |
1044 | |
1169 | |
1045 | decode_ws (dec); EXPECT_CH (':'); |
1170 | decode_ws (dec); EXPECT_CH (':'); |
1046 | |
1171 | |
|
|
1172 | decode_ws (dec); |
1047 | value = decode_sv (dec); |
1173 | value = decode_sv (dec); |
1048 | if (!value) |
1174 | if (!value) |
1049 | { |
1175 | { |
1050 | SvREFCNT_dec (key); |
1176 | SvREFCNT_dec (key); |
1051 | goto fail; |
1177 | goto fail; |
… | |
… | |
1063 | int len = p - key; |
1189 | int len = p - key; |
1064 | dec->cur = p + 1; |
1190 | dec->cur = p + 1; |
1065 | |
1191 | |
1066 | decode_ws (dec); EXPECT_CH (':'); |
1192 | decode_ws (dec); EXPECT_CH (':'); |
1067 | |
1193 | |
|
|
1194 | decode_ws (dec); |
1068 | value = decode_sv (dec); |
1195 | value = decode_sv (dec); |
1069 | if (!value) |
1196 | if (!value) |
1070 | goto fail; |
1197 | goto fail; |
1071 | |
1198 | |
1072 | hv_store (hv, key, len, value, 0); |
1199 | hv_store (hv, key, len, value, 0); |
… | |
… | |
1088 | |
1215 | |
1089 | if (*dec->cur != ',') |
1216 | if (*dec->cur != ',') |
1090 | ERR (", or } expected while parsing object/hash"); |
1217 | ERR (", or } expected while parsing object/hash"); |
1091 | |
1218 | |
1092 | ++dec->cur; |
1219 | ++dec->cur; |
|
|
1220 | |
|
|
1221 | decode_ws (dec); |
|
|
1222 | |
|
|
1223 | if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
|
|
1224 | { |
|
|
1225 | ++dec->cur; |
|
|
1226 | break; |
|
|
1227 | } |
1093 | } |
1228 | } |
1094 | |
1229 | |
1095 | DEC_DEC_DEPTH; |
1230 | DEC_DEC_DEPTH; |
1096 | return newRV_noinc ((SV *)hv); |
1231 | sv = newRV_noinc ((SV *)hv); |
|
|
1232 | |
|
|
1233 | // check filter callbacks |
|
|
1234 | if (dec->json.flags & F_HOOK) |
|
|
1235 | { |
|
|
1236 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1237 | { |
|
|
1238 | HE *cb, *he; |
|
|
1239 | |
|
|
1240 | hv_iterinit (hv); |
|
|
1241 | he = hv_iternext (hv); |
|
|
1242 | hv_iterinit (hv); |
|
|
1243 | |
|
|
1244 | // the next line creates a mortal sv each time its called. |
|
|
1245 | // might want to optimise this for common cases. |
|
|
1246 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1247 | |
|
|
1248 | if (cb) |
|
|
1249 | { |
|
|
1250 | dSP; |
|
|
1251 | int count; |
|
|
1252 | |
|
|
1253 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1254 | XPUSHs (HeVAL (he)); |
|
|
1255 | |
|
|
1256 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1257 | |
|
|
1258 | if (count == 1) |
|
|
1259 | { |
|
|
1260 | sv = newSVsv (POPs); |
|
|
1261 | FREETMPS; LEAVE; |
|
|
1262 | return sv; |
|
|
1263 | } |
|
|
1264 | |
|
|
1265 | FREETMPS; LEAVE; |
|
|
1266 | } |
|
|
1267 | } |
|
|
1268 | |
|
|
1269 | if (dec->json.cb_object) |
|
|
1270 | { |
|
|
1271 | dSP; |
|
|
1272 | int count; |
|
|
1273 | |
|
|
1274 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1275 | XPUSHs (sv_2mortal (sv)); |
|
|
1276 | |
|
|
1277 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1278 | |
|
|
1279 | if (count == 1) |
|
|
1280 | { |
|
|
1281 | sv = newSVsv (POPs); |
|
|
1282 | FREETMPS; LEAVE; |
|
|
1283 | return sv; |
|
|
1284 | } |
|
|
1285 | |
|
|
1286 | SvREFCNT_inc (sv); |
|
|
1287 | FREETMPS; LEAVE; |
|
|
1288 | } |
|
|
1289 | } |
|
|
1290 | |
|
|
1291 | return sv; |
1097 | |
1292 | |
1098 | fail: |
1293 | fail: |
1099 | SvREFCNT_dec (hv); |
1294 | SvREFCNT_dec (hv); |
1100 | DEC_DEC_DEPTH; |
1295 | DEC_DEC_DEPTH; |
1101 | return 0; |
1296 | return 0; |
1102 | } |
1297 | } |
1103 | |
1298 | |
1104 | static SV * |
1299 | static SV * |
1105 | decode_sv (dec_t *dec) |
1300 | decode_sv (dec_t *dec) |
1106 | { |
1301 | { |
1107 | decode_ws (dec); |
|
|
1108 | |
|
|
1109 | // the beauty of JSON: you need exactly one character lookahead |
1302 | // the beauty of JSON: you need exactly one character lookahead |
1110 | // to parse anything. |
1303 | // to parse anything. |
1111 | switch (*dec->cur) |
1304 | switch (*dec->cur) |
1112 | { |
1305 | { |
1113 | case '"': ++dec->cur; return decode_str (dec); |
1306 | case '"': ++dec->cur; return decode_str (dec); |
… | |
… | |
1121 | |
1314 | |
1122 | case 't': |
1315 | case 't': |
1123 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1316 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1124 | { |
1317 | { |
1125 | dec->cur += 4; |
1318 | dec->cur += 4; |
|
|
1319 | #if JSON_SLOW |
|
|
1320 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
|
|
1321 | #endif |
1126 | return SvREFCNT_inc (json_true); |
1322 | return SvREFCNT_inc (json_true); |
1127 | } |
1323 | } |
1128 | else |
1324 | else |
1129 | ERR ("'true' expected"); |
1325 | ERR ("'true' expected"); |
1130 | |
1326 | |
… | |
… | |
1132 | |
1328 | |
1133 | case 'f': |
1329 | case 'f': |
1134 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1330 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1135 | { |
1331 | { |
1136 | dec->cur += 5; |
1332 | dec->cur += 5; |
|
|
1333 | #if JSON_SLOW |
|
|
1334 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
|
|
1335 | #endif |
1137 | return SvREFCNT_inc (json_false); |
1336 | return SvREFCNT_inc (json_false); |
1138 | } |
1337 | } |
1139 | else |
1338 | else |
1140 | ERR ("'false' expected"); |
1339 | ERR ("'false' expected"); |
1141 | |
1340 | |
… | |
… | |
1187 | dec.end = SvEND (string); |
1386 | dec.end = SvEND (string); |
1188 | dec.err = 0; |
1387 | dec.err = 0; |
1189 | dec.depth = 0; |
1388 | dec.depth = 0; |
1190 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1389 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1191 | |
1390 | |
|
|
1391 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1392 | dec.json.flags |= F_HOOK; |
|
|
1393 | |
1192 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1394 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1395 | |
|
|
1396 | decode_ws (&dec); |
1193 | sv = decode_sv (&dec); |
1397 | sv = decode_sv (&dec); |
1194 | |
1398 | |
1195 | if (!(offset_return || !sv)) |
1399 | if (!(offset_return || !sv)) |
1196 | { |
1400 | { |
1197 | // check for trailing garbage |
1401 | // check for trailing garbage |
… | |
… | |
1265 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1469 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1266 | } |
1470 | } |
1267 | |
1471 | |
1268 | PROTOTYPES: DISABLE |
1472 | PROTOTYPES: DISABLE |
1269 | |
1473 | |
|
|
1474 | void CLONE (...) |
|
|
1475 | CODE: |
|
|
1476 | json_stash = 0; |
|
|
1477 | json_boolean_stash = 0; |
|
|
1478 | |
1270 | void new (char *klass) |
1479 | void new (char *klass) |
1271 | PPCODE: |
1480 | PPCODE: |
1272 | { |
1481 | { |
1273 | SV *pv = NEWSV (0, sizeof (JSON)); |
1482 | SV *pv = NEWSV (0, sizeof (JSON)); |
1274 | SvPOK_only (pv); |
1483 | SvPOK_only (pv); |
1275 | Zero (SvPVX (pv), 1, sizeof (JSON)); |
1484 | Zero (SvPVX (pv), 1, JSON); |
1276 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1485 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1277 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
1486 | XPUSHs (sv_2mortal (sv_bless ( |
|
|
1487 | newRV_noinc (pv), |
|
|
1488 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
|
|
1489 | ))); |
1278 | } |
1490 | } |
1279 | |
1491 | |
1280 | void ascii (JSON *self, int enable = 1) |
1492 | void ascii (JSON *self, int enable = 1) |
1281 | ALIAS: |
1493 | ALIAS: |
1282 | ascii = F_ASCII |
1494 | ascii = F_ASCII |
… | |
… | |
1289 | pretty = F_PRETTY |
1501 | pretty = F_PRETTY |
1290 | allow_nonref = F_ALLOW_NONREF |
1502 | allow_nonref = F_ALLOW_NONREF |
1291 | shrink = F_SHRINK |
1503 | shrink = F_SHRINK |
1292 | allow_blessed = F_ALLOW_BLESSED |
1504 | allow_blessed = F_ALLOW_BLESSED |
1293 | convert_blessed = F_CONV_BLESSED |
1505 | convert_blessed = F_CONV_BLESSED |
|
|
1506 | relaxed = F_RELAXED |
1294 | PPCODE: |
1507 | PPCODE: |
1295 | { |
1508 | { |
1296 | if (enable) |
1509 | if (enable) |
1297 | self->flags |= ix; |
1510 | self->flags |= ix; |
1298 | else |
1511 | else |
1299 | self->flags &= ~ix; |
1512 | self->flags &= ~ix; |
1300 | |
1513 | |
1301 | XPUSHs (ST (0)); |
1514 | XPUSHs (ST (0)); |
1302 | } |
1515 | } |
1303 | |
1516 | |
|
|
1517 | void get_ascii (JSON *self) |
|
|
1518 | ALIAS: |
|
|
1519 | get_ascii = F_ASCII |
|
|
1520 | get_latin1 = F_LATIN1 |
|
|
1521 | get_utf8 = F_UTF8 |
|
|
1522 | get_indent = F_INDENT |
|
|
1523 | get_canonical = F_CANONICAL |
|
|
1524 | get_space_before = F_SPACE_BEFORE |
|
|
1525 | get_space_after = F_SPACE_AFTER |
|
|
1526 | get_allow_nonref = F_ALLOW_NONREF |
|
|
1527 | get_shrink = F_SHRINK |
|
|
1528 | get_allow_blessed = F_ALLOW_BLESSED |
|
|
1529 | get_convert_blessed = F_CONV_BLESSED |
|
|
1530 | get_relaxed = F_RELAXED |
|
|
1531 | PPCODE: |
|
|
1532 | XPUSHs (boolSV (self->flags & ix)); |
|
|
1533 | |
1304 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1534 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1305 | PPCODE: |
1535 | PPCODE: |
1306 | { |
1536 | { |
1307 | UV log2 = 0; |
1537 | UV log2 = 0; |
1308 | |
1538 | |
… | |
… | |
1314 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1544 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1315 | |
1545 | |
1316 | XPUSHs (ST (0)); |
1546 | XPUSHs (ST (0)); |
1317 | } |
1547 | } |
1318 | |
1548 | |
|
|
1549 | U32 get_max_depth (JSON *self) |
|
|
1550 | CODE: |
|
|
1551 | RETVAL = DEC_DEPTH (self->flags); |
|
|
1552 | OUTPUT: |
|
|
1553 | RETVAL |
|
|
1554 | |
1319 | void max_size (JSON *self, UV max_size = 0) |
1555 | void max_size (JSON *self, UV max_size = 0) |
1320 | PPCODE: |
1556 | PPCODE: |
1321 | { |
1557 | { |
1322 | UV log2 = 0; |
1558 | UV log2 = 0; |
1323 | |
1559 | |
… | |
… | |
1326 | |
1562 | |
1327 | while ((1UL << log2) < max_size) |
1563 | while ((1UL << log2) < max_size) |
1328 | ++log2; |
1564 | ++log2; |
1329 | |
1565 | |
1330 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1566 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1567 | |
|
|
1568 | XPUSHs (ST (0)); |
|
|
1569 | } |
|
|
1570 | |
|
|
1571 | int get_max_size (JSON *self) |
|
|
1572 | CODE: |
|
|
1573 | RETVAL = DEC_SIZE (self->flags); |
|
|
1574 | OUTPUT: |
|
|
1575 | RETVAL |
|
|
1576 | |
|
|
1577 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
|
|
1578 | PPCODE: |
|
|
1579 | { |
|
|
1580 | SvREFCNT_dec (self->cb_object); |
|
|
1581 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1582 | |
|
|
1583 | XPUSHs (ST (0)); |
|
|
1584 | } |
|
|
1585 | |
|
|
1586 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1587 | PPCODE: |
|
|
1588 | { |
|
|
1589 | if (!self->cb_sk_object) |
|
|
1590 | self->cb_sk_object = newHV (); |
|
|
1591 | |
|
|
1592 | if (SvOK (cb)) |
|
|
1593 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1594 | else |
|
|
1595 | { |
|
|
1596 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1597 | |
|
|
1598 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1599 | { |
|
|
1600 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1601 | self->cb_sk_object = 0; |
|
|
1602 | } |
|
|
1603 | } |
1331 | |
1604 | |
1332 | XPUSHs (ST (0)); |
1605 | XPUSHs (ST (0)); |
1333 | } |
1606 | } |
1334 | |
1607 | |
1335 | void encode (JSON *self, SV *scalar) |
1608 | void encode (JSON *self, SV *scalar) |
… | |
… | |
1347 | EXTEND (SP, 2); |
1620 | EXTEND (SP, 2); |
1348 | PUSHs (decode_json (jsonstr, self, &offset)); |
1621 | PUSHs (decode_json (jsonstr, self, &offset)); |
1349 | PUSHs (sv_2mortal (newSVuv (offset))); |
1622 | PUSHs (sv_2mortal (newSVuv (offset))); |
1350 | } |
1623 | } |
1351 | |
1624 | |
|
|
1625 | void DESTROY (JSON *self) |
|
|
1626 | CODE: |
|
|
1627 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1628 | SvREFCNT_dec (self->cb_object); |
|
|
1629 | |
1352 | PROTOTYPES: ENABLE |
1630 | PROTOTYPES: ENABLE |
1353 | |
1631 | |
1354 | void to_json (SV *scalar) |
1632 | void encode_json (SV *scalar) |
1355 | PPCODE: |
1633 | PPCODE: |
1356 | { |
1634 | { |
1357 | JSON json = { F_DEFAULT | F_UTF8 }; |
1635 | JSON json = { F_DEFAULT | F_UTF8 }; |
1358 | XPUSHs (encode_json (scalar, &json)); |
1636 | XPUSHs (encode_json (scalar, &json)); |
1359 | } |
1637 | } |
1360 | |
1638 | |
1361 | void from_json (SV *jsonstr) |
1639 | void decode_json (SV *jsonstr) |
1362 | PPCODE: |
1640 | PPCODE: |
1363 | { |
1641 | { |
1364 | JSON json = { F_DEFAULT | F_UTF8 }; |
1642 | JSON json = { F_DEFAULT | F_UTF8 }; |
1365 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1643 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1366 | } |
1644 | } |