1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include <assert.h> |
6 | #include "string.h" |
6 | #include <string.h> |
7 | #include "stdlib.h" |
7 | #include <stdlib.h> |
8 | #include "stdio.h" |
8 | #include <stdio.h> |
|
|
9 | #include <limits.h> |
|
|
10 | #include <float.h> |
9 | |
11 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
14 | #endif |
13 | |
15 | |
14 | // some old perls do not have this, try to make it work, no |
16 | // some old perls do not have this, try to make it work, no |
15 | // guarentees, though. if it breaks, you get to keep the pieces. |
17 | // guarentees, though. if it breaks, you get to keep the pieces. |
16 | #ifndef UTF8_MAXBYTES |
18 | #ifndef UTF8_MAXBYTES |
17 | # define UTF8_MAXBYTES 13 |
19 | # define UTF8_MAXBYTES 13 |
18 | #endif |
20 | #endif |
|
|
21 | |
|
|
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
19 | |
23 | |
20 | #define F_ASCII 0x00000001UL |
24 | #define F_ASCII 0x00000001UL |
21 | #define F_LATIN1 0x00000002UL |
25 | #define F_LATIN1 0x00000002UL |
22 | #define F_UTF8 0x00000004UL |
26 | #define F_UTF8 0x00000004UL |
23 | #define F_INDENT 0x00000008UL |
27 | #define F_INDENT 0x00000008UL |
… | |
… | |
25 | #define F_SPACE_BEFORE 0x00000020UL |
29 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
30 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
31 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_SHRINK 0x00000200UL |
32 | #define F_SHRINK 0x00000200UL |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
33 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
34 | #define F_CONV_BLESSED 0x00000800UL |
|
|
35 | #define F_RELAXED 0x00001000UL |
|
|
36 | |
31 | #define F_MAXDEPTH 0xf8000000UL |
37 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define S_MAXDEPTH 27 |
38 | #define S_MAXDEPTH 27 |
33 | #define F_MAXSIZE 0x01f00000UL |
39 | #define F_MAXSIZE 0x01f00000UL |
34 | #define S_MAXSIZE 20 |
40 | #define S_MAXSIZE 20 |
|
|
41 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
35 | |
42 | |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
43 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
44 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
38 | |
45 | |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
46 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
… | |
… | |
46 | |
53 | |
47 | #define SB do { |
54 | #define SB do { |
48 | #define SE } while (0) |
55 | #define SE } while (0) |
49 | |
56 | |
50 | #if __GNUC__ >= 3 |
57 | #if __GNUC__ >= 3 |
51 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
58 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
52 | # define inline inline |
59 | # define INLINE static inline |
53 | #else |
60 | #else |
54 | # define expect(expr,value) (expr) |
61 | # define expect(expr,value) (expr) |
55 | # define inline static |
62 | # define INLINE static |
56 | #endif |
63 | #endif |
57 | |
64 | |
58 | #define expect_false(expr) expect ((expr) != 0, 0) |
65 | #define expect_false(expr) expect ((expr) != 0, 0) |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
66 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | |
67 | |
|
|
68 | #define IN_RANGE_INC(type,val,beg,end) \ |
|
|
69 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
|
|
70 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
|
|
71 | |
|
|
72 | #ifdef USE_ITHREADS |
|
|
73 | # define JSON_SLOW 1 |
|
|
74 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
75 | #else |
|
|
76 | # define JSON_SLOW 0 |
|
|
77 | # define JSON_STASH json_stash |
|
|
78 | #endif |
|
|
79 | |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
80 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
62 | static SV *json_true, *json_false; |
81 | static SV *json_true, *json_false; |
63 | |
82 | |
64 | typedef struct json { |
83 | typedef struct { |
65 | U32 flags; |
84 | U32 flags; |
|
|
85 | SV *cb_object; |
|
|
86 | HV *cb_sk_object; |
66 | } JSON__XS; |
87 | } JSON; |
67 | |
88 | |
68 | ///////////////////////////////////////////////////////////////////////////// |
89 | ///////////////////////////////////////////////////////////////////////////// |
69 | // utility functions |
90 | // utility functions |
70 | |
91 | |
71 | static UV * |
92 | INLINE void |
72 | SvJSON (SV *sv) |
|
|
73 | { |
|
|
74 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
75 | croak ("object is not of type JSON::XS"); |
|
|
76 | |
|
|
77 | return &SvUVX (SvRV (sv)); |
|
|
78 | } |
|
|
79 | |
|
|
80 | static void |
|
|
81 | shrink (SV *sv) |
93 | shrink (SV *sv) |
82 | { |
94 | { |
83 | sv_utf8_downgrade (sv, 1); |
95 | sv_utf8_downgrade (sv, 1); |
84 | if (SvLEN (sv) > SvCUR (sv) + 1) |
96 | if (SvLEN (sv) > SvCUR (sv) + 1) |
85 | { |
97 | { |
… | |
… | |
94 | // decode an utf-8 character and return it, or (UV)-1 in |
106 | // decode an utf-8 character and return it, or (UV)-1 in |
95 | // case of an error. |
107 | // case of an error. |
96 | // we special-case "safe" characters from U+80 .. U+7FF, |
108 | // we special-case "safe" characters from U+80 .. U+7FF, |
97 | // but use the very good perl function to parse anything else. |
109 | // but use the very good perl function to parse anything else. |
98 | // note that we never call this function for a ascii codepoints |
110 | // note that we never call this function for a ascii codepoints |
99 | inline UV |
111 | INLINE UV |
100 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
112 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
101 | { |
113 | { |
102 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
114 | if (expect_true (len >= 2 |
103 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
115 | && IN_RANGE_INC (char, s[0], 0xc2, 0xdf) |
104 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
116 | && IN_RANGE_INC (char, s[1], 0x80, 0xbf))) |
105 | { |
117 | { |
106 | *clen = 2; |
118 | *clen = 2; |
107 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
119 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
108 | } |
120 | } |
109 | else |
121 | else |
110 | { |
122 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
111 | *clen = (STRLEN)-1; |
123 | } |
112 | return (UV)-1; |
124 | |
113 | } |
125 | // likewise for encoding, also never called for ascii codepoints |
|
|
126 | // this function takes advantage of this fact, although current gccs |
|
|
127 | // seem to optimise the check for >= 0x80 away anyways |
|
|
128 | INLINE unsigned char * |
|
|
129 | encode_utf8 (unsigned char *s, UV ch) |
|
|
130 | { |
|
|
131 | if (expect_false (ch < 0x000080)) |
|
|
132 | *s++ = ch; |
|
|
133 | else if (expect_true (ch < 0x000800)) |
|
|
134 | *s++ = 0xc0 | ( ch >> 6), |
|
|
135 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
136 | else if ( ch < 0x010000) |
|
|
137 | *s++ = 0xe0 | ( ch >> 12), |
|
|
138 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
139 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
140 | else if ( ch < 0x110000) |
|
|
141 | *s++ = 0xf0 | ( ch >> 18), |
|
|
142 | *s++ = 0x80 | ((ch >> 12) & 0x3f), |
|
|
143 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
144 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
145 | |
|
|
146 | return s; |
114 | } |
147 | } |
115 | |
148 | |
116 | ///////////////////////////////////////////////////////////////////////////// |
149 | ///////////////////////////////////////////////////////////////////////////// |
117 | // encoder |
150 | // encoder |
118 | |
151 | |
… | |
… | |
120 | typedef struct |
153 | typedef struct |
121 | { |
154 | { |
122 | char *cur; // SvPVX (sv) + current output position |
155 | char *cur; // SvPVX (sv) + current output position |
123 | char *end; // SvEND (sv) |
156 | char *end; // SvEND (sv) |
124 | SV *sv; // result scalar |
157 | SV *sv; // result scalar |
125 | struct json json; |
158 | JSON json; |
126 | U32 indent; // indentation level |
159 | U32 indent; // indentation level |
127 | U32 maxdepth; // max. indentation/recursion level |
160 | U32 maxdepth; // max. indentation/recursion level |
|
|
161 | UV limit; // escape character values >= this value when encoding |
128 | } enc_t; |
162 | } enc_t; |
129 | |
163 | |
130 | inline void |
164 | INLINE void |
131 | need (enc_t *enc, STRLEN len) |
165 | need (enc_t *enc, STRLEN len) |
132 | { |
166 | { |
133 | if (expect_false (enc->cur + len >= enc->end)) |
167 | if (expect_false (enc->cur + len >= enc->end)) |
134 | { |
168 | { |
135 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
169 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
… | |
… | |
137 | enc->cur = SvPVX (enc->sv) + cur; |
171 | enc->cur = SvPVX (enc->sv) + cur; |
138 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
172 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
139 | } |
173 | } |
140 | } |
174 | } |
141 | |
175 | |
142 | inline void |
176 | INLINE void |
143 | encode_ch (enc_t *enc, char ch) |
177 | encode_ch (enc_t *enc, char ch) |
144 | { |
178 | { |
145 | need (enc, 1); |
179 | need (enc, 1); |
146 | *enc->cur++ = ch; |
180 | *enc->cur++ = ch; |
147 | } |
181 | } |
… | |
… | |
201 | { |
235 | { |
202 | uch = ch; |
236 | uch = ch; |
203 | clen = 1; |
237 | clen = 1; |
204 | } |
238 | } |
205 | |
239 | |
206 | if (uch > 0x10FFFFUL) |
240 | if (uch < 0x80/*0x20*/ || uch >= enc->limit) |
207 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
208 | |
|
|
209 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
|
|
210 | { |
241 | { |
211 | if (uch > 0xFFFFUL) |
242 | if (uch >= 0x10000UL) |
212 | { |
243 | { |
|
|
244 | if (uch >= 0x110000UL) |
|
|
245 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
246 | |
213 | need (enc, len += 11); |
247 | need (enc, len += 11); |
214 | sprintf (enc->cur, "\\u%04x\\u%04x", |
248 | sprintf (enc->cur, "\\u%04x\\u%04x", |
215 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
249 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
216 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
250 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
217 | enc->cur += 12; |
251 | enc->cur += 12; |
… | |
… | |
245 | while (--clen); |
279 | while (--clen); |
246 | } |
280 | } |
247 | else |
281 | else |
248 | { |
282 | { |
249 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
283 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
250 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
284 | enc->cur = encode_utf8 (enc->cur, uch); |
251 | ++str; |
285 | ++str; |
252 | } |
286 | } |
253 | } |
287 | } |
254 | } |
288 | } |
255 | } |
289 | } |
256 | |
290 | |
257 | --len; |
291 | --len; |
258 | } |
292 | } |
259 | } |
293 | } |
260 | |
294 | |
261 | inline void |
295 | INLINE void |
262 | encode_indent (enc_t *enc) |
296 | encode_indent (enc_t *enc) |
263 | { |
297 | { |
264 | if (enc->json.flags & F_INDENT) |
298 | if (enc->json.flags & F_INDENT) |
265 | { |
299 | { |
266 | int spaces = enc->indent * INDENT_STEP; |
300 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
269 | memset (enc->cur, ' ', spaces); |
303 | memset (enc->cur, ' ', spaces); |
270 | enc->cur += spaces; |
304 | enc->cur += spaces; |
271 | } |
305 | } |
272 | } |
306 | } |
273 | |
307 | |
274 | inline void |
308 | INLINE void |
275 | encode_space (enc_t *enc) |
309 | encode_space (enc_t *enc) |
276 | { |
310 | { |
277 | need (enc, 1); |
311 | need (enc, 1); |
278 | encode_ch (enc, ' '); |
312 | encode_ch (enc, ' '); |
279 | } |
313 | } |
280 | |
314 | |
281 | inline void |
315 | INLINE void |
282 | encode_nl (enc_t *enc) |
316 | encode_nl (enc_t *enc) |
283 | { |
317 | { |
284 | if (enc->json.flags & F_INDENT) |
318 | if (enc->json.flags & F_INDENT) |
285 | { |
319 | { |
286 | need (enc, 1); |
320 | need (enc, 1); |
287 | encode_ch (enc, '\n'); |
321 | encode_ch (enc, '\n'); |
288 | } |
322 | } |
289 | } |
323 | } |
290 | |
324 | |
291 | inline void |
325 | INLINE void |
292 | encode_comma (enc_t *enc) |
326 | encode_comma (enc_t *enc) |
293 | { |
327 | { |
294 | encode_ch (enc, ','); |
328 | encode_ch (enc, ','); |
295 | |
329 | |
296 | if (enc->json.flags & F_INDENT) |
330 | if (enc->json.flags & F_INDENT) |
… | |
… | |
307 | int i, len = av_len (av); |
341 | int i, len = av_len (av); |
308 | |
342 | |
309 | if (enc->indent >= enc->maxdepth) |
343 | if (enc->indent >= enc->maxdepth) |
310 | croak ("data structure too deep (hit recursion limit)"); |
344 | croak ("data structure too deep (hit recursion limit)"); |
311 | |
345 | |
312 | encode_ch (enc, '['); encode_nl (enc); |
346 | encode_ch (enc, '['); |
313 | ++enc->indent; |
347 | |
|
|
348 | if (len >= 0) |
|
|
349 | { |
|
|
350 | encode_nl (enc); ++enc->indent; |
314 | |
351 | |
315 | for (i = 0; i <= len; ++i) |
352 | for (i = 0; i <= len; ++i) |
316 | { |
353 | { |
|
|
354 | SV **svp = av_fetch (av, i, 0); |
|
|
355 | |
317 | encode_indent (enc); |
356 | encode_indent (enc); |
318 | encode_sv (enc, *av_fetch (av, i, 0)); |
|
|
319 | |
357 | |
|
|
358 | if (svp) |
|
|
359 | encode_sv (enc, *svp); |
|
|
360 | else |
|
|
361 | encode_str (enc, "null", 4, 0); |
|
|
362 | |
320 | if (i < len) |
363 | if (i < len) |
321 | encode_comma (enc); |
364 | encode_comma (enc); |
322 | } |
365 | } |
323 | |
366 | |
|
|
367 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
368 | } |
|
|
369 | |
324 | encode_nl (enc); |
370 | encode_ch (enc, ']'); |
325 | |
|
|
326 | --enc->indent; |
|
|
327 | encode_indent (enc); encode_ch (enc, ']'); |
|
|
328 | } |
371 | } |
329 | |
372 | |
330 | static void |
373 | static void |
331 | encode_he (enc_t *enc, HE *he) |
374 | encode_hk (enc_t *enc, HE *he) |
332 | { |
375 | { |
333 | encode_ch (enc, '"'); |
376 | encode_ch (enc, '"'); |
334 | |
377 | |
335 | if (HeKLEN (he) == HEf_SVKEY) |
378 | if (HeKLEN (he) == HEf_SVKEY) |
336 | { |
379 | { |
… | |
… | |
349 | encode_ch (enc, '"'); |
392 | encode_ch (enc, '"'); |
350 | |
393 | |
351 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
394 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
352 | encode_ch (enc, ':'); |
395 | encode_ch (enc, ':'); |
353 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
396 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
354 | encode_sv (enc, HeVAL (he)); |
|
|
355 | } |
397 | } |
356 | |
398 | |
357 | // compare hash entries, used when all keys are bytestrings |
399 | // compare hash entries, used when all keys are bytestrings |
358 | static int |
400 | static int |
359 | he_cmp_fast (const void *a_, const void *b_) |
401 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
364 | HE *b = *(HE **)b_; |
406 | HE *b = *(HE **)b_; |
365 | |
407 | |
366 | STRLEN la = HeKLEN (a); |
408 | STRLEN la = HeKLEN (a); |
367 | STRLEN lb = HeKLEN (b); |
409 | STRLEN lb = HeKLEN (b); |
368 | |
410 | |
369 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
411 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
370 | cmp = la - lb; |
412 | cmp = lb - la; |
371 | |
413 | |
372 | return cmp; |
414 | return cmp; |
373 | } |
415 | } |
374 | |
416 | |
375 | // compare hash entries, used when some keys are sv's or utf-x |
417 | // compare hash entries, used when some keys are sv's or utf-x |
376 | static int |
418 | static int |
377 | he_cmp_slow (const void *a, const void *b) |
419 | he_cmp_slow (const void *a, const void *b) |
378 | { |
420 | { |
379 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
421 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
380 | } |
422 | } |
381 | |
423 | |
382 | static void |
424 | static void |
383 | encode_hv (enc_t *enc, HV *hv) |
425 | encode_hv (enc_t *enc, HV *hv) |
384 | { |
426 | { |
|
|
427 | HE *he; |
385 | int count, i; |
428 | int count; |
386 | |
429 | |
387 | if (enc->indent >= enc->maxdepth) |
430 | if (enc->indent >= enc->maxdepth) |
388 | croak ("data structure too deep (hit recursion limit)"); |
431 | croak ("data structure too deep (hit recursion limit)"); |
389 | |
432 | |
390 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
433 | encode_ch (enc, '{'); |
391 | |
434 | |
392 | if ((count = hv_iterinit (hv))) |
|
|
393 | { |
|
|
394 | // for canonical output we have to sort by keys first |
435 | // for canonical output we have to sort by keys first |
395 | // actually, this is mostly due to the stupid so-called |
436 | // actually, this is mostly due to the stupid so-called |
396 | // security workaround added somewhere in 5.8.x. |
437 | // security workaround added somewhere in 5.8.x. |
397 | // that randomises hash orderings |
438 | // that randomises hash orderings |
398 | if (enc->json.flags & F_CANONICAL) |
439 | if (enc->json.flags & F_CANONICAL) |
|
|
440 | { |
|
|
441 | int count = hv_iterinit (hv); |
|
|
442 | |
|
|
443 | if (SvMAGICAL (hv)) |
399 | { |
444 | { |
|
|
445 | // need to count by iterating. could improve by dynamically building the vector below |
|
|
446 | // but I don't care for the speed of this special case. |
|
|
447 | // note also that we will run into undefined behaviour when the two iterations |
|
|
448 | // do not result in the same count, something I might care for in some later release. |
|
|
449 | |
|
|
450 | count = 0; |
|
|
451 | while (hv_iternext (hv)) |
|
|
452 | ++count; |
|
|
453 | |
|
|
454 | hv_iterinit (hv); |
|
|
455 | } |
|
|
456 | |
|
|
457 | if (count) |
|
|
458 | { |
400 | int fast = 1; |
459 | int i, fast = 1; |
401 | HE *he; |
|
|
402 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
460 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
403 | HE **hes = _alloca (count * sizeof (HE)); |
461 | HE **hes = _alloca (count * sizeof (HE)); |
404 | #else |
462 | #else |
405 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
463 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
406 | #endif |
464 | #endif |
… | |
… | |
433 | |
491 | |
434 | FREETMPS; |
492 | FREETMPS; |
435 | LEAVE; |
493 | LEAVE; |
436 | } |
494 | } |
437 | |
495 | |
438 | for (i = 0; i < count; ++i) |
496 | encode_nl (enc); ++enc->indent; |
|
|
497 | |
|
|
498 | while (count--) |
439 | { |
499 | { |
440 | encode_indent (enc); |
500 | encode_indent (enc); |
|
|
501 | he = hes [count]; |
441 | encode_he (enc, hes [i]); |
502 | encode_hk (enc, he); |
|
|
503 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
442 | |
504 | |
443 | if (i < count - 1) |
505 | if (count) |
444 | encode_comma (enc); |
506 | encode_comma (enc); |
445 | } |
507 | } |
446 | |
508 | |
447 | encode_nl (enc); |
509 | encode_nl (enc); --enc->indent; encode_indent (enc); |
448 | } |
510 | } |
|
|
511 | } |
449 | else |
512 | else |
|
|
513 | { |
|
|
514 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
|
|
515 | if ((he = hv_iternext (hv))) |
450 | { |
516 | { |
451 | HE *he = hv_iternext (hv); |
517 | encode_nl (enc); ++enc->indent; |
452 | |
518 | |
453 | for (;;) |
519 | for (;;) |
454 | { |
520 | { |
455 | encode_indent (enc); |
521 | encode_indent (enc); |
456 | encode_he (enc, he); |
522 | encode_hk (enc, he); |
|
|
523 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
457 | |
524 | |
458 | if (!(he = hv_iternext (hv))) |
525 | if (!(he = hv_iternext (hv))) |
459 | break; |
526 | break; |
460 | |
527 | |
461 | encode_comma (enc); |
528 | encode_comma (enc); |
462 | } |
529 | } |
463 | |
530 | |
464 | encode_nl (enc); |
531 | encode_nl (enc); --enc->indent; encode_indent (enc); |
465 | } |
532 | } |
466 | } |
533 | } |
467 | |
534 | |
468 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
535 | encode_ch (enc, '}'); |
469 | } |
536 | } |
470 | |
537 | |
471 | // encode objects, arrays and special \0=false and \1=true values. |
538 | // encode objects, arrays and special \0=false and \1=true values. |
472 | static void |
539 | static void |
473 | encode_rv (enc_t *enc, SV *sv) |
540 | encode_rv (enc_t *enc, SV *sv) |
… | |
… | |
477 | SvGETMAGIC (sv); |
544 | SvGETMAGIC (sv); |
478 | svt = SvTYPE (sv); |
545 | svt = SvTYPE (sv); |
479 | |
546 | |
480 | if (expect_false (SvOBJECT (sv))) |
547 | if (expect_false (SvOBJECT (sv))) |
481 | { |
548 | { |
|
|
549 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
550 | ? json_boolean_stash |
|
|
551 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
552 | |
482 | if (SvSTASH (sv) == json_boolean_stash) |
553 | if (SvSTASH (sv) == stash) |
483 | { |
554 | { |
484 | if (SvIV (sv) == 0) |
555 | if (SvIV (sv)) |
|
|
556 | encode_str (enc, "true", 4, 0); |
|
|
557 | else |
485 | encode_str (enc, "false", 5, 0); |
558 | encode_str (enc, "false", 5, 0); |
486 | else |
|
|
487 | encode_str (enc, "true", 4, 0); |
|
|
488 | } |
559 | } |
489 | else |
560 | else |
490 | { |
561 | { |
491 | #if 0 |
562 | #if 0 |
492 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
563 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
… | |
… | |
495 | } |
566 | } |
496 | #endif |
567 | #endif |
497 | if (enc->json.flags & F_CONV_BLESSED) |
568 | if (enc->json.flags & F_CONV_BLESSED) |
498 | { |
569 | { |
499 | // we re-bless the reference to get overload and other niceties right |
570 | // we re-bless the reference to get overload and other niceties right |
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
571 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
501 | |
572 | |
502 | if (to_json) |
573 | if (to_json) |
503 | { |
574 | { |
504 | dSP; |
575 | dSP; |
505 | ENTER; |
576 | |
506 | SAVETMPS; |
|
|
507 | PUSHMARK (SP); |
577 | ENTER; SAVETMPS; PUSHMARK (SP); |
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
578 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
509 | |
579 | |
510 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
580 | // calling with G_SCALAR ensures that we always get a 1 return value |
511 | // check anyways. |
|
|
512 | PUTBACK; |
581 | PUTBACK; |
513 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
582 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
514 | SPAGAIN; |
583 | SPAGAIN; |
515 | |
584 | |
|
|
585 | // catch this surprisingly common error |
|
|
586 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
587 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
588 | |
|
|
589 | sv = POPs; |
|
|
590 | PUTBACK; |
|
|
591 | |
516 | encode_sv (enc, POPs); |
592 | encode_sv (enc, sv); |
517 | |
593 | |
518 | FREETMPS; |
594 | FREETMPS; LEAVE; |
519 | LEAVE; |
|
|
520 | } |
595 | } |
521 | else if (enc->json.flags & F_ALLOW_BLESSED) |
596 | else if (enc->json.flags & F_ALLOW_BLESSED) |
522 | encode_str (enc, "null", 4, 0); |
597 | encode_str (enc, "null", 4, 0); |
523 | else |
598 | else |
524 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
599 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
… | |
… | |
535 | encode_hv (enc, (HV *)sv); |
610 | encode_hv (enc, (HV *)sv); |
536 | else if (svt == SVt_PVAV) |
611 | else if (svt == SVt_PVAV) |
537 | encode_av (enc, (AV *)sv); |
612 | encode_av (enc, (AV *)sv); |
538 | else if (svt < SVt_PVAV) |
613 | else if (svt < SVt_PVAV) |
539 | { |
614 | { |
540 | if (SvNIOK (sv) && SvIV (sv) == 0) |
615 | STRLEN len = 0; |
|
|
616 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
617 | |
|
|
618 | if (len == 1 && *pv == '1') |
|
|
619 | encode_str (enc, "true", 4, 0); |
|
|
620 | else if (len == 1 && *pv == '0') |
541 | encode_str (enc, "false", 5, 0); |
621 | encode_str (enc, "false", 5, 0); |
542 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
543 | encode_str (enc, "true", 4, 0); |
|
|
544 | else |
622 | else |
545 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
623 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
546 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
624 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
547 | } |
625 | } |
548 | else |
626 | else |
… | |
… | |
570 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
648 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
571 | enc->cur += strlen (enc->cur); |
649 | enc->cur += strlen (enc->cur); |
572 | } |
650 | } |
573 | else if (SvIOKp (sv)) |
651 | else if (SvIOKp (sv)) |
574 | { |
652 | { |
575 | // we assume we can always read an IV as a UV |
653 | // we assume we can always read an IV as a UV and vice versa |
576 | if (SvUV (sv) & ~(UV)0x7fff) |
654 | // we assume two's complement |
577 | { |
655 | // we assume no aliasing issues in the union |
578 | // large integer, use the (rather slow) snprintf way. |
656 | if (SvIsUV (sv) ? SvUVX (sv) <= 59000 |
579 | need (enc, sizeof (UV) * 3); |
657 | : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000) |
580 | enc->cur += |
|
|
581 | SvIsUV(sv) |
|
|
582 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
|
|
583 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
584 | } |
|
|
585 | else |
|
|
586 | { |
658 | { |
587 | // optimise the "small number case" |
659 | // optimise the "small number case" |
588 | // code will likely be branchless and use only a single multiplication |
660 | // code will likely be branchless and use only a single multiplication |
|
|
661 | // works for numbers up to 59074 |
589 | I32 i = SvIV (sv); |
662 | I32 i = SvIVX (sv); |
590 | U32 u; |
663 | U32 u; |
591 | char digit, nz = 0; |
664 | char digit, nz = 0; |
592 | |
665 | |
593 | need (enc, 6); |
666 | need (enc, 6); |
594 | |
667 | |
… | |
… | |
600 | |
673 | |
601 | // now output digit by digit, each time masking out the integer part |
674 | // now output digit by digit, each time masking out the integer part |
602 | // and multiplying by 5 while moving the decimal point one to the right, |
675 | // and multiplying by 5 while moving the decimal point one to the right, |
603 | // resulting in a net multiplication by 10. |
676 | // resulting in a net multiplication by 10. |
604 | // we always write the digit to memory but conditionally increment |
677 | // we always write the digit to memory but conditionally increment |
605 | // the pointer, to ease the usage of conditional move instructions. |
678 | // the pointer, to enable the use of conditional move instructions. |
606 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
679 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; |
607 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
680 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; |
608 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
681 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; |
609 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
682 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; |
610 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
683 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
684 | } |
|
|
685 | else |
|
|
686 | { |
|
|
687 | // large integer, use the (rather slow) snprintf way. |
|
|
688 | need (enc, IVUV_MAXCHARS); |
|
|
689 | enc->cur += |
|
|
690 | SvIsUV(sv) |
|
|
691 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
|
|
692 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
611 | } |
693 | } |
612 | } |
694 | } |
613 | else if (SvROK (sv)) |
695 | else if (SvROK (sv)) |
614 | encode_rv (enc, SvRV (sv)); |
696 | encode_rv (enc, SvRV (sv)); |
615 | else if (!SvOK (sv)) |
697 | else if (!SvOK (sv)) |
… | |
… | |
618 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
700 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
619 | SvPV_nolen (sv), SvFLAGS (sv)); |
701 | SvPV_nolen (sv), SvFLAGS (sv)); |
620 | } |
702 | } |
621 | |
703 | |
622 | static SV * |
704 | static SV * |
623 | encode_json (SV *scalar, struct json *json) |
705 | encode_json (SV *scalar, JSON *json) |
624 | { |
706 | { |
625 | enc_t enc; |
707 | enc_t enc; |
626 | |
708 | |
627 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
709 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
628 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
710 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
… | |
… | |
631 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
713 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
632 | enc.cur = SvPVX (enc.sv); |
714 | enc.cur = SvPVX (enc.sv); |
633 | enc.end = SvEND (enc.sv); |
715 | enc.end = SvEND (enc.sv); |
634 | enc.indent = 0; |
716 | enc.indent = 0; |
635 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
717 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
|
|
718 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
|
|
719 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
|
|
720 | : 0x110000UL; |
636 | |
721 | |
637 | SvPOK_only (enc.sv); |
722 | SvPOK_only (enc.sv); |
638 | encode_sv (&enc, scalar); |
723 | encode_sv (&enc, scalar); |
639 | |
724 | |
640 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
725 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
… | |
… | |
656 | typedef struct |
741 | typedef struct |
657 | { |
742 | { |
658 | char *cur; // current parser pointer |
743 | char *cur; // current parser pointer |
659 | char *end; // end of input string |
744 | char *end; // end of input string |
660 | const char *err; // parse error, if != 0 |
745 | const char *err; // parse error, if != 0 |
661 | struct json json; |
746 | JSON json; |
662 | U32 depth; // recursion depth |
747 | U32 depth; // recursion depth |
663 | U32 maxdepth; // recursion depth limit |
748 | U32 maxdepth; // recursion depth limit |
664 | } dec_t; |
749 | } dec_t; |
665 | |
750 | |
666 | inline void |
751 | INLINE void |
|
|
752 | decode_comment (dec_t *dec) |
|
|
753 | { |
|
|
754 | // only '#'-style comments allowed a.t.m. |
|
|
755 | |
|
|
756 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
|
|
757 | ++dec->cur; |
|
|
758 | } |
|
|
759 | |
|
|
760 | INLINE void |
667 | decode_ws (dec_t *dec) |
761 | decode_ws (dec_t *dec) |
668 | { |
762 | { |
669 | for (;;) |
763 | for (;;) |
670 | { |
764 | { |
671 | char ch = *dec->cur; |
765 | char ch = *dec->cur; |
672 | |
766 | |
673 | if (ch > 0x20 |
767 | if (ch > 0x20) |
|
|
768 | { |
|
|
769 | if (expect_false (ch == '#')) |
|
|
770 | { |
|
|
771 | if (dec->json.flags & F_RELAXED) |
|
|
772 | decode_comment (dec); |
|
|
773 | else |
|
|
774 | break; |
|
|
775 | } |
|
|
776 | else |
|
|
777 | break; |
|
|
778 | } |
674 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
779 | else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
675 | break; |
780 | break; // parse error, but let higher level handle it, gives better error messages |
676 | |
781 | |
677 | ++dec->cur; |
782 | ++dec->cur; |
678 | } |
783 | } |
679 | } |
784 | } |
680 | |
785 | |
… | |
… | |
786 | |
891 | |
787 | if (hi >= 0x80) |
892 | if (hi >= 0x80) |
788 | { |
893 | { |
789 | utf8 = 1; |
894 | utf8 = 1; |
790 | |
895 | |
791 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
896 | cur = encode_utf8 (cur, hi); |
792 | } |
897 | } |
793 | else |
898 | else |
794 | *cur++ = hi; |
899 | *cur++ = hi; |
795 | } |
900 | } |
796 | break; |
901 | break; |
… | |
… | |
798 | default: |
903 | default: |
799 | --dec_cur; |
904 | --dec_cur; |
800 | ERR ("illegal backslash escape sequence in string"); |
905 | ERR ("illegal backslash escape sequence in string"); |
801 | } |
906 | } |
802 | } |
907 | } |
803 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
908 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
804 | *cur++ = ch; |
909 | *cur++ = ch; |
805 | else if (ch >= 0x80) |
910 | else if (ch >= 0x80) |
806 | { |
911 | { |
807 | STRLEN clen; |
912 | STRLEN clen; |
808 | UV uch; |
913 | UV uch; |
… | |
… | |
929 | is_nv = 1; |
1034 | is_nv = 1; |
930 | } |
1035 | } |
931 | |
1036 | |
932 | if (!is_nv) |
1037 | if (!is_nv) |
933 | { |
1038 | { |
|
|
1039 | int len = dec->cur - start; |
|
|
1040 | |
934 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
1041 | // special case the rather common 1..5-digit-int case |
935 | if (*start == '-') |
1042 | if (*start == '-') |
936 | switch (dec->cur - start) |
1043 | switch (len) |
937 | { |
1044 | { |
938 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1045 | case 2: return newSViv (-( start [1] - '0' * 1)); |
939 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1046 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
940 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1047 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
941 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1048 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1049 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
942 | } |
1050 | } |
943 | else |
1051 | else |
944 | switch (dec->cur - start) |
1052 | switch (len) |
945 | { |
1053 | { |
946 | case 1: return newSViv ( start [0] - '0' * 1); |
1054 | case 1: return newSViv ( start [0] - '0' * 1); |
947 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1055 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
948 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1056 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
949 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1057 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1058 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
950 | } |
1059 | } |
951 | |
1060 | |
952 | { |
1061 | { |
953 | UV uv; |
1062 | UV uv; |
954 | int numtype = grok_number (start, dec->cur - start, &uv); |
1063 | int numtype = grok_number (start, len, &uv); |
955 | if (numtype & IS_NUMBER_IN_UV) |
1064 | if (numtype & IS_NUMBER_IN_UV) |
956 | if (numtype & IS_NUMBER_NEG) |
1065 | if (numtype & IS_NUMBER_NEG) |
957 | { |
1066 | { |
958 | if (uv < (UV)IV_MIN) |
1067 | if (uv < (UV)IV_MIN) |
959 | return newSViv (-(IV)uv); |
1068 | return newSViv (-(IV)uv); |
960 | } |
1069 | } |
961 | else |
1070 | else |
962 | return newSVuv (uv); |
1071 | return newSVuv (uv); |
963 | |
|
|
964 | // here would likely be the place for bigint support |
|
|
965 | } |
1072 | } |
966 | } |
|
|
967 | |
1073 | |
968 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
1074 | len -= *start == '-' ? 1 : 0; |
|
|
1075 | |
|
|
1076 | // does not fit into IV or UV, try NV |
|
|
1077 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
1078 | #if defined (LDBL_DIG) |
|
|
1079 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1080 | #endif |
|
|
1081 | ) |
|
|
1082 | // fits into NV without loss of precision |
|
|
1083 | return newSVnv (Atof (start)); |
|
|
1084 | |
|
|
1085 | // everything else fails, convert it to a string |
|
|
1086 | return newSVpvn (start, dec->cur - start); |
|
|
1087 | } |
|
|
1088 | |
|
|
1089 | // loss of precision here |
969 | return newSVnv (Atof (start)); |
1090 | return newSVnv (Atof (start)); |
970 | |
1091 | |
971 | fail: |
1092 | fail: |
972 | return 0; |
1093 | return 0; |
973 | } |
1094 | } |
… | |
… | |
1003 | |
1124 | |
1004 | if (*dec->cur != ',') |
1125 | if (*dec->cur != ',') |
1005 | ERR (", or ] expected while parsing array"); |
1126 | ERR (", or ] expected while parsing array"); |
1006 | |
1127 | |
1007 | ++dec->cur; |
1128 | ++dec->cur; |
|
|
1129 | |
|
|
1130 | decode_ws (dec); |
|
|
1131 | |
|
|
1132 | if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
|
|
1133 | { |
|
|
1134 | ++dec->cur; |
|
|
1135 | break; |
|
|
1136 | } |
1008 | } |
1137 | } |
1009 | |
1138 | |
1010 | DEC_DEC_DEPTH; |
1139 | DEC_DEC_DEPTH; |
1011 | return newRV_noinc ((SV *)av); |
1140 | return newRV_noinc ((SV *)av); |
1012 | |
1141 | |
… | |
… | |
1017 | } |
1146 | } |
1018 | |
1147 | |
1019 | static SV * |
1148 | static SV * |
1020 | decode_hv (dec_t *dec) |
1149 | decode_hv (dec_t *dec) |
1021 | { |
1150 | { |
|
|
1151 | SV *sv; |
1022 | HV *hv = newHV (); |
1152 | HV *hv = newHV (); |
1023 | |
1153 | |
1024 | DEC_INC_DEPTH; |
1154 | DEC_INC_DEPTH; |
1025 | decode_ws (dec); |
1155 | decode_ws (dec); |
1026 | |
1156 | |
1027 | if (*dec->cur == '}') |
1157 | if (*dec->cur == '}') |
1028 | ++dec->cur; |
1158 | ++dec->cur; |
1029 | else |
1159 | else |
1030 | for (;;) |
1160 | for (;;) |
1031 | { |
1161 | { |
1032 | decode_ws (dec); EXPECT_CH ('"'); |
1162 | EXPECT_CH ('"'); |
1033 | |
1163 | |
1034 | // heuristic: assume that |
1164 | // heuristic: assume that |
1035 | // a) decode_str + hv_store_ent are abysmally slow. |
1165 | // a) decode_str + hv_store_ent are abysmally slow. |
1036 | // b) most hash keys are short, simple ascii text. |
1166 | // b) most hash keys are short, simple ascii text. |
1037 | // => try to "fast-match" such strings to avoid |
1167 | // => try to "fast-match" such strings to avoid |
… | |
… | |
1051 | if (!key) |
1181 | if (!key) |
1052 | goto fail; |
1182 | goto fail; |
1053 | |
1183 | |
1054 | decode_ws (dec); EXPECT_CH (':'); |
1184 | decode_ws (dec); EXPECT_CH (':'); |
1055 | |
1185 | |
|
|
1186 | decode_ws (dec); |
1056 | value = decode_sv (dec); |
1187 | value = decode_sv (dec); |
1057 | if (!value) |
1188 | if (!value) |
1058 | { |
1189 | { |
1059 | SvREFCNT_dec (key); |
1190 | SvREFCNT_dec (key); |
1060 | goto fail; |
1191 | goto fail; |
… | |
… | |
1072 | int len = p - key; |
1203 | int len = p - key; |
1073 | dec->cur = p + 1; |
1204 | dec->cur = p + 1; |
1074 | |
1205 | |
1075 | decode_ws (dec); EXPECT_CH (':'); |
1206 | decode_ws (dec); EXPECT_CH (':'); |
1076 | |
1207 | |
|
|
1208 | decode_ws (dec); |
1077 | value = decode_sv (dec); |
1209 | value = decode_sv (dec); |
1078 | if (!value) |
1210 | if (!value) |
1079 | goto fail; |
1211 | goto fail; |
1080 | |
1212 | |
1081 | hv_store (hv, key, len, value, 0); |
1213 | hv_store (hv, key, len, value, 0); |
… | |
… | |
1097 | |
1229 | |
1098 | if (*dec->cur != ',') |
1230 | if (*dec->cur != ',') |
1099 | ERR (", or } expected while parsing object/hash"); |
1231 | ERR (", or } expected while parsing object/hash"); |
1100 | |
1232 | |
1101 | ++dec->cur; |
1233 | ++dec->cur; |
|
|
1234 | |
|
|
1235 | decode_ws (dec); |
|
|
1236 | |
|
|
1237 | if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
|
|
1238 | { |
|
|
1239 | ++dec->cur; |
|
|
1240 | break; |
|
|
1241 | } |
1102 | } |
1242 | } |
1103 | |
1243 | |
1104 | DEC_DEC_DEPTH; |
1244 | DEC_DEC_DEPTH; |
1105 | return newRV_noinc ((SV *)hv); |
1245 | sv = newRV_noinc ((SV *)hv); |
|
|
1246 | |
|
|
1247 | // check filter callbacks |
|
|
1248 | if (dec->json.flags & F_HOOK) |
|
|
1249 | { |
|
|
1250 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1251 | { |
|
|
1252 | HE *cb, *he; |
|
|
1253 | |
|
|
1254 | hv_iterinit (hv); |
|
|
1255 | he = hv_iternext (hv); |
|
|
1256 | hv_iterinit (hv); |
|
|
1257 | |
|
|
1258 | // the next line creates a mortal sv each time its called. |
|
|
1259 | // might want to optimise this for common cases. |
|
|
1260 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1261 | |
|
|
1262 | if (cb) |
|
|
1263 | { |
|
|
1264 | dSP; |
|
|
1265 | int count; |
|
|
1266 | |
|
|
1267 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1268 | XPUSHs (HeVAL (he)); |
|
|
1269 | |
|
|
1270 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1271 | |
|
|
1272 | if (count == 1) |
|
|
1273 | { |
|
|
1274 | sv = newSVsv (POPs); |
|
|
1275 | FREETMPS; LEAVE; |
|
|
1276 | return sv; |
|
|
1277 | } |
|
|
1278 | |
|
|
1279 | FREETMPS; LEAVE; |
|
|
1280 | } |
|
|
1281 | } |
|
|
1282 | |
|
|
1283 | if (dec->json.cb_object) |
|
|
1284 | { |
|
|
1285 | dSP; |
|
|
1286 | int count; |
|
|
1287 | |
|
|
1288 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1289 | XPUSHs (sv_2mortal (sv)); |
|
|
1290 | |
|
|
1291 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1292 | |
|
|
1293 | if (count == 1) |
|
|
1294 | { |
|
|
1295 | sv = newSVsv (POPs); |
|
|
1296 | FREETMPS; LEAVE; |
|
|
1297 | return sv; |
|
|
1298 | } |
|
|
1299 | |
|
|
1300 | SvREFCNT_inc (sv); |
|
|
1301 | FREETMPS; LEAVE; |
|
|
1302 | } |
|
|
1303 | } |
|
|
1304 | |
|
|
1305 | return sv; |
1106 | |
1306 | |
1107 | fail: |
1307 | fail: |
1108 | SvREFCNT_dec (hv); |
1308 | SvREFCNT_dec (hv); |
1109 | DEC_DEC_DEPTH; |
1309 | DEC_DEC_DEPTH; |
1110 | return 0; |
1310 | return 0; |
1111 | } |
1311 | } |
1112 | |
1312 | |
1113 | static SV * |
1313 | static SV * |
1114 | decode_sv (dec_t *dec) |
1314 | decode_sv (dec_t *dec) |
1115 | { |
1315 | { |
1116 | decode_ws (dec); |
|
|
1117 | |
|
|
1118 | // the beauty of JSON: you need exactly one character lookahead |
1316 | // the beauty of JSON: you need exactly one character lookahead |
1119 | // to parse anything. |
1317 | // to parse everything. |
1120 | switch (*dec->cur) |
1318 | switch (*dec->cur) |
1121 | { |
1319 | { |
1122 | case '"': ++dec->cur; return decode_str (dec); |
1320 | case '"': ++dec->cur; return decode_str (dec); |
1123 | case '[': ++dec->cur; return decode_av (dec); |
1321 | case '[': ++dec->cur; return decode_av (dec); |
1124 | case '{': ++dec->cur; return decode_hv (dec); |
1322 | case '{': ++dec->cur; return decode_hv (dec); |
1125 | |
1323 | |
1126 | case '-': |
1324 | case '-': |
1127 | case '0': case '1': case '2': case '3': case '4': |
1325 | case '0': case '1': case '2': case '3': case '4': |
1128 | case '5': case '6': case '7': case '8': case '9': |
1326 | case '5': case '6': case '7': case '8': case '9': |
1129 | return decode_num (dec); |
1327 | return decode_num (dec); |
1130 | |
1328 | |
1131 | case 't': |
1329 | case 't': |
1132 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1330 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1133 | { |
1331 | { |
1134 | dec->cur += 4; |
1332 | dec->cur += 4; |
|
|
1333 | #if JSON_SLOW |
|
|
1334 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
|
|
1335 | #endif |
1135 | return SvREFCNT_inc (json_true); |
1336 | return SvREFCNT_inc (json_true); |
1136 | } |
1337 | } |
1137 | else |
1338 | else |
1138 | ERR ("'true' expected"); |
1339 | ERR ("'true' expected"); |
1139 | |
1340 | |
… | |
… | |
1141 | |
1342 | |
1142 | case 'f': |
1343 | case 'f': |
1143 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1344 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1144 | { |
1345 | { |
1145 | dec->cur += 5; |
1346 | dec->cur += 5; |
|
|
1347 | #if JSON_SLOW |
|
|
1348 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
|
|
1349 | #endif |
1146 | return SvREFCNT_inc (json_false); |
1350 | return SvREFCNT_inc (json_false); |
1147 | } |
1351 | } |
1148 | else |
1352 | else |
1149 | ERR ("'false' expected"); |
1353 | ERR ("'false' expected"); |
1150 | |
1354 | |
… | |
… | |
1169 | fail: |
1373 | fail: |
1170 | return 0; |
1374 | return 0; |
1171 | } |
1375 | } |
1172 | |
1376 | |
1173 | static SV * |
1377 | static SV * |
1174 | decode_json (SV *string, struct json *json, UV *offset_return) |
1378 | decode_json (SV *string, JSON *json, UV *offset_return) |
1175 | { |
1379 | { |
1176 | dec_t dec; |
1380 | dec_t dec; |
1177 | UV offset; |
1381 | UV offset; |
1178 | SV *sv; |
1382 | SV *sv; |
1179 | |
1383 | |
… | |
… | |
1196 | dec.end = SvEND (string); |
1400 | dec.end = SvEND (string); |
1197 | dec.err = 0; |
1401 | dec.err = 0; |
1198 | dec.depth = 0; |
1402 | dec.depth = 0; |
1199 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1403 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1200 | |
1404 | |
|
|
1405 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1406 | dec.json.flags |= F_HOOK; |
|
|
1407 | |
1201 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1408 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1409 | |
|
|
1410 | decode_ws (&dec); |
1202 | sv = decode_sv (&dec); |
1411 | sv = decode_sv (&dec); |
1203 | |
1412 | |
1204 | if (!(offset_return || !sv)) |
1413 | if (!(offset_return || !sv)) |
1205 | { |
1414 | { |
1206 | // check for trailing garbage |
1415 | // check for trailing garbage |
… | |
… | |
1274 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1483 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1275 | } |
1484 | } |
1276 | |
1485 | |
1277 | PROTOTYPES: DISABLE |
1486 | PROTOTYPES: DISABLE |
1278 | |
1487 | |
1279 | SV *new (char *dummy) |
1488 | void CLONE (...) |
1280 | CODE: |
1489 | CODE: |
1281 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1490 | json_stash = 0; |
1282 | OUTPUT: |
1491 | json_boolean_stash = 0; |
1283 | RETVAL |
|
|
1284 | |
1492 | |
|
|
1493 | void new (char *klass) |
|
|
1494 | PPCODE: |
|
|
1495 | { |
|
|
1496 | SV *pv = NEWSV (0, sizeof (JSON)); |
|
|
1497 | SvPOK_only (pv); |
|
|
1498 | Zero (SvPVX (pv), 1, JSON); |
|
|
1499 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1500 | XPUSHs (sv_2mortal (sv_bless ( |
|
|
1501 | newRV_noinc (pv), |
|
|
1502 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
|
|
1503 | ))); |
|
|
1504 | } |
|
|
1505 | |
1285 | SV *ascii (SV *self, int enable = 1) |
1506 | void ascii (JSON *self, int enable = 1) |
1286 | ALIAS: |
1507 | ALIAS: |
1287 | ascii = F_ASCII |
1508 | ascii = F_ASCII |
1288 | latin1 = F_LATIN1 |
1509 | latin1 = F_LATIN1 |
1289 | utf8 = F_UTF8 |
1510 | utf8 = F_UTF8 |
1290 | indent = F_INDENT |
1511 | indent = F_INDENT |
… | |
… | |
1294 | pretty = F_PRETTY |
1515 | pretty = F_PRETTY |
1295 | allow_nonref = F_ALLOW_NONREF |
1516 | allow_nonref = F_ALLOW_NONREF |
1296 | shrink = F_SHRINK |
1517 | shrink = F_SHRINK |
1297 | allow_blessed = F_ALLOW_BLESSED |
1518 | allow_blessed = F_ALLOW_BLESSED |
1298 | convert_blessed = F_CONV_BLESSED |
1519 | convert_blessed = F_CONV_BLESSED |
|
|
1520 | relaxed = F_RELAXED |
|
|
1521 | PPCODE: |
|
|
1522 | { |
|
|
1523 | if (enable) |
|
|
1524 | self->flags |= ix; |
|
|
1525 | else |
|
|
1526 | self->flags &= ~ix; |
|
|
1527 | |
|
|
1528 | XPUSHs (ST (0)); |
|
|
1529 | } |
|
|
1530 | |
|
|
1531 | void get_ascii (JSON *self) |
|
|
1532 | ALIAS: |
|
|
1533 | get_ascii = F_ASCII |
|
|
1534 | get_latin1 = F_LATIN1 |
|
|
1535 | get_utf8 = F_UTF8 |
|
|
1536 | get_indent = F_INDENT |
|
|
1537 | get_canonical = F_CANONICAL |
|
|
1538 | get_space_before = F_SPACE_BEFORE |
|
|
1539 | get_space_after = F_SPACE_AFTER |
|
|
1540 | get_allow_nonref = F_ALLOW_NONREF |
|
|
1541 | get_shrink = F_SHRINK |
|
|
1542 | get_allow_blessed = F_ALLOW_BLESSED |
|
|
1543 | get_convert_blessed = F_CONV_BLESSED |
|
|
1544 | get_relaxed = F_RELAXED |
|
|
1545 | PPCODE: |
|
|
1546 | XPUSHs (boolSV (self->flags & ix)); |
|
|
1547 | |
|
|
1548 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
|
|
1549 | PPCODE: |
|
|
1550 | { |
|
|
1551 | UV log2 = 0; |
|
|
1552 | |
|
|
1553 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1554 | |
|
|
1555 | while ((1UL << log2) < max_depth) |
|
|
1556 | ++log2; |
|
|
1557 | |
|
|
1558 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1559 | |
|
|
1560 | XPUSHs (ST (0)); |
|
|
1561 | } |
|
|
1562 | |
|
|
1563 | U32 get_max_depth (JSON *self) |
1299 | CODE: |
1564 | CODE: |
1300 | { |
1565 | RETVAL = DEC_DEPTH (self->flags); |
1301 | UV *uv = SvJSON (self); |
|
|
1302 | if (enable) |
|
|
1303 | *uv |= ix; |
|
|
1304 | else |
|
|
1305 | *uv &= ~ix; |
|
|
1306 | |
|
|
1307 | RETVAL = newSVsv (self); |
|
|
1308 | } |
|
|
1309 | OUTPUT: |
1566 | OUTPUT: |
1310 | RETVAL |
1567 | RETVAL |
1311 | |
1568 | |
1312 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1569 | void max_size (JSON *self, UV max_size = 0) |
|
|
1570 | PPCODE: |
|
|
1571 | { |
|
|
1572 | UV log2 = 0; |
|
|
1573 | |
|
|
1574 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1575 | if (max_size == 1) max_size = 2; |
|
|
1576 | |
|
|
1577 | while ((1UL << log2) < max_size) |
|
|
1578 | ++log2; |
|
|
1579 | |
|
|
1580 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1581 | |
|
|
1582 | XPUSHs (ST (0)); |
|
|
1583 | } |
|
|
1584 | |
|
|
1585 | int get_max_size (JSON *self) |
1313 | CODE: |
1586 | CODE: |
1314 | { |
1587 | RETVAL = DEC_SIZE (self->flags); |
1315 | UV *uv = SvJSON (self); |
|
|
1316 | UV log2 = 0; |
|
|
1317 | |
|
|
1318 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1319 | |
|
|
1320 | while ((1UL << log2) < max_depth) |
|
|
1321 | ++log2; |
|
|
1322 | |
|
|
1323 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1324 | |
|
|
1325 | RETVAL = newSVsv (self); |
|
|
1326 | } |
|
|
1327 | OUTPUT: |
1588 | OUTPUT: |
1328 | RETVAL |
1589 | RETVAL |
1329 | |
1590 | |
1330 | SV *max_size (SV *self, UV max_size = 0) |
1591 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
|
|
1592 | PPCODE: |
|
|
1593 | { |
|
|
1594 | SvREFCNT_dec (self->cb_object); |
|
|
1595 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1596 | |
|
|
1597 | XPUSHs (ST (0)); |
|
|
1598 | } |
|
|
1599 | |
|
|
1600 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1601 | PPCODE: |
|
|
1602 | { |
|
|
1603 | if (!self->cb_sk_object) |
|
|
1604 | self->cb_sk_object = newHV (); |
|
|
1605 | |
|
|
1606 | if (SvOK (cb)) |
|
|
1607 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1608 | else |
|
|
1609 | { |
|
|
1610 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1611 | |
|
|
1612 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1613 | { |
|
|
1614 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1615 | self->cb_sk_object = 0; |
|
|
1616 | } |
|
|
1617 | } |
|
|
1618 | |
|
|
1619 | XPUSHs (ST (0)); |
|
|
1620 | } |
|
|
1621 | |
|
|
1622 | void encode (JSON *self, SV *scalar) |
|
|
1623 | PPCODE: |
|
|
1624 | XPUSHs (encode_json (scalar, self)); |
|
|
1625 | |
|
|
1626 | void decode (JSON *self, SV *jsonstr) |
|
|
1627 | PPCODE: |
|
|
1628 | XPUSHs (decode_json (jsonstr, self, 0)); |
|
|
1629 | |
|
|
1630 | void decode_prefix (JSON *self, SV *jsonstr) |
|
|
1631 | PPCODE: |
|
|
1632 | { |
|
|
1633 | UV offset; |
|
|
1634 | EXTEND (SP, 2); |
|
|
1635 | PUSHs (decode_json (jsonstr, self, &offset)); |
|
|
1636 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1637 | } |
|
|
1638 | |
|
|
1639 | void DESTROY (JSON *self) |
1331 | CODE: |
1640 | CODE: |
1332 | { |
1641 | SvREFCNT_dec (self->cb_sk_object); |
1333 | UV *uv = SvJSON (self); |
1642 | SvREFCNT_dec (self->cb_object); |
1334 | UV log2 = 0; |
|
|
1335 | |
1643 | |
1336 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1644 | PROTOTYPES: ENABLE |
1337 | if (max_size == 1) max_size = 2; |
|
|
1338 | |
1645 | |
1339 | while ((1UL << log2) < max_size) |
|
|
1340 | ++log2; |
|
|
1341 | |
|
|
1342 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1343 | |
|
|
1344 | RETVAL = newSVsv (self); |
|
|
1345 | } |
|
|
1346 | OUTPUT: |
|
|
1347 | RETVAL |
|
|
1348 | |
|
|
1349 | void encode (SV *self, SV *scalar) |
1646 | void encode_json (SV *scalar) |
1350 | PPCODE: |
1647 | PPCODE: |
1351 | { |
1648 | { |
1352 | struct json json = { *SvJSON (self) }; |
1649 | JSON json = { F_DEFAULT | F_UTF8 }; |
1353 | XPUSHs (encode_json (scalar, &json)); |
1650 | XPUSHs (encode_json (scalar, &json)); |
1354 | } |
1651 | } |
1355 | |
1652 | |
1356 | void decode (SV *self, SV *jsonstr) |
1653 | void decode_json (SV *jsonstr) |
1357 | PPCODE: |
1654 | PPCODE: |
1358 | { |
1655 | { |
1359 | struct json json = { *SvJSON (self) }; |
1656 | JSON json = { F_DEFAULT | F_UTF8 }; |
1360 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1657 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1361 | } |
1658 | } |
1362 | |
1659 | |
1363 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1364 | PPCODE: |
|
|
1365 | { |
|
|
1366 | UV offset; |
|
|
1367 | struct json json = { *SvJSON (self) }; |
|
|
1368 | EXTEND (SP, 2); |
|
|
1369 | PUSHs (decode_json (jsonstr, &json, &offset)); |
|
|
1370 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1371 | } |
|
|
1372 | |
|
|
1373 | PROTOTYPES: ENABLE |
|
|
1374 | |
|
|
1375 | void to_json (SV *scalar) |
|
|
1376 | PPCODE: |
|
|
1377 | { |
|
|
1378 | struct json json = { F_DEFAULT | F_UTF8 }; |
|
|
1379 | XPUSHs (encode_json (scalar, &json)); |
|
|
1380 | } |
|
|
1381 | |
|
|
1382 | void from_json (SV *jsonstr) |
|
|
1383 | PPCODE: |
|
|
1384 | { |
|
|
1385 | struct json json = { F_DEFAULT | F_UTF8 }; |
|
|
1386 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1387 | } |
|
|
1388 | |
|
|