ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
Revision: 1.20
Committed: Sun Mar 25 21:53:19 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-0_8
Changes since 1.19: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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