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