… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include "assert.h" |
6 | #include "string.h" |
6 | #include "string.h" |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
|
|
8 | #include "stdio.h" |
8 | |
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
|
|
13 | |
9 | #define F_ASCII 0x00000001 |
14 | #define F_ASCII 0x00000001UL |
|
|
15 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002 |
16 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004 |
17 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008 |
18 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
19 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020 |
20 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
21 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100 |
22 | #define F_SHRINK 0x00000200UL |
|
|
23 | #define F_MAXDEPTH 0xf8000000UL |
|
|
24 | #define S_MAXDEPTH 27 |
17 | |
25 | |
18 | // F_SKIPINVALID? |
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
19 | // F_EXECCODEREF? |
27 | |
20 | // F_SELFCONVERT? <=> { &__class__ => } |
28 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
29 | // F_BLESSED? <=> { $__class__$ => } |
21 | |
30 | |
22 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
23 | #define F_DEFAULT 0 |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
24 | |
33 | |
25 | #define INIT_SIZE 32 // initial scalar size to be allocated |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
26 | #define INDENT_STEP 3 // spaces per indentation level |
35 | #define INDENT_STEP 3 // spaces per indentation level |
27 | |
36 | |
28 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
29 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
30 | |
38 | |
31 | #define SB do { |
39 | #define SB do { |
32 | #define SE } while (0) |
40 | #define SE } while (0) |
|
|
41 | |
|
|
42 | #if __GNUC__ >= 3 |
|
|
43 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
44 | # define inline inline |
|
|
45 | #else |
|
|
46 | # define expect(expr,value) (expr) |
|
|
47 | # define inline static |
|
|
48 | #endif |
|
|
49 | |
|
|
50 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
51 | #define expect_true(expr) expect ((expr) != 0, 1) |
33 | |
52 | |
34 | static HV *json_stash; // JSON::XS:: |
53 | static HV *json_stash; // JSON::XS:: |
35 | |
54 | |
36 | ///////////////////////////////////////////////////////////////////////////// |
55 | ///////////////////////////////////////////////////////////////////////////// |
37 | // utility functions |
56 | // utility functions |
… | |
… | |
62 | // decode an utf-8 character and return it, or (UV)-1 in |
81 | // decode an utf-8 character and return it, or (UV)-1 in |
63 | // case of an error. |
82 | // case of an error. |
64 | // we special-case "safe" characters from U+80 .. U+7FF, |
83 | // we special-case "safe" characters from U+80 .. U+7FF, |
65 | // but use the very good perl function to parse anything else. |
84 | // but use the very good perl function to parse anything else. |
66 | // note that we never call this function for a ascii codepoints |
85 | // note that we never call this function for a ascii codepoints |
67 | static UV |
86 | inline UV |
68 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
87 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
69 | { |
88 | { |
70 | if (s[0] > 0xdf || s[0] < 0xc2) |
89 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
71 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
90 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
72 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
91 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
73 | { |
92 | { |
74 | *clen = 2; |
93 | *clen = 2; |
75 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
94 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
76 | } |
95 | } |
77 | else |
96 | else |
|
|
97 | { |
|
|
98 | *clen = (STRLEN)-1; |
78 | return (UV)-1; |
99 | return (UV)-1; |
|
|
100 | } |
79 | } |
101 | } |
80 | |
102 | |
81 | ///////////////////////////////////////////////////////////////////////////// |
103 | ///////////////////////////////////////////////////////////////////////////// |
82 | // encoder |
104 | // encoder |
83 | |
105 | |
… | |
… | |
85 | typedef struct |
107 | typedef struct |
86 | { |
108 | { |
87 | char *cur; // SvPVX (sv) + current output position |
109 | char *cur; // SvPVX (sv) + current output position |
88 | char *end; // SvEND (sv) |
110 | char *end; // SvEND (sv) |
89 | SV *sv; // result scalar |
111 | SV *sv; // result scalar |
90 | UV flags; // F_* |
112 | U32 flags; // F_* |
91 | int indent; // indentation level |
113 | U32 indent; // indentation level |
92 | int max_depth; // max. recursion level |
114 | U32 maxdepth; // max. indentation/recursion level |
93 | } enc_t; |
115 | } enc_t; |
94 | |
116 | |
95 | static void |
117 | inline void |
96 | need (enc_t *enc, STRLEN len) |
118 | need (enc_t *enc, STRLEN len) |
97 | { |
119 | { |
98 | if (enc->cur + len >= enc->end) |
120 | if (expect_false (enc->cur + len >= enc->end)) |
99 | { |
121 | { |
100 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
122 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
101 | SvGROW (enc->sv, cur + len + 1); |
123 | SvGROW (enc->sv, cur + len + 1); |
102 | enc->cur = SvPVX (enc->sv) + cur; |
124 | enc->cur = SvPVX (enc->sv) + cur; |
103 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
125 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
104 | } |
126 | } |
105 | } |
127 | } |
106 | |
128 | |
107 | static void |
129 | inline void |
108 | encode_ch (enc_t *enc, char ch) |
130 | encode_ch (enc_t *enc, char ch) |
109 | { |
131 | { |
110 | need (enc, 1); |
132 | need (enc, 1); |
111 | *enc->cur++ = ch; |
133 | *enc->cur++ = ch; |
112 | } |
134 | } |
… | |
… | |
120 | |
142 | |
121 | while (str < end) |
143 | while (str < end) |
122 | { |
144 | { |
123 | unsigned char ch = *(unsigned char *)str; |
145 | unsigned char ch = *(unsigned char *)str; |
124 | |
146 | |
125 | if (ch >= 0x20 && ch < 0x80) // most common case |
147 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
126 | { |
148 | { |
127 | if (ch == '"') // but with slow exceptions |
149 | if (expect_false (ch == '"')) // but with slow exceptions |
128 | { |
150 | { |
129 | need (enc, len += 1); |
151 | need (enc, len += 1); |
130 | *enc->cur++ = '\\'; |
152 | *enc->cur++ = '\\'; |
131 | *enc->cur++ = '"'; |
153 | *enc->cur++ = '"'; |
132 | } |
154 | } |
133 | else if (ch == '\\') |
155 | else if (expect_false (ch == '\\')) |
134 | { |
156 | { |
135 | need (enc, len += 1); |
157 | need (enc, len += 1); |
136 | *enc->cur++ = '\\'; |
158 | *enc->cur++ = '\\'; |
137 | *enc->cur++ = '\\'; |
159 | *enc->cur++ = '\\'; |
138 | } |
160 | } |
… | |
… | |
170 | } |
192 | } |
171 | |
193 | |
172 | if (uch > 0x10FFFFUL) |
194 | if (uch > 0x10FFFFUL) |
173 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
195 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
174 | |
196 | |
175 | if (uch < 0x80 || enc->flags & F_ASCII) |
197 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
176 | { |
198 | { |
177 | if (uch > 0xFFFFUL) |
199 | if (uch > 0xFFFFUL) |
178 | { |
200 | { |
179 | need (enc, len += 11); |
201 | need (enc, len += 11); |
180 | sprintf (enc->cur, "\\u%04x\\u%04x", |
202 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
194 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
216 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
195 | } |
217 | } |
196 | |
218 | |
197 | str += clen; |
219 | str += clen; |
198 | } |
220 | } |
|
|
221 | else if (enc->flags & F_LATIN1) |
|
|
222 | { |
|
|
223 | *enc->cur++ = uch; |
|
|
224 | str += clen; |
|
|
225 | } |
199 | else if (is_utf8) |
226 | else if (is_utf8) |
200 | { |
227 | { |
201 | need (enc, len += clen); |
228 | need (enc, len += clen); |
202 | do |
229 | do |
203 | { |
230 | { |
… | |
… | |
205 | } |
232 | } |
206 | while (--clen); |
233 | while (--clen); |
207 | } |
234 | } |
208 | else |
235 | else |
209 | { |
236 | { |
210 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
237 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
211 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
238 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
212 | ++str; |
239 | ++str; |
213 | } |
240 | } |
214 | } |
241 | } |
215 | } |
242 | } |
… | |
… | |
217 | |
244 | |
218 | --len; |
245 | --len; |
219 | } |
246 | } |
220 | } |
247 | } |
221 | |
248 | |
222 | static void |
249 | inline void |
223 | encode_indent (enc_t *enc) |
250 | encode_indent (enc_t *enc) |
224 | { |
251 | { |
225 | if (enc->flags & F_INDENT) |
252 | if (enc->flags & F_INDENT) |
226 | { |
253 | { |
227 | int spaces = enc->indent * INDENT_STEP; |
254 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
230 | memset (enc->cur, ' ', spaces); |
257 | memset (enc->cur, ' ', spaces); |
231 | enc->cur += spaces; |
258 | enc->cur += spaces; |
232 | } |
259 | } |
233 | } |
260 | } |
234 | |
261 | |
235 | static void |
262 | inline void |
236 | encode_space (enc_t *enc) |
263 | encode_space (enc_t *enc) |
237 | { |
264 | { |
238 | need (enc, 1); |
265 | need (enc, 1); |
239 | encode_ch (enc, ' '); |
266 | encode_ch (enc, ' '); |
240 | } |
267 | } |
241 | |
268 | |
242 | static void |
269 | inline void |
243 | encode_nl (enc_t *enc) |
270 | encode_nl (enc_t *enc) |
244 | { |
271 | { |
245 | if (enc->flags & F_INDENT) |
272 | if (enc->flags & F_INDENT) |
246 | { |
273 | { |
247 | need (enc, 1); |
274 | need (enc, 1); |
248 | encode_ch (enc, '\n'); |
275 | encode_ch (enc, '\n'); |
249 | } |
276 | } |
250 | } |
277 | } |
251 | |
278 | |
252 | static void |
279 | inline void |
253 | encode_comma (enc_t *enc) |
280 | encode_comma (enc_t *enc) |
254 | { |
281 | { |
255 | encode_ch (enc, ','); |
282 | encode_ch (enc, ','); |
256 | |
283 | |
257 | if (enc->flags & F_INDENT) |
284 | if (enc->flags & F_INDENT) |
… | |
… | |
264 | |
291 | |
265 | static void |
292 | static void |
266 | encode_av (enc_t *enc, AV *av) |
293 | encode_av (enc_t *enc, AV *av) |
267 | { |
294 | { |
268 | int i, len = av_len (av); |
295 | int i, len = av_len (av); |
|
|
296 | |
|
|
297 | if (enc->indent >= enc->maxdepth) |
|
|
298 | croak ("data structure too deep (hit recursion limit)"); |
269 | |
299 | |
270 | encode_ch (enc, '['); encode_nl (enc); |
300 | encode_ch (enc, '['); encode_nl (enc); |
271 | ++enc->indent; |
301 | ++enc->indent; |
272 | |
302 | |
273 | for (i = 0; i <= len; ++i) |
303 | for (i = 0; i <= len; ++i) |
… | |
… | |
340 | static void |
370 | static void |
341 | encode_hv (enc_t *enc, HV *hv) |
371 | encode_hv (enc_t *enc, HV *hv) |
342 | { |
372 | { |
343 | int count, i; |
373 | int count, i; |
344 | |
374 | |
|
|
375 | if (enc->indent >= enc->maxdepth) |
|
|
376 | croak ("data structure too deep (hit recursion limit)"); |
|
|
377 | |
345 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
378 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
346 | |
379 | |
347 | if ((count = hv_iterinit (hv))) |
380 | if ((count = hv_iterinit (hv))) |
348 | { |
381 | { |
349 | // for canonical output we have to sort by keys first |
382 | // for canonical output we have to sort by keys first |
350 | // actually, this is mostly due to the stupid so-called |
383 | // actually, this is mostly due to the stupid so-called |
351 | // security workaround added somewhere in 5.8.x. |
384 | // security workaround added somewhere in 5.8.x. |
352 | // that randomises hash orderings |
385 | // that randomises hash orderings |
353 | if (enc->flags & F_CANONICAL) |
386 | if (enc->flags & F_CANONICAL) |
354 | { |
387 | { |
355 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
356 | int fast = 1; |
388 | int fast = 1; |
|
|
389 | HE *he; |
|
|
390 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
391 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
392 | #else |
|
|
393 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
394 | #endif |
357 | |
395 | |
358 | i = 0; |
396 | i = 0; |
359 | while ((he = hv_iternext (hv))) |
397 | while ((he = hv_iternext (hv))) |
360 | { |
398 | { |
361 | hes [i++] = he; |
399 | hes [i++] = he; |
… | |
… | |
396 | |
434 | |
397 | encode_nl (enc); |
435 | encode_nl (enc); |
398 | } |
436 | } |
399 | else |
437 | else |
400 | { |
438 | { |
401 | SV *sv; |
|
|
402 | HE *he = hv_iternext (hv); |
439 | HE *he = hv_iternext (hv); |
403 | |
440 | |
404 | for (;;) |
441 | for (;;) |
405 | { |
442 | { |
406 | encode_indent (enc); |
443 | encode_indent (enc); |
… | |
… | |
415 | encode_nl (enc); |
452 | encode_nl (enc); |
416 | } |
453 | } |
417 | } |
454 | } |
418 | |
455 | |
419 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
456 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
457 | } |
|
|
458 | |
|
|
459 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
460 | static void |
|
|
461 | encode_rv (enc_t *enc, SV *sv) |
|
|
462 | { |
|
|
463 | svtype svt; |
|
|
464 | |
|
|
465 | SvGETMAGIC (sv); |
|
|
466 | svt = SvTYPE (sv); |
|
|
467 | |
|
|
468 | if (svt == SVt_PVHV) |
|
|
469 | encode_hv (enc, (HV *)sv); |
|
|
470 | else if (svt == SVt_PVAV) |
|
|
471 | encode_av (enc, (AV *)sv); |
|
|
472 | else if (svt < SVt_PVAV) |
|
|
473 | { |
|
|
474 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
475 | encode_str (enc, "false", 5, 0); |
|
|
476 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
477 | encode_str (enc, "true", 4, 0); |
|
|
478 | else |
|
|
479 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
480 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
481 | } |
|
|
482 | else |
|
|
483 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
484 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
420 | } |
485 | } |
421 | |
486 | |
422 | static void |
487 | static void |
423 | encode_sv (enc_t *enc, SV *sv) |
488 | encode_sv (enc_t *enc, SV *sv) |
424 | { |
489 | { |
… | |
… | |
438 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
503 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
439 | enc->cur += strlen (enc->cur); |
504 | enc->cur += strlen (enc->cur); |
440 | } |
505 | } |
441 | else if (SvIOKp (sv)) |
506 | else if (SvIOKp (sv)) |
442 | { |
507 | { |
|
|
508 | // we assume we can always read an IV as a UV |
|
|
509 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
510 | { |
443 | need (enc, 64); |
511 | need (enc, 64); |
444 | enc->cur += |
512 | enc->cur += |
445 | SvIsUV(sv) |
513 | SvIsUV(sv) |
446 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
514 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
447 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
515 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
|
|
516 | } |
|
|
517 | else |
|
|
518 | { |
|
|
519 | // optimise the "small number case" |
|
|
520 | // code will likely be branchless and use only a single multiplication |
|
|
521 | I32 i = SvIV (sv); |
|
|
522 | U32 u; |
|
|
523 | |
|
|
524 | need (enc, 6); |
|
|
525 | |
|
|
526 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
527 | u = i < 0 ? -i : i; |
|
|
528 | |
|
|
529 | // convert to 4.28 fixed-point representation |
|
|
530 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
531 | |
|
|
532 | char digit, nz = 0; |
|
|
533 | |
|
|
534 | digit = u >> 28; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0xfffffff) * 5; |
|
|
535 | digit = u >> 27; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x7ffffff) * 5; |
|
|
536 | digit = u >> 26; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x3ffffff) * 5; |
|
|
537 | digit = u >> 25; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x1ffffff) * 5; |
|
|
538 | digit = u >> 24; *enc->cur = digit + '0'; nz |= digit; enc->cur += 1; |
|
|
539 | } |
448 | } |
540 | } |
449 | else if (SvROK (sv)) |
541 | else if (SvROK (sv)) |
450 | { |
542 | encode_rv (enc, SvRV (sv)); |
451 | SV *rv = SvRV (sv); |
|
|
452 | |
|
|
453 | if (enc->indent >= enc->max_depth) |
|
|
454 | croak ("data structure too deep (hit recursion limit)"); |
|
|
455 | |
|
|
456 | switch (SvTYPE (rv)) |
|
|
457 | { |
|
|
458 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
459 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
460 | |
|
|
461 | default: |
|
|
462 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
463 | SvPV_nolen (sv)); |
|
|
464 | } |
|
|
465 | } |
|
|
466 | else if (!SvOK (sv)) |
543 | else if (!SvOK (sv)) |
467 | encode_str (enc, "null", 4, 0); |
544 | encode_str (enc, "null", 4, 0); |
468 | else |
545 | else |
469 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
546 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
470 | SvPV_nolen (sv), SvFLAGS (sv)); |
547 | SvPV_nolen (sv), SvFLAGS (sv)); |
471 | } |
548 | } |
472 | |
549 | |
473 | static SV * |
550 | static SV * |
474 | encode_json (SV *scalar, UV flags) |
551 | encode_json (SV *scalar, U32 flags) |
475 | { |
552 | { |
|
|
553 | enc_t enc; |
|
|
554 | |
476 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
555 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
477 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
556 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
478 | |
557 | |
479 | enc_t enc; |
|
|
480 | enc.flags = flags; |
558 | enc.flags = flags; |
481 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
559 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
482 | enc.cur = SvPVX (enc.sv); |
560 | enc.cur = SvPVX (enc.sv); |
483 | enc.end = SvEND (enc.sv); |
561 | enc.end = SvEND (enc.sv); |
484 | enc.indent = 0; |
562 | enc.indent = 0; |
485 | enc.max_depth = 0x7fffffffUL; |
563 | enc.maxdepth = DEC_DEPTH (flags); |
486 | |
564 | |
487 | SvPOK_only (enc.sv); |
565 | SvPOK_only (enc.sv); |
488 | encode_sv (&enc, scalar); |
566 | encode_sv (&enc, scalar); |
489 | |
567 | |
|
|
568 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
569 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
570 | |
490 | if (!(flags & (F_ASCII | F_UTF8))) |
571 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
491 | SvUTF8_on (enc.sv); |
572 | SvUTF8_on (enc.sv); |
492 | |
|
|
493 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
494 | |
573 | |
495 | if (enc.flags & F_SHRINK) |
574 | if (enc.flags & F_SHRINK) |
496 | shrink (enc.sv); |
575 | shrink (enc.sv); |
497 | |
576 | |
498 | return enc.sv; |
577 | return enc.sv; |
… | |
… | |
505 | typedef struct |
584 | typedef struct |
506 | { |
585 | { |
507 | char *cur; // current parser pointer |
586 | char *cur; // current parser pointer |
508 | char *end; // end of input string |
587 | char *end; // end of input string |
509 | const char *err; // parse error, if != 0 |
588 | const char *err; // parse error, if != 0 |
510 | UV flags; // F_* |
589 | U32 flags; // F_* |
|
|
590 | U32 depth; // recursion depth |
|
|
591 | U32 maxdepth; // recursion depth limit |
511 | } dec_t; |
592 | } dec_t; |
512 | |
593 | |
513 | static void |
594 | inline void |
514 | decode_ws (dec_t *dec) |
595 | decode_ws (dec_t *dec) |
515 | { |
596 | { |
516 | for (;;) |
597 | for (;;) |
517 | { |
598 | { |
518 | char ch = *dec->cur; |
599 | char ch = *dec->cur; |
… | |
… | |
524 | ++dec->cur; |
605 | ++dec->cur; |
525 | } |
606 | } |
526 | } |
607 | } |
527 | |
608 | |
528 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
609 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
610 | |
529 | #define EXPECT_CH(ch) SB \ |
611 | #define EXPECT_CH(ch) SB \ |
530 | if (*dec->cur != ch) \ |
612 | if (*dec->cur != ch) \ |
531 | ERR (# ch " expected"); \ |
613 | ERR (# ch " expected"); \ |
532 | ++dec->cur; \ |
614 | ++dec->cur; \ |
533 | SE |
615 | SE |
534 | |
616 | |
|
|
617 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
618 | #define DEC_DEC_DEPTH --dec->depth |
|
|
619 | |
535 | static SV *decode_sv (dec_t *dec); |
620 | static SV *decode_sv (dec_t *dec); |
536 | |
621 | |
537 | static signed char decode_hexdigit[256]; |
622 | static signed char decode_hexdigit[256]; |
538 | |
623 | |
539 | static UV |
624 | static UV |
540 | decode_4hex (dec_t *dec) |
625 | decode_4hex (dec_t *dec) |
541 | { |
626 | { |
542 | signed char d1, d2, d3, d4; |
627 | signed char d1, d2, d3, d4; |
543 | unsigned char *cur = (unsigned char *)dec->cur; |
628 | unsigned char *cur = (unsigned char *)dec->cur; |
544 | |
629 | |
545 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
630 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
546 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
631 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); |
547 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
632 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); |
548 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
633 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); |
549 | |
634 | |
550 | dec->cur += 4; |
635 | dec->cur += 4; |
551 | |
636 | |
552 | return ((UV)d1) << 12 |
637 | return ((UV)d1) << 12 |
553 | | ((UV)d2) << 8 |
638 | | ((UV)d2) << 8 |
… | |
… | |
564 | SV *sv = 0; |
649 | SV *sv = 0; |
565 | int utf8 = 0; |
650 | int utf8 = 0; |
566 | |
651 | |
567 | do |
652 | do |
568 | { |
653 | { |
569 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
654 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
570 | char *cur = buf; |
655 | char *cur = buf; |
571 | |
656 | |
572 | do |
657 | do |
573 | { |
658 | { |
574 | unsigned char ch = *(unsigned char *)dec->cur++; |
659 | unsigned char ch = *(unsigned char *)dec->cur++; |
575 | |
660 | |
576 | if (ch == '"') |
661 | if (expect_false (ch == '"')) |
577 | { |
662 | { |
578 | --dec->cur; |
663 | --dec->cur; |
579 | break; |
664 | break; |
580 | } |
665 | } |
581 | else if (ch == '\\') |
666 | else if (expect_false (ch == '\\')) |
582 | { |
667 | { |
583 | switch (*dec->cur) |
668 | switch (*dec->cur) |
584 | { |
669 | { |
585 | case '\\': |
670 | case '\\': |
586 | case '/': |
671 | case '/': |
… | |
… | |
636 | default: |
721 | default: |
637 | --dec->cur; |
722 | --dec->cur; |
638 | ERR ("illegal backslash escape sequence in string"); |
723 | ERR ("illegal backslash escape sequence in string"); |
639 | } |
724 | } |
640 | } |
725 | } |
641 | else if (ch >= 0x20 && ch <= 0x7f) |
726 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
642 | *cur++ = ch; |
727 | *cur++ = ch; |
643 | else if (ch >= 0x80) |
728 | else if (ch >= 0x80) |
644 | { |
729 | { |
|
|
730 | STRLEN clen; |
|
|
731 | UV uch; |
|
|
732 | |
645 | --dec->cur; |
733 | --dec->cur; |
646 | |
734 | |
647 | STRLEN clen; |
|
|
648 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
735 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
649 | if (clen == (STRLEN)-1) |
736 | if (clen == (STRLEN)-1) |
650 | ERR ("malformed UTF-8 character in JSON string"); |
737 | ERR ("malformed UTF-8 character in JSON string"); |
651 | |
738 | |
652 | do |
739 | do |
653 | { |
|
|
654 | *cur++ = *dec->cur++; |
740 | *cur++ = *dec->cur++; |
655 | } |
|
|
656 | while (--clen); |
741 | while (--clen); |
657 | |
742 | |
658 | utf8 = 1; |
743 | utf8 = 1; |
659 | } |
744 | } |
660 | else if (!ch) |
|
|
661 | ERR ("unexpected end of string while parsing json string"); |
|
|
662 | else |
745 | else |
|
|
746 | { |
|
|
747 | --dec->cur; |
|
|
748 | |
|
|
749 | if (!ch) |
|
|
750 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
751 | else |
663 | ERR ("invalid character encountered"); |
752 | ERR ("invalid character encountered while parsing JSON string"); |
664 | |
753 | } |
665 | } |
754 | } |
666 | while (cur < buf + SHORT_STRING_LEN); |
755 | while (cur < buf + SHORT_STRING_LEN); |
667 | |
756 | |
|
|
757 | { |
668 | STRLEN len = cur - buf; |
758 | STRLEN len = cur - buf; |
669 | |
759 | |
670 | if (sv) |
760 | if (sv) |
671 | { |
761 | { |
672 | SvGROW (sv, SvCUR (sv) + len + 1); |
762 | SvGROW (sv, SvCUR (sv) + len + 1); |
673 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
763 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
674 | SvCUR_set (sv, SvCUR (sv) + len); |
764 | SvCUR_set (sv, SvCUR (sv) + len); |
675 | } |
765 | } |
676 | else |
766 | else |
677 | sv = newSVpvn (buf, len); |
767 | sv = newSVpvn (buf, len); |
|
|
768 | } |
678 | } |
769 | } |
679 | while (*dec->cur != '"'); |
770 | while (*dec->cur != '"'); |
680 | |
771 | |
681 | ++dec->cur; |
772 | ++dec->cur; |
682 | |
773 | |
… | |
… | |
759 | is_nv = 1; |
850 | is_nv = 1; |
760 | } |
851 | } |
761 | |
852 | |
762 | if (!is_nv) |
853 | if (!is_nv) |
763 | { |
854 | { |
764 | UV uv; |
855 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
765 | int numtype = grok_number (start, dec->cur - start, &uv); |
856 | if (*start == '-') |
766 | if (numtype & IS_NUMBER_IN_UV) |
857 | switch (dec->cur - start) |
767 | if (numtype & IS_NUMBER_NEG) |
|
|
768 | { |
858 | { |
769 | if (uv < (UV)IV_MIN) |
859 | case 2: return newSViv (-( start [1] - '0' )); |
770 | return newSViv (-(IV)uv); |
860 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
861 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
862 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
771 | } |
863 | } |
|
|
864 | else |
|
|
865 | switch (dec->cur - start) |
|
|
866 | { |
|
|
867 | case 1: return newSViv ( start [0] - '0' ); |
|
|
868 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
869 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
870 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
871 | } |
|
|
872 | |
|
|
873 | { |
|
|
874 | UV uv; |
|
|
875 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
876 | if (numtype & IS_NUMBER_IN_UV) |
|
|
877 | if (numtype & IS_NUMBER_NEG) |
|
|
878 | { |
|
|
879 | if (uv < (UV)IV_MIN) |
|
|
880 | return newSViv (-(IV)uv); |
|
|
881 | } |
772 | else |
882 | else |
773 | return newSVuv (uv); |
883 | return newSVuv (uv); |
|
|
884 | } |
774 | } |
885 | } |
775 | |
886 | |
776 | return newSVnv (Atof (start)); |
887 | return newSVnv (Atof (start)); |
777 | |
888 | |
778 | fail: |
889 | fail: |
… | |
… | |
782 | static SV * |
893 | static SV * |
783 | decode_av (dec_t *dec) |
894 | decode_av (dec_t *dec) |
784 | { |
895 | { |
785 | AV *av = newAV (); |
896 | AV *av = newAV (); |
786 | |
897 | |
|
|
898 | DEC_INC_DEPTH; |
787 | decode_ws (dec); |
899 | decode_ws (dec); |
|
|
900 | |
788 | if (*dec->cur == ']') |
901 | if (*dec->cur == ']') |
789 | ++dec->cur; |
902 | ++dec->cur; |
790 | else |
903 | else |
791 | for (;;) |
904 | for (;;) |
792 | { |
905 | { |
… | |
… | |
810 | ERR (", or ] expected while parsing array"); |
923 | ERR (", or ] expected while parsing array"); |
811 | |
924 | |
812 | ++dec->cur; |
925 | ++dec->cur; |
813 | } |
926 | } |
814 | |
927 | |
|
|
928 | DEC_DEC_DEPTH; |
815 | return newRV_noinc ((SV *)av); |
929 | return newRV_noinc ((SV *)av); |
816 | |
930 | |
817 | fail: |
931 | fail: |
818 | SvREFCNT_dec (av); |
932 | SvREFCNT_dec (av); |
|
|
933 | DEC_DEC_DEPTH; |
819 | return 0; |
934 | return 0; |
820 | } |
935 | } |
821 | |
936 | |
822 | static SV * |
937 | static SV * |
823 | decode_hv (dec_t *dec) |
938 | decode_hv (dec_t *dec) |
824 | { |
939 | { |
825 | HV *hv = newHV (); |
940 | HV *hv = newHV (); |
826 | |
941 | |
|
|
942 | DEC_INC_DEPTH; |
827 | decode_ws (dec); |
943 | decode_ws (dec); |
|
|
944 | |
828 | if (*dec->cur == '}') |
945 | if (*dec->cur == '}') |
829 | ++dec->cur; |
946 | ++dec->cur; |
830 | else |
947 | else |
831 | for (;;) |
948 | for (;;) |
832 | { |
949 | { |
… | |
… | |
845 | { |
962 | { |
846 | SvREFCNT_dec (key); |
963 | SvREFCNT_dec (key); |
847 | goto fail; |
964 | goto fail; |
848 | } |
965 | } |
849 | |
966 | |
850 | //TODO: optimise |
|
|
851 | hv_store_ent (hv, key, value, 0); |
967 | hv_store_ent (hv, key, value, 0); |
|
|
968 | SvREFCNT_dec (key); |
852 | |
969 | |
853 | decode_ws (dec); |
970 | decode_ws (dec); |
854 | |
971 | |
855 | if (*dec->cur == '}') |
972 | if (*dec->cur == '}') |
856 | { |
973 | { |
… | |
… | |
862 | ERR (", or } expected while parsing object/hash"); |
979 | ERR (", or } expected while parsing object/hash"); |
863 | |
980 | |
864 | ++dec->cur; |
981 | ++dec->cur; |
865 | } |
982 | } |
866 | |
983 | |
|
|
984 | DEC_DEC_DEPTH; |
867 | return newRV_noinc ((SV *)hv); |
985 | return newRV_noinc ((SV *)hv); |
868 | |
986 | |
869 | fail: |
987 | fail: |
870 | SvREFCNT_dec (hv); |
988 | SvREFCNT_dec (hv); |
|
|
989 | DEC_DEC_DEPTH; |
871 | return 0; |
990 | return 0; |
872 | } |
991 | } |
873 | |
992 | |
874 | static SV * |
993 | static SV * |
875 | decode_sv (dec_t *dec) |
994 | decode_sv (dec_t *dec) |
… | |
… | |
918 | ERR ("'null' expected"); |
1037 | ERR ("'null' expected"); |
919 | |
1038 | |
920 | break; |
1039 | break; |
921 | |
1040 | |
922 | default: |
1041 | default: |
923 | ERR ("malformed json string, neither array, object, number, string or atom"); |
1042 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
924 | break; |
1043 | break; |
925 | } |
1044 | } |
926 | |
1045 | |
927 | fail: |
1046 | fail: |
928 | return 0; |
1047 | return 0; |
929 | } |
1048 | } |
930 | |
1049 | |
931 | static SV * |
1050 | static SV * |
932 | decode_json (SV *string, UV flags) |
1051 | decode_json (SV *string, U32 flags, UV *offset_return) |
933 | { |
1052 | { |
|
|
1053 | dec_t dec; |
|
|
1054 | UV offset; |
934 | SV *sv; |
1055 | SV *sv; |
|
|
1056 | |
|
|
1057 | SvGETMAGIC (string); |
|
|
1058 | SvUPGRADE (string, SVt_PV); |
935 | |
1059 | |
936 | if (flags & F_UTF8) |
1060 | if (flags & F_UTF8) |
937 | sv_utf8_downgrade (string, 0); |
1061 | sv_utf8_downgrade (string, 0); |
938 | else |
1062 | else |
939 | sv_utf8_upgrade (string); |
1063 | sv_utf8_upgrade (string); |
940 | |
1064 | |
941 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1065 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
942 | |
1066 | |
943 | dec_t dec; |
|
|
944 | dec.flags = flags; |
1067 | dec.flags = flags; |
945 | dec.cur = SvPVX (string); |
1068 | dec.cur = SvPVX (string); |
946 | dec.end = SvEND (string); |
1069 | dec.end = SvEND (string); |
947 | dec.err = 0; |
1070 | dec.err = 0; |
|
|
1071 | dec.depth = 0; |
|
|
1072 | dec.maxdepth = DEC_DEPTH (dec.flags); |
948 | |
1073 | |
|
|
1074 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
949 | sv = decode_sv (&dec); |
1075 | sv = decode_sv (&dec); |
950 | |
1076 | |
|
|
1077 | if (!(offset_return || !sv)) |
|
|
1078 | { |
|
|
1079 | // check for trailing garbage |
|
|
1080 | decode_ws (&dec); |
|
|
1081 | |
|
|
1082 | if (*dec.cur) |
|
|
1083 | { |
|
|
1084 | dec.err = "garbage after JSON object"; |
|
|
1085 | SvREFCNT_dec (sv); |
|
|
1086 | sv = 0; |
|
|
1087 | } |
|
|
1088 | } |
|
|
1089 | |
|
|
1090 | if (offset_return || !sv) |
|
|
1091 | { |
|
|
1092 | offset = dec.flags & F_UTF8 |
|
|
1093 | ? dec.cur - SvPVX (string) |
|
|
1094 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1095 | |
|
|
1096 | if (offset_return) |
|
|
1097 | *offset_return = offset; |
|
|
1098 | } |
|
|
1099 | |
951 | if (!sv) |
1100 | if (!sv) |
952 | { |
1101 | { |
953 | IV offset = dec.flags & F_UTF8 |
|
|
954 | ? dec.cur - SvPVX (string) |
|
|
955 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
956 | SV *uni = sv_newmortal (); |
1102 | SV *uni = sv_newmortal (); |
957 | |
1103 | |
958 | // horrible hack to silence warning inside pv_uni_display |
1104 | // horrible hack to silence warning inside pv_uni_display |
959 | COP cop = *PL_curcop; |
1105 | COP cop = *PL_curcop; |
960 | cop.cop_warnings = pWARN_NONE; |
1106 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
962 | SAVEVPTR (PL_curcop); |
1108 | SAVEVPTR (PL_curcop); |
963 | PL_curcop = &cop; |
1109 | PL_curcop = &cop; |
964 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1110 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
965 | LEAVE; |
1111 | LEAVE; |
966 | |
1112 | |
967 | croak ("%s, at character offset %d (%s)", |
1113 | croak ("%s, at character offset %d [\"%s\"]", |
968 | dec.err, |
1114 | dec.err, |
969 | (int)offset, |
1115 | (int)offset, |
970 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1116 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
971 | } |
1117 | } |
972 | |
1118 | |
… | |
… | |
986 | BOOT: |
1132 | BOOT: |
987 | { |
1133 | { |
988 | int i; |
1134 | int i; |
989 | |
1135 | |
990 | memset (decode_hexdigit, 0xff, 256); |
1136 | memset (decode_hexdigit, 0xff, 256); |
|
|
1137 | |
991 | for (i = 10; i--; ) |
1138 | for (i = 0; i < 256; ++i) |
992 | decode_hexdigit ['0' + i] = i; |
1139 | decode_hexdigit [i] = |
993 | |
1140 | i >= '0' && i <= '9' ? i - '0' |
994 | for (i = 7; i--; ) |
1141 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
995 | { |
1142 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
996 | decode_hexdigit ['a' + i] = 10 + i; |
1143 | : -1; |
997 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
998 | } |
|
|
999 | |
1144 | |
1000 | json_stash = gv_stashpv ("JSON::XS", 1); |
1145 | json_stash = gv_stashpv ("JSON::XS", 1); |
1001 | } |
1146 | } |
1002 | |
1147 | |
1003 | PROTOTYPES: DISABLE |
1148 | PROTOTYPES: DISABLE |
… | |
… | |
1009 | RETVAL |
1154 | RETVAL |
1010 | |
1155 | |
1011 | SV *ascii (SV *self, int enable = 1) |
1156 | SV *ascii (SV *self, int enable = 1) |
1012 | ALIAS: |
1157 | ALIAS: |
1013 | ascii = F_ASCII |
1158 | ascii = F_ASCII |
|
|
1159 | latin1 = F_LATIN1 |
1014 | utf8 = F_UTF8 |
1160 | utf8 = F_UTF8 |
1015 | indent = F_INDENT |
1161 | indent = F_INDENT |
1016 | canonical = F_CANONICAL |
1162 | canonical = F_CANONICAL |
1017 | space_before = F_SPACE_BEFORE |
1163 | space_before = F_SPACE_BEFORE |
1018 | space_after = F_SPACE_AFTER |
1164 | space_after = F_SPACE_AFTER |
… | |
… | |
1030 | RETVAL = newSVsv (self); |
1176 | RETVAL = newSVsv (self); |
1031 | } |
1177 | } |
1032 | OUTPUT: |
1178 | OUTPUT: |
1033 | RETVAL |
1179 | RETVAL |
1034 | |
1180 | |
|
|
1181 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1182 | CODE: |
|
|
1183 | { |
|
|
1184 | UV *uv = SvJSON (self); |
|
|
1185 | UV log2 = 0; |
|
|
1186 | |
|
|
1187 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1188 | |
|
|
1189 | while ((1UL << log2) < max_depth) |
|
|
1190 | ++log2; |
|
|
1191 | |
|
|
1192 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1193 | |
|
|
1194 | RETVAL = newSVsv (self); |
|
|
1195 | } |
|
|
1196 | OUTPUT: |
|
|
1197 | RETVAL |
|
|
1198 | |
1035 | void encode (SV *self, SV *scalar) |
1199 | void encode (SV *self, SV *scalar) |
1036 | PPCODE: |
1200 | PPCODE: |
1037 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1201 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1038 | |
1202 | |
1039 | void decode (SV *self, SV *jsonstr) |
1203 | void decode (SV *self, SV *jsonstr) |
1040 | PPCODE: |
1204 | PPCODE: |
1041 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1205 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1206 | |
|
|
1207 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1208 | PPCODE: |
|
|
1209 | { |
|
|
1210 | UV offset; |
|
|
1211 | EXTEND (SP, 2); |
|
|
1212 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1213 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1214 | } |
1042 | |
1215 | |
1043 | PROTOTYPES: ENABLE |
1216 | PROTOTYPES: ENABLE |
1044 | |
1217 | |
1045 | void to_json (SV *scalar) |
1218 | void to_json (SV *scalar) |
1046 | ALIAS: |
1219 | ALIAS: |
1047 | objToJson = 0 |
1220 | objToJson = 0 |
1048 | PPCODE: |
1221 | PPCODE: |
1049 | XPUSHs (encode_json (scalar, F_UTF8)); |
1222 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1050 | |
1223 | |
1051 | void from_json (SV *jsonstr) |
1224 | void from_json (SV *jsonstr) |
1052 | ALIAS: |
1225 | ALIAS: |
1053 | jsonToObj = 0 |
1226 | jsonToObj = 0 |
1054 | PPCODE: |
1227 | PPCODE: |
1055 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1228 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1056 | |
1229 | |