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