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