ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
Revision: 1.23
Committed: Tue Apr 3 23:34:17 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
Changes since 1.22: +13 -8 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include "assert.h"
6 #include "string.h"
7 #include "stdlib.h"
8
9 #define F_ASCII 0x00000001UL
10 #define F_UTF8 0x00000002UL
11 #define F_INDENT 0x00000004UL
12 #define F_CANONICAL 0x00000008UL
13 #define F_SPACE_BEFORE 0x00000010UL
14 #define F_SPACE_AFTER 0x00000020UL
15 #define F_ALLOW_NONREF 0x00000080UL
16 #define F_SHRINK 0x00000100UL
17 #define F_MAXDEPTH 0xf8000000UL
18 #define S_MAXDEPTH 27
19
20 #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
21
22 // F_SELFCONVERT? <=> to_json/toJson
23 // F_BLESSED? <=> { $__class__$ => }
24
25 #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
26 #define F_DEFAULT (12UL << S_MAXDEPTH)
27
28 #define INIT_SIZE 32 // initial scalar size to be allocated
29 #define INDENT_STEP 3 // spaces per indentation level
30
31 #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character
32 #define SHORT_STRING_LEN 512 // special-case strings of up to this size
33
34 #define SB do {
35 #define SE } while (0)
36
37 static HV *json_stash; // JSON::XS::
38
39 /////////////////////////////////////////////////////////////////////////////
40 // utility functions
41
42 static UV *
43 SvJSON (SV *sv)
44 {
45 if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash))
46 croak ("object is not of type JSON::XS");
47
48 return &SvUVX (SvRV (sv));
49 }
50
51 static void
52 shrink (SV *sv)
53 {
54 sv_utf8_downgrade (sv, 1);
55 if (SvLEN (sv) > SvCUR (sv) + 1)
56 {
57 #ifdef SvPV_shrink_to_cur
58 SvPV_shrink_to_cur (sv);
59 #elif defined (SvPV_renew)
60 SvPV_renew (sv, SvCUR (sv) + 1);
61 #endif
62 }
63 }
64
65 // decode an utf-8 character and return it, or (UV)-1 in
66 // case of an error.
67 // we special-case "safe" characters from U+80 .. U+7FF,
68 // but use the very good perl function to parse anything else.
69 // note that we never call this function for a ascii codepoints
70 static UV
71 decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
72 {
73 if (s[0] > 0xdf || s[0] < 0xc2)
74 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
75 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf)
76 {
77 *clen = 2;
78 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
79 }
80 else
81 {
82 *clen = (STRLEN)-1;
83 return (UV)-1;
84 }
85 }
86
87 /////////////////////////////////////////////////////////////////////////////
88 // encoder
89
90 // structure used for encoding JSON
91 typedef struct
92 {
93 char *cur; // SvPVX (sv) + current output position
94 char *end; // SvEND (sv)
95 SV *sv; // result scalar
96 U32 flags; // F_*
97 U32 indent; // indentation level
98 U32 maxdepth; // max. indentation/recursion level
99 } enc_t;
100
101 static void
102 need (enc_t *enc, STRLEN len)
103 {
104 if (enc->cur + len >= enc->end)
105 {
106 STRLEN cur = enc->cur - SvPVX (enc->sv);
107 SvGROW (enc->sv, cur + len + 1);
108 enc->cur = SvPVX (enc->sv) + cur;
109 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv);
110 }
111 }
112
113 static void
114 encode_ch (enc_t *enc, char ch)
115 {
116 need (enc, 1);
117 *enc->cur++ = ch;
118 }
119
120 static void
121 encode_str (enc_t *enc, char *str, STRLEN len, int is_utf8)
122 {
123 char *end = str + len;
124
125 need (enc, len);
126
127 while (str < end)
128 {
129 unsigned char ch = *(unsigned char *)str;
130
131 if (ch >= 0x20 && ch < 0x80) // most common case
132 {
133 if (ch == '"') // but with slow exceptions
134 {
135 need (enc, len += 1);
136 *enc->cur++ = '\\';
137 *enc->cur++ = '"';
138 }
139 else if (ch == '\\')
140 {
141 need (enc, len += 1);
142 *enc->cur++ = '\\';
143 *enc->cur++ = '\\';
144 }
145 else
146 *enc->cur++ = ch;
147
148 ++str;
149 }
150 else
151 {
152 switch (ch)
153 {
154 case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break;
155 case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break;
156 case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break;
157 case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break;
158 case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break;
159
160 default:
161 {
162 STRLEN clen;
163 UV uch;
164
165 if (is_utf8)
166 {
167 //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
168 uch = decode_utf8 (str, end - str, &clen);
169 if (clen == (STRLEN)-1)
170 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str);
171 }
172 else
173 {
174 uch = ch;
175 clen = 1;
176 }
177
178 if (uch > 0x10FFFFUL)
179 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
180
181 if (uch < 0x80 || enc->flags & F_ASCII)
182 {
183 if (uch > 0xFFFFUL)
184 {
185 need (enc, len += 11);
186 sprintf (enc->cur, "\\u%04x\\u%04x",
187 (int)((uch - 0x10000) / 0x400 + 0xD800),
188 (int)((uch - 0x10000) % 0x400 + 0xDC00));
189 enc->cur += 12;
190 }
191 else
192 {
193 static char hexdigit [16] = "0123456789abcdef";
194 need (enc, len += 5);
195 *enc->cur++ = '\\';
196 *enc->cur++ = 'u';
197 *enc->cur++ = hexdigit [ uch >> 12 ];
198 *enc->cur++ = hexdigit [(uch >> 8) & 15];
199 *enc->cur++ = hexdigit [(uch >> 4) & 15];
200 *enc->cur++ = hexdigit [(uch >> 0) & 15];
201 }
202
203 str += clen;
204 }
205 else if (is_utf8)
206 {
207 need (enc, len += clen);
208 do
209 {
210 *enc->cur++ = *str++;
211 }
212 while (--clen);
213 }
214 else
215 {
216 need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed
217 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
218 ++str;
219 }
220 }
221 }
222 }
223
224 --len;
225 }
226 }
227
228 static void
229 encode_indent (enc_t *enc)
230 {
231 if (enc->flags & F_INDENT)
232 {
233 int spaces = enc->indent * INDENT_STEP;
234
235 need (enc, spaces);
236 memset (enc->cur, ' ', spaces);
237 enc->cur += spaces;
238 }
239 }
240
241 static void
242 encode_space (enc_t *enc)
243 {
244 need (enc, 1);
245 encode_ch (enc, ' ');
246 }
247
248 static void
249 encode_nl (enc_t *enc)
250 {
251 if (enc->flags & F_INDENT)
252 {
253 need (enc, 1);
254 encode_ch (enc, '\n');
255 }
256 }
257
258 static void
259 encode_comma (enc_t *enc)
260 {
261 encode_ch (enc, ',');
262
263 if (enc->flags & F_INDENT)
264 encode_nl (enc);
265 else if (enc->flags & F_SPACE_AFTER)
266 encode_space (enc);
267 }
268
269 static void encode_sv (enc_t *enc, SV *sv);
270
271 static void
272 encode_av (enc_t *enc, AV *av)
273 {
274 int i, len = av_len (av);
275
276 if (enc->indent >= enc->maxdepth)
277 croak ("data structure too deep (hit recursion limit)");
278
279 encode_ch (enc, '['); encode_nl (enc);
280 ++enc->indent;
281
282 for (i = 0; i <= len; ++i)
283 {
284 encode_indent (enc);
285 encode_sv (enc, *av_fetch (av, i, 0));
286
287 if (i < len)
288 encode_comma (enc);
289 }
290
291 encode_nl (enc);
292
293 --enc->indent;
294 encode_indent (enc); encode_ch (enc, ']');
295 }
296
297 static void
298 encode_he (enc_t *enc, HE *he)
299 {
300 encode_ch (enc, '"');
301
302 if (HeKLEN (he) == HEf_SVKEY)
303 {
304 SV *sv = HeSVKEY (he);
305 STRLEN len;
306 char *str;
307
308 SvGETMAGIC (sv);
309 str = SvPV (sv, len);
310
311 encode_str (enc, str, len, SvUTF8 (sv));
312 }
313 else
314 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he));
315
316 encode_ch (enc, '"');
317
318 if (enc->flags & F_SPACE_BEFORE) encode_space (enc);
319 encode_ch (enc, ':');
320 if (enc->flags & F_SPACE_AFTER ) encode_space (enc);
321 encode_sv (enc, HeVAL (he));
322 }
323
324 // compare hash entries, used when all keys are bytestrings
325 static int
326 he_cmp_fast (const void *a_, const void *b_)
327 {
328 int cmp;
329
330 HE *a = *(HE **)a_;
331 HE *b = *(HE **)b_;
332
333 STRLEN la = HeKLEN (a);
334 STRLEN lb = HeKLEN (b);
335
336 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb)))
337 cmp = la - lb;
338
339 return cmp;
340 }
341
342 // compare hash entries, used when some keys are sv's or utf-x
343 static int
344 he_cmp_slow (const void *a, const void *b)
345 {
346 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b));
347 }
348
349 static void
350 encode_hv (enc_t *enc, HV *hv)
351 {
352 int count, i;
353
354 if (enc->indent >= enc->maxdepth)
355 croak ("data structure too deep (hit recursion limit)");
356
357 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent;
358
359 if ((count = hv_iterinit (hv)))
360 {
361 // for canonical output we have to sort by keys first
362 // actually, this is mostly due to the stupid so-called
363 // security workaround added somewhere in 5.8.x.
364 // that randomises hash orderings
365 if (enc->flags & F_CANONICAL)
366 {
367 HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode
368 int fast = 1;
369
370 i = 0;
371 while ((he = hv_iternext (hv)))
372 {
373 hes [i++] = he;
374 if (HeKLEN (he) < 0 || HeKUTF8 (he))
375 fast = 0;
376 }
377
378 assert (i == count);
379
380 if (fast)
381 qsort (hes, count, sizeof (HE *), he_cmp_fast);
382 else
383 {
384 // hack to forcefully disable "use bytes"
385 COP cop = *PL_curcop;
386 cop.op_private = 0;
387
388 ENTER;
389 SAVETMPS;
390
391 SAVEVPTR (PL_curcop);
392 PL_curcop = &cop;
393
394 qsort (hes, count, sizeof (HE *), he_cmp_slow);
395
396 FREETMPS;
397 LEAVE;
398 }
399
400 for (i = 0; i < count; ++i)
401 {
402 encode_indent (enc);
403 encode_he (enc, hes [i]);
404
405 if (i < count - 1)
406 encode_comma (enc);
407 }
408
409 encode_nl (enc);
410 }
411 else
412 {
413 SV *sv;
414 HE *he = hv_iternext (hv);
415
416 for (;;)
417 {
418 encode_indent (enc);
419 encode_he (enc, he);
420
421 if (!(he = hv_iternext (hv)))
422 break;
423
424 encode_comma (enc);
425 }
426
427 encode_nl (enc);
428 }
429 }
430
431 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
432 }
433
434 // encode objects, arrays and special \0=false and \1=true values.
435 static void
436 encode_rv (enc_t *enc, SV *sv)
437 {
438 SvGETMAGIC (sv);
439
440 svtype svt = SvTYPE (sv);
441
442 if (svt == SVt_PVHV)
443 encode_hv (enc, (HV *)sv);
444 else if (svt == SVt_PVAV)
445 encode_av (enc, (AV *)sv);
446 else if (svt < SVt_PVAV)
447 {
448 if (SvNIOK (sv) && SvIV (sv) == 0)
449 encode_str (enc, "false", 5, 0);
450 else if (SvNIOK (sv) && SvIV (sv) == 1)
451 encode_str (enc, "true", 4, 0);
452 else
453 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
454 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
455 }
456 else
457 croak ("encountered %s, but JSON can only represent references to arrays or hashes",
458 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
459 }
460
461 static void
462 encode_sv (enc_t *enc, SV *sv)
463 {
464 SvGETMAGIC (sv);
465
466 if (SvPOKp (sv))
467 {
468 STRLEN len;
469 char *str = SvPV (sv, len);
470 encode_ch (enc, '"');
471 encode_str (enc, str, len, SvUTF8 (sv));
472 encode_ch (enc, '"');
473 }
474 else if (SvNOKp (sv))
475 {
476 need (enc, NV_DIG + 32);
477 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
478 enc->cur += strlen (enc->cur);
479 }
480 else if (SvIOKp (sv))
481 {
482 need (enc, 64);
483 enc->cur +=
484 SvIsUV(sv)
485 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv))
486 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv));
487 }
488 else if (SvROK (sv))
489 encode_rv (enc, SvRV (sv));
490 else if (!SvOK (sv))
491 encode_str (enc, "null", 4, 0);
492 else
493 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
494 SvPV_nolen (sv), SvFLAGS (sv));
495 }
496
497 static SV *
498 encode_json (SV *scalar, U32 flags)
499 {
500 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar))
501 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
502
503 enc_t enc;
504 enc.flags = flags;
505 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
506 enc.cur = SvPVX (enc.sv);
507 enc.end = SvEND (enc.sv);
508 enc.indent = 0;
509 enc.maxdepth = DEC_DEPTH (flags);
510
511 SvPOK_only (enc.sv);
512 encode_sv (&enc, scalar);
513
514 if (!(flags & (F_ASCII | F_UTF8)))
515 SvUTF8_on (enc.sv);
516
517 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
518
519 if (enc.flags & F_SHRINK)
520 shrink (enc.sv);
521
522 return enc.sv;
523 }
524
525 /////////////////////////////////////////////////////////////////////////////
526 // decoder
527
528 // structure used for decoding JSON
529 typedef struct
530 {
531 char *cur; // current parser pointer
532 char *end; // end of input string
533 const char *err; // parse error, if != 0
534 U32 flags; // F_*
535 U32 depth; // recursion depth
536 U32 maxdepth; // recursion depth limit
537 } dec_t;
538
539 static void
540 decode_ws (dec_t *dec)
541 {
542 for (;;)
543 {
544 char ch = *dec->cur;
545
546 if (ch > 0x20
547 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09))
548 break;
549
550 ++dec->cur;
551 }
552 }
553
554 #define ERR(reason) SB dec->err = reason; goto fail; SE
555
556 #define EXPECT_CH(ch) SB \
557 if (*dec->cur != ch) \
558 ERR (# ch " expected"); \
559 ++dec->cur; \
560 SE
561
562 #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)")
563 #define DEC_DEC_DEPTH --dec->depth
564
565 static SV *decode_sv (dec_t *dec);
566
567 static signed char decode_hexdigit[256];
568
569 static UV
570 decode_4hex (dec_t *dec)
571 {
572 signed char d1, d2, d3, d4;
573 unsigned char *cur = (unsigned char *)dec->cur;
574
575 d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected");
576 d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected");
577 d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected");
578 d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected");
579
580 dec->cur += 4;
581
582 return ((UV)d1) << 12
583 | ((UV)d2) << 8
584 | ((UV)d3) << 4
585 | ((UV)d4);
586
587 fail:
588 return (UV)-1;
589 }
590
591 static SV *
592 decode_str (dec_t *dec)
593 {
594 SV *sv = 0;
595 int utf8 = 0;
596
597 do
598 {
599 char buf [SHORT_STRING_LEN + UTF8_MAX_LEN];
600 char *cur = buf;
601
602 do
603 {
604 unsigned char ch = *(unsigned char *)dec->cur++;
605
606 if (ch == '"')
607 {
608 --dec->cur;
609 break;
610 }
611 else if (ch == '\\')
612 {
613 switch (*dec->cur)
614 {
615 case '\\':
616 case '/':
617 case '"': *cur++ = *dec->cur++; break;
618
619 case 'b': ++dec->cur; *cur++ = '\010'; break;
620 case 't': ++dec->cur; *cur++ = '\011'; break;
621 case 'n': ++dec->cur; *cur++ = '\012'; break;
622 case 'f': ++dec->cur; *cur++ = '\014'; break;
623 case 'r': ++dec->cur; *cur++ = '\015'; break;
624
625 case 'u':
626 {
627 UV lo, hi;
628 ++dec->cur;
629
630 hi = decode_4hex (dec);
631 if (hi == (UV)-1)
632 goto fail;
633
634 // possibly a surrogate pair
635 if (hi >= 0xd800)
636 if (hi < 0xdc00)
637 {
638 if (dec->cur [0] != '\\' || dec->cur [1] != 'u')
639 ERR ("missing low surrogate character in surrogate pair");
640
641 dec->cur += 2;
642
643 lo = decode_4hex (dec);
644 if (lo == (UV)-1)
645 goto fail;
646
647 if (lo < 0xdc00 || lo >= 0xe000)
648 ERR ("surrogate pair expected");
649
650 hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000;
651 }
652 else if (hi < 0xe000)
653 ERR ("missing high surrogate character in surrogate pair");
654
655 if (hi >= 0x80)
656 {
657 utf8 = 1;
658
659 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0);
660 }
661 else
662 *cur++ = hi;
663 }
664 break;
665
666 default:
667 --dec->cur;
668 ERR ("illegal backslash escape sequence in string");
669 }
670 }
671 else if (ch >= 0x20 && ch <= 0x7f)
672 *cur++ = ch;
673 else if (ch >= 0x80)
674 {
675 --dec->cur;
676
677 STRLEN clen;
678 UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen);
679 if (clen == (STRLEN)-1)
680 ERR ("malformed UTF-8 character in JSON string");
681
682 do
683 *cur++ = *dec->cur++;
684 while (--clen);
685
686 utf8 = 1;
687 }
688 else
689 {
690 --dec->cur;
691
692 if (!ch)
693 ERR ("unexpected end of string while parsing json string");
694 else
695 ERR ("invalid character encountered while parsing json string");
696 }
697 }
698 while (cur < buf + SHORT_STRING_LEN);
699
700 STRLEN len = cur - buf;
701
702 if (sv)
703 {
704 SvGROW (sv, SvCUR (sv) + len + 1);
705 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
706 SvCUR_set (sv, SvCUR (sv) + len);
707 }
708 else
709 sv = newSVpvn (buf, len);
710 }
711 while (*dec->cur != '"');
712
713 ++dec->cur;
714
715 if (sv)
716 {
717 SvPOK_only (sv);
718 *SvEND (sv) = 0;
719
720 if (utf8)
721 SvUTF8_on (sv);
722 }
723 else
724 sv = newSVpvn ("", 0);
725
726 return sv;
727
728 fail:
729 return 0;
730 }
731
732 static SV *
733 decode_num (dec_t *dec)
734 {
735 int is_nv = 0;
736 char *start = dec->cur;
737
738 // [minus]
739 if (*dec->cur == '-')
740 ++dec->cur;
741
742 if (*dec->cur == '0')
743 {
744 ++dec->cur;
745 if (*dec->cur >= '0' && *dec->cur <= '9')
746 ERR ("malformed number (leading zero must not be followed by another digit)");
747 }
748 else if (*dec->cur < '0' || *dec->cur > '9')
749 ERR ("malformed number (no digits after initial minus)");
750 else
751 do
752 {
753 ++dec->cur;
754 }
755 while (*dec->cur >= '0' && *dec->cur <= '9');
756
757 // [frac]
758 if (*dec->cur == '.')
759 {
760 ++dec->cur;
761
762 if (*dec->cur < '0' || *dec->cur > '9')
763 ERR ("malformed number (no digits after decimal point)");
764
765 do
766 {
767 ++dec->cur;
768 }
769 while (*dec->cur >= '0' && *dec->cur <= '9');
770
771 is_nv = 1;
772 }
773
774 // [exp]
775 if (*dec->cur == 'e' || *dec->cur == 'E')
776 {
777 ++dec->cur;
778
779 if (*dec->cur == '-' || *dec->cur == '+')
780 ++dec->cur;
781
782 if (*dec->cur < '0' || *dec->cur > '9')
783 ERR ("malformed number (no digits after exp sign)");
784
785 do
786 {
787 ++dec->cur;
788 }
789 while (*dec->cur >= '0' && *dec->cur <= '9');
790
791 is_nv = 1;
792 }
793
794 if (!is_nv)
795 {
796 UV uv;
797 int numtype = grok_number (start, dec->cur - start, &uv);
798 if (numtype & IS_NUMBER_IN_UV)
799 if (numtype & IS_NUMBER_NEG)
800 {
801 if (uv < (UV)IV_MIN)
802 return newSViv (-(IV)uv);
803 }
804 else
805 return newSVuv (uv);
806 }
807
808 return newSVnv (Atof (start));
809
810 fail:
811 return 0;
812 }
813
814 static SV *
815 decode_av (dec_t *dec)
816 {
817 AV *av = newAV ();
818
819 DEC_INC_DEPTH;
820 decode_ws (dec);
821
822 if (*dec->cur == ']')
823 ++dec->cur;
824 else
825 for (;;)
826 {
827 SV *value;
828
829 value = decode_sv (dec);
830 if (!value)
831 goto fail;
832
833 av_push (av, value);
834
835 decode_ws (dec);
836
837 if (*dec->cur == ']')
838 {
839 ++dec->cur;
840 break;
841 }
842
843 if (*dec->cur != ',')
844 ERR (", or ] expected while parsing array");
845
846 ++dec->cur;
847 }
848
849 DEC_DEC_DEPTH;
850 return newRV_noinc ((SV *)av);
851
852 fail:
853 SvREFCNT_dec (av);
854 DEC_DEC_DEPTH;
855 return 0;
856 }
857
858 static SV *
859 decode_hv (dec_t *dec)
860 {
861 HV *hv = newHV ();
862
863 DEC_INC_DEPTH;
864 decode_ws (dec);
865
866 if (*dec->cur == '}')
867 ++dec->cur;
868 else
869 for (;;)
870 {
871 SV *key, *value;
872
873 decode_ws (dec); EXPECT_CH ('"');
874
875 key = decode_str (dec);
876 if (!key)
877 goto fail;
878
879 decode_ws (dec); EXPECT_CH (':');
880
881 value = decode_sv (dec);
882 if (!value)
883 {
884 SvREFCNT_dec (key);
885 goto fail;
886 }
887
888 hv_store_ent (hv, key, value, 0);
889 SvREFCNT_dec (key);
890
891 decode_ws (dec);
892
893 if (*dec->cur == '}')
894 {
895 ++dec->cur;
896 break;
897 }
898
899 if (*dec->cur != ',')
900 ERR (", or } expected while parsing object/hash");
901
902 ++dec->cur;
903 }
904
905 DEC_DEC_DEPTH;
906 return newRV_noinc ((SV *)hv);
907
908 fail:
909 SvREFCNT_dec (hv);
910 DEC_DEC_DEPTH;
911 return 0;
912 }
913
914 static SV *
915 decode_sv (dec_t *dec)
916 {
917 decode_ws (dec);
918 switch (*dec->cur)
919 {
920 case '"': ++dec->cur; return decode_str (dec);
921 case '[': ++dec->cur; return decode_av (dec);
922 case '{': ++dec->cur; return decode_hv (dec);
923
924 case '-':
925 case '0': case '1': case '2': case '3': case '4':
926 case '5': case '6': case '7': case '8': case '9':
927 return decode_num (dec);
928
929 case 't':
930 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
931 {
932 dec->cur += 4;
933 return newSViv (1);
934 }
935 else
936 ERR ("'true' expected");
937
938 break;
939
940 case 'f':
941 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
942 {
943 dec->cur += 5;
944 return newSViv (0);
945 }
946 else
947 ERR ("'false' expected");
948
949 break;
950
951 case 'n':
952 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4))
953 {
954 dec->cur += 4;
955 return newSVsv (&PL_sv_undef);
956 }
957 else
958 ERR ("'null' expected");
959
960 break;
961
962 default:
963 ERR ("malformed json string, neither array, object, number, string or atom");
964 break;
965 }
966
967 fail:
968 return 0;
969 }
970
971 static SV *
972 decode_json (SV *string, U32 flags)
973 {
974 SV *sv;
975
976 SvUPGRADE (string, SVt_PV);
977
978 if (flags & F_UTF8)
979 sv_utf8_downgrade (string, 0);
980 else
981 sv_utf8_upgrade (string);
982
983 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
984
985 dec_t dec;
986 dec.flags = flags;
987 dec.cur = SvPVX (string);
988 dec.end = SvEND (string);
989 dec.err = 0;
990 dec.depth = 0;
991 dec.maxdepth = DEC_DEPTH (dec.flags);
992
993 *dec.end = 0; // this should basically be a nop, too, but make sure its there
994 sv = decode_sv (&dec);
995
996 if (!sv)
997 {
998 IV offset = dec.flags & F_UTF8
999 ? dec.cur - SvPVX (string)
1000 : utf8_distance (dec.cur, SvPVX (string));
1001 SV *uni = sv_newmortal ();
1002
1003 // horrible hack to silence warning inside pv_uni_display
1004 COP cop = *PL_curcop;
1005 cop.cop_warnings = pWARN_NONE;
1006 ENTER;
1007 SAVEVPTR (PL_curcop);
1008 PL_curcop = &cop;
1009 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1010 LEAVE;
1011
1012 croak ("%s, at character offset %d [\"%s\"]",
1013 dec.err,
1014 (int)offset,
1015 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1016 }
1017
1018 sv = sv_2mortal (sv);
1019
1020 if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv))
1021 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
1022
1023 return sv;
1024 }
1025
1026 /////////////////////////////////////////////////////////////////////////////
1027 // XS interface functions
1028
1029 MODULE = JSON::XS PACKAGE = JSON::XS
1030
1031 BOOT:
1032 {
1033 int i;
1034
1035 memset (decode_hexdigit, 0xff, 256);
1036
1037 for (i = 0; i < 256; ++i)
1038 decode_hexdigit [i] =
1039 i >= '0' && i <= '9' ? i - '0'
1040 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1041 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1042 : -1;
1043
1044 json_stash = gv_stashpv ("JSON::XS", 1);
1045 }
1046
1047 PROTOTYPES: DISABLE
1048
1049 SV *new (char *dummy)
1050 CODE:
1051 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash);
1052 OUTPUT:
1053 RETVAL
1054
1055 SV *ascii (SV *self, int enable = 1)
1056 ALIAS:
1057 ascii = F_ASCII
1058 utf8 = F_UTF8
1059 indent = F_INDENT
1060 canonical = F_CANONICAL
1061 space_before = F_SPACE_BEFORE
1062 space_after = F_SPACE_AFTER
1063 pretty = F_PRETTY
1064 allow_nonref = F_ALLOW_NONREF
1065 shrink = F_SHRINK
1066 CODE:
1067 {
1068 UV *uv = SvJSON (self);
1069 if (enable)
1070 *uv |= ix;
1071 else
1072 *uv &= ~ix;
1073
1074 RETVAL = newSVsv (self);
1075 }
1076 OUTPUT:
1077 RETVAL
1078
1079 SV *max_depth (SV *self, int max_depth = 0x80000000UL)
1080 CODE:
1081 {
1082 UV *uv = SvJSON (self);
1083 UV log2 = 0;
1084
1085 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL;
1086
1087 while ((1UL << log2) < max_depth)
1088 ++log2;
1089
1090 *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1091
1092 RETVAL = newSVsv (self);
1093 }
1094 OUTPUT:
1095 RETVAL
1096
1097 void encode (SV *self, SV *scalar)
1098 PPCODE:
1099 XPUSHs (encode_json (scalar, *SvJSON (self)));
1100
1101 void decode (SV *self, SV *jsonstr)
1102 PPCODE:
1103 XPUSHs (decode_json (jsonstr, *SvJSON (self)));
1104
1105 PROTOTYPES: ENABLE
1106
1107 void to_json (SV *scalar)
1108 ALIAS:
1109 objToJson = 0
1110 PPCODE:
1111 XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8));
1112
1113 void from_json (SV *jsonstr)
1114 ALIAS:
1115 jsonToObj = 0
1116 PPCODE:
1117 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8));
1118