… | |
… | |
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 | |
8 | |
9 | #define F_ASCII 0x00000001 |
9 | #define F_ASCII 0x00000001UL |
10 | #define F_UTF8 0x00000002 |
10 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004 |
11 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008 |
12 | #define F_CANONICAL 0x00000008UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
13 | #define F_SPACE_BEFORE 0x00000010UL |
14 | #define F_SPACE_AFTER 0x00000020 |
14 | #define F_SPACE_AFTER 0x00000020UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
15 | #define F_ALLOW_NONREF 0x00000080UL |
16 | #define F_SHRINK 0x00000100 |
16 | #define F_SHRINK 0x00000100UL |
|
|
17 | #define F_MAXDEPTH 0xf8000000UL |
|
|
18 | #define S_MAXDEPTH 27 |
|
|
19 | |
|
|
20 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
21 | |
|
|
22 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
23 | // F_BLESSED? <=> { $__class__$ => } |
17 | |
24 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
26 | #define F_DEFAULT (12UL << S_MAXDEPTH) |
20 | |
27 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
|
|
29 | #define INDENT_STEP 3 // spaces per indentation level |
|
|
30 | |
|
|
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
32 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
22 | |
33 | |
23 | #define SB do { |
34 | #define SB do { |
24 | #define SE } while (0) |
35 | #define SE } while (0) |
25 | |
36 | |
26 | static HV *json_stash; |
37 | static HV *json_stash; // JSON::XS:: |
|
|
38 | |
|
|
39 | ///////////////////////////////////////////////////////////////////////////// |
|
|
40 | // utility functions |
|
|
41 | |
|
|
42 | static UV * |
|
|
43 | SvJSON (SV *sv) |
|
|
44 | { |
|
|
45 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
46 | croak ("object is not of type JSON::XS"); |
|
|
47 | |
|
|
48 | return &SvUVX (SvRV (sv)); |
|
|
49 | } |
|
|
50 | |
|
|
51 | static void |
|
|
52 | shrink (SV *sv) |
|
|
53 | { |
|
|
54 | sv_utf8_downgrade (sv, 1); |
|
|
55 | if (SvLEN (sv) > SvCUR (sv) + 1) |
|
|
56 | { |
|
|
57 | #ifdef SvPV_shrink_to_cur |
|
|
58 | SvPV_shrink_to_cur (sv); |
|
|
59 | #elif defined (SvPV_renew) |
|
|
60 | SvPV_renew (sv, SvCUR (sv) + 1); |
|
|
61 | #endif |
|
|
62 | } |
|
|
63 | } |
|
|
64 | |
|
|
65 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
66 | // case of an error. |
|
|
67 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
68 | // but use the very good perl function to parse anything else. |
|
|
69 | // note that we never call this function for a ascii codepoints |
|
|
70 | static UV |
|
|
71 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
72 | { |
|
|
73 | if (s[0] > 0xdf || s[0] < 0xc2) |
|
|
74 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
75 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
76 | { |
|
|
77 | *clen = 2; |
|
|
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
79 | } |
|
|
80 | else |
|
|
81 | return (UV)-1; |
|
|
82 | } |
|
|
83 | |
|
|
84 | ///////////////////////////////////////////////////////////////////////////// |
|
|
85 | // encoder |
27 | |
86 | |
28 | // structure used for encoding JSON |
87 | // structure used for encoding JSON |
29 | typedef struct |
88 | typedef struct |
30 | { |
89 | { |
31 | char *cur; |
90 | char *cur; // SvPVX (sv) + current output position |
32 | STRLEN len; // SvLEN (sv) |
|
|
33 | char *end; // SvEND (sv) |
91 | char *end; // SvEND (sv) |
34 | SV *sv; |
92 | SV *sv; // result scalar |
35 | UV flags; |
93 | U32 flags; // F_* |
36 | int max_recurse; |
94 | U32 indent; // indentation level |
37 | int indent; |
95 | U32 maxdepth; // max. indentation/recursion level |
38 | } enc_t; |
96 | } enc_t; |
39 | |
|
|
40 | // structure used for decoding JSON |
|
|
41 | typedef struct |
|
|
42 | { |
|
|
43 | char *cur; |
|
|
44 | char *end; |
|
|
45 | const char *err; |
|
|
46 | UV flags; |
|
|
47 | } dec_t; |
|
|
48 | |
|
|
49 | static UV * |
|
|
50 | SvJSON (SV *sv) |
|
|
51 | { |
|
|
52 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
53 | croak ("object is not of type JSON::XS"); |
|
|
54 | |
|
|
55 | return &SvUVX (SvRV (sv)); |
|
|
56 | } |
|
|
57 | |
|
|
58 | static void |
|
|
59 | shrink (SV *sv) |
|
|
60 | { |
|
|
61 | sv_utf8_downgrade (sv, 1); |
|
|
62 | #ifdef SvPV_shrink_to_cur |
|
|
63 | SvPV_shrink_to_cur (sv); |
|
|
64 | #endif |
|
|
65 | } |
|
|
66 | |
|
|
67 | ///////////////////////////////////////////////////////////////////////////// |
|
|
68 | |
97 | |
69 | static void |
98 | static void |
70 | need (enc_t *enc, STRLEN len) |
99 | need (enc_t *enc, STRLEN len) |
71 | { |
100 | { |
72 | if (enc->cur + len >= enc->end) |
101 | if (enc->cur + len >= enc->end) |
… | |
… | |
130 | STRLEN clen; |
159 | STRLEN clen; |
131 | UV uch; |
160 | UV uch; |
132 | |
161 | |
133 | if (is_utf8) |
162 | if (is_utf8) |
134 | { |
163 | { |
135 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
164 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
165 | uch = decode_utf8 (str, end - str, &clen); |
136 | if (clen == (STRLEN)-1) |
166 | if (clen == (STRLEN)-1) |
137 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
167 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
138 | } |
168 | } |
139 | else |
169 | else |
140 | { |
170 | { |
… | |
… | |
178 | } |
208 | } |
179 | while (--clen); |
209 | while (--clen); |
180 | } |
210 | } |
181 | else |
211 | else |
182 | { |
212 | { |
183 | need (enc, len += 10); // never more than 11 bytes needed |
213 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
184 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
214 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
185 | ++str; |
215 | ++str; |
186 | } |
216 | } |
187 | } |
217 | } |
188 | } |
218 | } |
… | |
… | |
190 | |
220 | |
191 | --len; |
221 | --len; |
192 | } |
222 | } |
193 | } |
223 | } |
194 | |
224 | |
195 | #define INDENT SB \ |
225 | static void |
|
|
226 | encode_indent (enc_t *enc) |
|
|
227 | { |
196 | if (enc->flags & F_INDENT) \ |
228 | if (enc->flags & F_INDENT) |
197 | { \ |
229 | { |
198 | int i_; \ |
230 | int spaces = enc->indent * INDENT_STEP; |
199 | need (enc, enc->indent); \ |
|
|
200 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
201 | encode_ch (enc, ' '); \ |
|
|
202 | } \ |
|
|
203 | SE |
|
|
204 | |
231 | |
205 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
232 | need (enc, spaces); |
206 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
233 | memset (enc->cur, ' ', spaces); |
207 | #define COMMA SB \ |
234 | enc->cur += spaces; |
|
|
235 | } |
|
|
236 | } |
|
|
237 | |
|
|
238 | static void |
|
|
239 | encode_space (enc_t *enc) |
|
|
240 | { |
|
|
241 | need (enc, 1); |
208 | encode_ch (enc, ','); \ |
242 | encode_ch (enc, ' '); |
|
|
243 | } |
|
|
244 | |
|
|
245 | static void |
|
|
246 | encode_nl (enc_t *enc) |
|
|
247 | { |
209 | if (enc->flags & F_INDENT) \ |
248 | if (enc->flags & F_INDENT) |
210 | NL; \ |
249 | { |
|
|
250 | need (enc, 1); |
|
|
251 | encode_ch (enc, '\n'); |
|
|
252 | } |
|
|
253 | } |
|
|
254 | |
|
|
255 | static void |
|
|
256 | encode_comma (enc_t *enc) |
|
|
257 | { |
|
|
258 | encode_ch (enc, ','); |
|
|
259 | |
|
|
260 | if (enc->flags & F_INDENT) |
|
|
261 | encode_nl (enc); |
211 | else if (enc->flags & F_SPACE_AFTER) \ |
262 | else if (enc->flags & F_SPACE_AFTER) |
212 | SPACE; \ |
263 | encode_space (enc); |
213 | SE |
264 | } |
214 | |
265 | |
215 | static void encode_sv (enc_t *enc, SV *sv); |
266 | static void encode_sv (enc_t *enc, SV *sv); |
216 | |
267 | |
217 | static void |
268 | static void |
218 | encode_av (enc_t *enc, AV *av) |
269 | encode_av (enc_t *enc, AV *av) |
219 | { |
270 | { |
220 | int i, len = av_len (av); |
271 | int i, len = av_len (av); |
221 | |
272 | |
222 | encode_ch (enc, '['); NL; |
273 | if (enc->indent >= enc->maxdepth) |
|
|
274 | croak ("data structure too deep (hit recursion limit)"); |
|
|
275 | |
|
|
276 | encode_ch (enc, '['); encode_nl (enc); |
223 | ++enc->indent; |
277 | ++enc->indent; |
224 | |
278 | |
225 | for (i = 0; i <= len; ++i) |
279 | for (i = 0; i <= len; ++i) |
226 | { |
280 | { |
227 | INDENT; |
281 | encode_indent (enc); |
228 | encode_sv (enc, *av_fetch (av, i, 0)); |
282 | encode_sv (enc, *av_fetch (av, i, 0)); |
229 | |
283 | |
230 | if (i < len) |
284 | if (i < len) |
231 | COMMA; |
285 | encode_comma (enc); |
232 | } |
286 | } |
233 | |
287 | |
234 | NL; |
288 | encode_nl (enc); |
235 | |
289 | |
236 | --enc->indent; |
290 | --enc->indent; |
237 | INDENT; encode_ch (enc, ']'); |
291 | encode_indent (enc); encode_ch (enc, ']'); |
238 | } |
292 | } |
239 | |
293 | |
240 | static void |
294 | static void |
241 | encode_he (enc_t *enc, HE *he) |
295 | encode_he (enc_t *enc, HE *he) |
242 | { |
296 | { |
… | |
… | |
256 | else |
310 | else |
257 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
311 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
258 | |
312 | |
259 | encode_ch (enc, '"'); |
313 | encode_ch (enc, '"'); |
260 | |
314 | |
261 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
315 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
262 | encode_ch (enc, ':'); |
316 | encode_ch (enc, ':'); |
263 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
317 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
264 | encode_sv (enc, HeVAL (he)); |
318 | encode_sv (enc, HeVAL (he)); |
265 | } |
319 | } |
266 | |
320 | |
267 | // compare hash entries, used when all keys are bytestrings |
321 | // compare hash entries, used when all keys are bytestrings |
268 | static int |
322 | static int |
… | |
… | |
274 | HE *b = *(HE **)b_; |
328 | HE *b = *(HE **)b_; |
275 | |
329 | |
276 | STRLEN la = HeKLEN (a); |
330 | STRLEN la = HeKLEN (a); |
277 | STRLEN lb = HeKLEN (b); |
331 | STRLEN lb = HeKLEN (b); |
278 | |
332 | |
279 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
333 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
280 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
334 | cmp = la - lb; |
281 | |
335 | |
282 | return cmp; |
336 | return cmp; |
283 | } |
337 | } |
284 | |
338 | |
285 | // compare hash entries, used when some keys are sv's or utf-x |
339 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
292 | static void |
346 | static void |
293 | encode_hv (enc_t *enc, HV *hv) |
347 | encode_hv (enc_t *enc, HV *hv) |
294 | { |
348 | { |
295 | int count, i; |
349 | int count, i; |
296 | |
350 | |
|
|
351 | if (enc->indent >= enc->maxdepth) |
|
|
352 | croak ("data structure too deep (hit recursion limit)"); |
|
|
353 | |
297 | encode_ch (enc, '{'); NL; ++enc->indent; |
354 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
298 | |
355 | |
299 | if ((count = hv_iterinit (hv))) |
356 | if ((count = hv_iterinit (hv))) |
300 | { |
357 | { |
301 | // for canonical output we have to sort by keys first |
358 | // for canonical output we have to sort by keys first |
302 | // actually, this is mostly due to the stupid so-called |
359 | // actually, this is mostly due to the stupid so-called |
303 | // security workaround added somewhere in 5.8.x. |
360 | // security workaround added somewhere in 5.8.x. |
304 | // that randomises hash orderings |
361 | // that randomises hash orderings |
305 | if (enc->flags & F_CANONICAL) |
362 | if (enc->flags & F_CANONICAL) |
306 | { |
363 | { |
307 | HE *he, *hes [count]; |
364 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
308 | int fast = 1; |
365 | int fast = 1; |
309 | |
366 | |
310 | i = 0; |
367 | i = 0; |
311 | while ((he = hv_iternext (hv))) |
368 | while ((he = hv_iternext (hv))) |
312 | { |
369 | { |
… | |
… | |
337 | LEAVE; |
394 | LEAVE; |
338 | } |
395 | } |
339 | |
396 | |
340 | for (i = 0; i < count; ++i) |
397 | for (i = 0; i < count; ++i) |
341 | { |
398 | { |
342 | INDENT; |
399 | encode_indent (enc); |
343 | encode_he (enc, hes [i]); |
400 | encode_he (enc, hes [i]); |
344 | |
401 | |
345 | if (i < count - 1) |
402 | if (i < count - 1) |
346 | COMMA; |
403 | encode_comma (enc); |
347 | } |
404 | } |
348 | |
405 | |
349 | NL; |
406 | encode_nl (enc); |
350 | } |
407 | } |
351 | else |
408 | else |
352 | { |
409 | { |
353 | SV *sv; |
410 | SV *sv; |
354 | HE *he = hv_iternext (hv); |
411 | HE *he = hv_iternext (hv); |
355 | |
412 | |
356 | for (;;) |
413 | for (;;) |
357 | { |
414 | { |
358 | INDENT; |
415 | encode_indent (enc); |
359 | encode_he (enc, he); |
416 | encode_he (enc, he); |
360 | |
417 | |
361 | if (!(he = hv_iternext (hv))) |
418 | if (!(he = hv_iternext (hv))) |
362 | break; |
419 | break; |
363 | |
420 | |
364 | COMMA; |
421 | encode_comma (enc); |
365 | } |
422 | } |
366 | |
423 | |
367 | NL; |
424 | encode_nl (enc); |
368 | } |
425 | } |
369 | } |
426 | } |
370 | |
427 | |
371 | --enc->indent; INDENT; encode_ch (enc, '}'); |
428 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
429 | } |
|
|
430 | |
|
|
431 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
432 | static void |
|
|
433 | encode_rv (enc_t *enc, SV *sv) |
|
|
434 | { |
|
|
435 | SvGETMAGIC (sv); |
|
|
436 | |
|
|
437 | svtype svt = SvTYPE (sv); |
|
|
438 | |
|
|
439 | if (svt == SVt_PVHV) |
|
|
440 | encode_hv (enc, (HV *)sv); |
|
|
441 | else if (svt == SVt_PVAV) |
|
|
442 | encode_av (enc, (AV *)sv); |
|
|
443 | else if (svt < SVt_PVAV) |
|
|
444 | { |
|
|
445 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
446 | encode_str (enc, "false", 5, 0); |
|
|
447 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
448 | encode_str (enc, "true", 4, 0); |
|
|
449 | else |
|
|
450 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
451 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
452 | } |
|
|
453 | else |
|
|
454 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
455 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
372 | } |
456 | } |
373 | |
457 | |
374 | static void |
458 | static void |
375 | encode_sv (enc_t *enc, SV *sv) |
459 | encode_sv (enc_t *enc, SV *sv) |
376 | { |
460 | { |
… | |
… | |
397 | SvIsUV(sv) |
481 | SvIsUV(sv) |
398 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
482 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
399 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
483 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
400 | } |
484 | } |
401 | else if (SvROK (sv)) |
485 | else if (SvROK (sv)) |
402 | { |
486 | encode_rv (enc, SvRV (sv)); |
403 | SV *rv = SvRV (sv); |
|
|
404 | |
|
|
405 | if (!--enc->max_recurse) |
|
|
406 | croak ("data structure too deep (hit recursion limit)"); |
|
|
407 | |
|
|
408 | switch (SvTYPE (rv)) |
|
|
409 | { |
|
|
410 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
411 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
412 | |
|
|
413 | default: |
|
|
414 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
415 | SvPV_nolen (sv)); |
|
|
416 | } |
|
|
417 | } |
|
|
418 | else if (!SvOK (sv)) |
487 | else if (!SvOK (sv)) |
419 | encode_str (enc, "null", 4, 0); |
488 | encode_str (enc, "null", 4, 0); |
420 | else |
489 | else |
421 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
490 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
422 | SvPV_nolen (sv), SvFLAGS (sv)); |
491 | SvPV_nolen (sv), SvFLAGS (sv)); |
423 | } |
492 | } |
424 | |
493 | |
425 | static SV * |
494 | static SV * |
426 | encode_json (SV *scalar, UV flags) |
495 | encode_json (SV *scalar, U32 flags) |
427 | { |
496 | { |
428 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
497 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
429 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
498 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
430 | |
499 | |
431 | enc_t enc; |
500 | enc_t enc; |
432 | enc.flags = flags; |
501 | enc.flags = flags; |
433 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
502 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
434 | enc.cur = SvPVX (enc.sv); |
503 | enc.cur = SvPVX (enc.sv); |
435 | enc.end = SvEND (enc.sv); |
504 | enc.end = SvEND (enc.sv); |
436 | enc.max_recurse = 0; |
|
|
437 | enc.indent = 0; |
505 | enc.indent = 0; |
|
|
506 | enc.maxdepth = DEC_DEPTH (flags); |
438 | |
507 | |
439 | SvPOK_only (enc.sv); |
508 | SvPOK_only (enc.sv); |
440 | encode_sv (&enc, scalar); |
509 | encode_sv (&enc, scalar); |
441 | |
510 | |
442 | if (!(flags & (F_ASCII | F_UTF8))) |
511 | if (!(flags & (F_ASCII | F_UTF8))) |
… | |
… | |
449 | |
518 | |
450 | return enc.sv; |
519 | return enc.sv; |
451 | } |
520 | } |
452 | |
521 | |
453 | ///////////////////////////////////////////////////////////////////////////// |
522 | ///////////////////////////////////////////////////////////////////////////// |
|
|
523 | // decoder |
454 | |
524 | |
455 | #define WS \ |
525 | // structure used for decoding JSON |
|
|
526 | typedef struct |
|
|
527 | { |
|
|
528 | char *cur; // current parser pointer |
|
|
529 | char *end; // end of input string |
|
|
530 | const char *err; // parse error, if != 0 |
|
|
531 | U32 flags; // F_* |
|
|
532 | U32 depth; // recursion depth |
|
|
533 | U32 maxdepth; // recursion depth limit |
|
|
534 | } dec_t; |
|
|
535 | |
|
|
536 | static void |
|
|
537 | decode_ws (dec_t *dec) |
|
|
538 | { |
456 | for (;;) \ |
539 | for (;;) |
457 | { \ |
540 | { |
458 | char ch = *dec->cur; \ |
541 | char ch = *dec->cur; |
|
|
542 | |
459 | if (ch > 0x20 \ |
543 | if (ch > 0x20 |
460 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
544 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
461 | break; \ |
545 | break; |
|
|
546 | |
462 | ++dec->cur; \ |
547 | ++dec->cur; |
463 | } |
548 | } |
|
|
549 | } |
464 | |
550 | |
465 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
551 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
552 | |
466 | #define EXPECT_CH(ch) SB \ |
553 | #define EXPECT_CH(ch) SB \ |
467 | if (*dec->cur != ch) \ |
554 | if (*dec->cur != ch) \ |
468 | ERR (# ch " expected"); \ |
555 | ERR (# ch " expected"); \ |
469 | ++dec->cur; \ |
556 | ++dec->cur; \ |
470 | SE |
557 | SE |
471 | |
558 | |
|
|
559 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
560 | #define DEC_DEC_DEPTH --dec->depth |
|
|
561 | |
472 | static SV *decode_sv (dec_t *dec); |
562 | static SV *decode_sv (dec_t *dec); |
473 | |
563 | |
474 | static signed char decode_hexdigit[256]; |
564 | static signed char decode_hexdigit[256]; |
475 | |
565 | |
476 | static UV |
566 | static UV |
477 | decode_4hex (dec_t *dec) |
567 | decode_4hex (dec_t *dec) |
478 | { |
568 | { |
479 | signed char d1, d2, d3, d4; |
569 | signed char d1, d2, d3, d4; |
|
|
570 | unsigned char *cur = (unsigned char *)dec->cur; |
480 | |
571 | |
481 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
482 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
572 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
483 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
|
|
484 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
573 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
485 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
486 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
574 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
487 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
488 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
575 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
489 | |
576 | |
490 | dec->cur += 4; |
577 | dec->cur += 4; |
491 | |
578 | |
492 | return ((UV)d1) << 12 |
579 | return ((UV)d1) << 12 |
493 | | ((UV)d2) << 8 |
580 | | ((UV)d2) << 8 |
… | |
… | |
496 | |
583 | |
497 | fail: |
584 | fail: |
498 | return (UV)-1; |
585 | return (UV)-1; |
499 | } |
586 | } |
500 | |
587 | |
501 | #define APPEND_GROW(n) SB \ |
|
|
502 | if (cur + (n) >= end) \ |
|
|
503 | { \ |
|
|
504 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
505 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
506 | cur = SvPVX (sv) + ofs; \ |
|
|
507 | end = SvEND (sv); \ |
|
|
508 | } \ |
|
|
509 | SE |
|
|
510 | |
|
|
511 | #define APPEND_CH(ch) SB \ |
|
|
512 | APPEND_GROW (1); \ |
|
|
513 | *cur++ = (ch); \ |
|
|
514 | SE |
|
|
515 | |
|
|
516 | static SV * |
588 | static SV * |
517 | decode_str (dec_t *dec) |
589 | decode_str (dec_t *dec) |
518 | { |
590 | { |
519 | SV *sv = NEWSV (0,2); |
591 | SV *sv = 0; |
520 | int utf8 = 0; |
592 | int utf8 = 0; |
521 | char *cur = SvPVX (sv); |
|
|
522 | char *end = SvEND (sv); |
|
|
523 | |
593 | |
524 | for (;;) |
594 | do |
525 | { |
595 | { |
526 | unsigned char ch = *(unsigned char *)dec->cur; |
596 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
|
|
597 | char *cur = buf; |
527 | |
598 | |
528 | if (ch == '"') |
599 | do |
529 | break; |
|
|
530 | else if (ch == '\\') |
|
|
531 | { |
600 | { |
532 | switch (*++dec->cur) |
601 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
602 | |
|
|
603 | if (ch == '"') |
533 | { |
604 | { |
534 | case '\\': |
605 | --dec->cur; |
535 | case '/': |
606 | break; |
536 | case '"': APPEND_CH (*dec->cur++); break; |
607 | } |
537 | |
608 | else if (ch == '\\') |
538 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
609 | { |
539 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
610 | switch (*dec->cur) |
540 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
541 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
542 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
543 | |
|
|
544 | case 'u': |
|
|
545 | { |
611 | { |
546 | UV lo, hi; |
612 | case '\\': |
547 | ++dec->cur; |
613 | case '/': |
|
|
614 | case '"': *cur++ = *dec->cur++; break; |
548 | |
615 | |
549 | hi = decode_4hex (dec); |
616 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
550 | if (hi == (UV)-1) |
617 | case 't': ++dec->cur; *cur++ = '\011'; break; |
551 | goto fail; |
618 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
|
|
619 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
|
|
620 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
552 | |
621 | |
553 | // possibly a surrogate pair |
622 | case 'u': |
554 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
555 | { |
623 | { |
556 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
624 | UV lo, hi; |
557 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
558 | |
|
|
559 | dec->cur += 2; |
625 | ++dec->cur; |
560 | |
626 | |
561 | lo = decode_4hex (dec); |
627 | hi = decode_4hex (dec); |
562 | if (lo == (UV)-1) |
628 | if (hi == (UV)-1) |
563 | goto fail; |
629 | goto fail; |
564 | |
630 | |
|
|
631 | // possibly a surrogate pair |
|
|
632 | if (hi >= 0xd800) |
|
|
633 | if (hi < 0xdc00) |
|
|
634 | { |
|
|
635 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
|
|
636 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
637 | |
|
|
638 | dec->cur += 2; |
|
|
639 | |
|
|
640 | lo = decode_4hex (dec); |
|
|
641 | if (lo == (UV)-1) |
|
|
642 | goto fail; |
|
|
643 | |
565 | if (lo < 0xdc00 || lo >= 0xe000) |
644 | if (lo < 0xdc00 || lo >= 0xe000) |
566 | ERR ("surrogate pair expected"); |
645 | ERR ("surrogate pair expected"); |
567 | |
646 | |
568 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
647 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
648 | } |
|
|
649 | else if (hi < 0xe000) |
|
|
650 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
651 | |
|
|
652 | if (hi >= 0x80) |
|
|
653 | { |
|
|
654 | utf8 = 1; |
|
|
655 | |
|
|
656 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
657 | } |
|
|
658 | else |
|
|
659 | *cur++ = hi; |
569 | } |
660 | } |
570 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
571 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
572 | |
|
|
573 | if (hi >= 0x80) |
|
|
574 | { |
661 | break; |
575 | utf8 = 1; |
|
|
576 | |
662 | |
577 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
578 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
579 | } |
|
|
580 | else |
663 | default: |
581 | APPEND_CH (hi); |
664 | --dec->cur; |
|
|
665 | ERR ("illegal backslash escape sequence in string"); |
582 | } |
666 | } |
583 | break; |
|
|
584 | |
|
|
585 | default: |
|
|
586 | --dec->cur; |
|
|
587 | ERR ("illegal backslash escape sequence in string"); |
|
|
588 | } |
667 | } |
|
|
668 | else if (ch >= 0x20 && ch <= 0x7f) |
|
|
669 | *cur++ = ch; |
|
|
670 | else if (ch >= 0x80) |
|
|
671 | { |
|
|
672 | --dec->cur; |
|
|
673 | |
|
|
674 | STRLEN clen; |
|
|
675 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
|
|
676 | if (clen == (STRLEN)-1) |
|
|
677 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
678 | |
|
|
679 | do |
|
|
680 | { |
|
|
681 | *cur++ = *dec->cur++; |
|
|
682 | } |
|
|
683 | while (--clen); |
|
|
684 | |
|
|
685 | utf8 = 1; |
|
|
686 | } |
|
|
687 | else if (!ch) |
|
|
688 | ERR ("unexpected end of string while parsing json string"); |
|
|
689 | else |
|
|
690 | ERR ("invalid character encountered"); |
|
|
691 | |
589 | } |
692 | } |
590 | else if (ch >= 0x20 && ch <= 0x7f) |
693 | while (cur < buf + SHORT_STRING_LEN); |
591 | APPEND_CH (*dec->cur++); |
694 | |
592 | else if (ch >= 0x80) |
695 | STRLEN len = cur - buf; |
|
|
696 | |
|
|
697 | if (sv) |
593 | { |
698 | { |
594 | STRLEN clen; |
699 | SvGROW (sv, SvCUR (sv) + len + 1); |
595 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
700 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
596 | if (clen == (STRLEN)-1) |
701 | SvCUR_set (sv, SvCUR (sv) + len); |
597 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
598 | |
|
|
599 | APPEND_GROW (clen); |
|
|
600 | do |
|
|
601 | { |
|
|
602 | *cur++ = *dec->cur++; |
|
|
603 | } |
|
|
604 | while (--clen); |
|
|
605 | |
|
|
606 | utf8 = 1; |
|
|
607 | } |
702 | } |
608 | else if (dec->cur == dec->end) |
|
|
609 | ERR ("unexpected end of string while parsing json string"); |
|
|
610 | else |
703 | else |
611 | ERR ("invalid character encountered"); |
704 | sv = newSVpvn (buf, len); |
612 | } |
705 | } |
|
|
706 | while (*dec->cur != '"'); |
613 | |
707 | |
614 | ++dec->cur; |
708 | ++dec->cur; |
615 | |
709 | |
616 | SvCUR_set (sv, cur - SvPVX (sv)); |
710 | if (sv) |
617 | |
711 | { |
618 | SvPOK_only (sv); |
712 | SvPOK_only (sv); |
619 | *SvEND (sv) = 0; |
713 | *SvEND (sv) = 0; |
620 | |
714 | |
621 | if (utf8) |
715 | if (utf8) |
622 | SvUTF8_on (sv); |
716 | SvUTF8_on (sv); |
623 | |
717 | } |
624 | if (dec->flags & F_SHRINK) |
718 | else |
625 | shrink (sv); |
719 | sv = newSVpvn ("", 0); |
626 | |
720 | |
627 | return sv; |
721 | return sv; |
628 | |
722 | |
629 | fail: |
723 | fail: |
630 | SvREFCNT_dec (sv); |
|
|
631 | return 0; |
724 | return 0; |
632 | } |
725 | } |
633 | |
726 | |
634 | static SV * |
727 | static SV * |
635 | decode_num (dec_t *dec) |
728 | decode_num (dec_t *dec) |
… | |
… | |
716 | static SV * |
809 | static SV * |
717 | decode_av (dec_t *dec) |
810 | decode_av (dec_t *dec) |
718 | { |
811 | { |
719 | AV *av = newAV (); |
812 | AV *av = newAV (); |
720 | |
813 | |
721 | WS; |
814 | DEC_INC_DEPTH; |
|
|
815 | decode_ws (dec); |
|
|
816 | |
722 | if (*dec->cur == ']') |
817 | if (*dec->cur == ']') |
723 | ++dec->cur; |
818 | ++dec->cur; |
724 | else |
819 | else |
725 | for (;;) |
820 | for (;;) |
726 | { |
821 | { |
… | |
… | |
730 | if (!value) |
825 | if (!value) |
731 | goto fail; |
826 | goto fail; |
732 | |
827 | |
733 | av_push (av, value); |
828 | av_push (av, value); |
734 | |
829 | |
735 | WS; |
830 | decode_ws (dec); |
736 | |
831 | |
737 | if (*dec->cur == ']') |
832 | if (*dec->cur == ']') |
738 | { |
833 | { |
739 | ++dec->cur; |
834 | ++dec->cur; |
740 | break; |
835 | break; |
… | |
… | |
744 | ERR (", or ] expected while parsing array"); |
839 | ERR (", or ] expected while parsing array"); |
745 | |
840 | |
746 | ++dec->cur; |
841 | ++dec->cur; |
747 | } |
842 | } |
748 | |
843 | |
|
|
844 | DEC_DEC_DEPTH; |
749 | return newRV_noinc ((SV *)av); |
845 | return newRV_noinc ((SV *)av); |
750 | |
846 | |
751 | fail: |
847 | fail: |
752 | SvREFCNT_dec (av); |
848 | SvREFCNT_dec (av); |
|
|
849 | DEC_DEC_DEPTH; |
753 | return 0; |
850 | return 0; |
754 | } |
851 | } |
755 | |
852 | |
756 | static SV * |
853 | static SV * |
757 | decode_hv (dec_t *dec) |
854 | decode_hv (dec_t *dec) |
758 | { |
855 | { |
759 | HV *hv = newHV (); |
856 | HV *hv = newHV (); |
760 | |
857 | |
761 | WS; |
858 | DEC_INC_DEPTH; |
|
|
859 | decode_ws (dec); |
|
|
860 | |
762 | if (*dec->cur == '}') |
861 | if (*dec->cur == '}') |
763 | ++dec->cur; |
862 | ++dec->cur; |
764 | else |
863 | else |
765 | for (;;) |
864 | for (;;) |
766 | { |
865 | { |
767 | SV *key, *value; |
866 | SV *key, *value; |
768 | |
867 | |
769 | WS; EXPECT_CH ('"'); |
868 | decode_ws (dec); EXPECT_CH ('"'); |
770 | |
869 | |
771 | key = decode_str (dec); |
870 | key = decode_str (dec); |
772 | if (!key) |
871 | if (!key) |
773 | goto fail; |
872 | goto fail; |
774 | |
873 | |
775 | WS; EXPECT_CH (':'); |
874 | decode_ws (dec); EXPECT_CH (':'); |
776 | |
875 | |
777 | value = decode_sv (dec); |
876 | value = decode_sv (dec); |
778 | if (!value) |
877 | if (!value) |
779 | { |
878 | { |
780 | SvREFCNT_dec (key); |
879 | SvREFCNT_dec (key); |
781 | goto fail; |
880 | goto fail; |
782 | } |
881 | } |
783 | |
882 | |
784 | //TODO: optimise |
|
|
785 | hv_store_ent (hv, key, value, 0); |
883 | hv_store_ent (hv, key, value, 0); |
|
|
884 | SvREFCNT_dec (key); |
786 | |
885 | |
787 | WS; |
886 | decode_ws (dec); |
788 | |
887 | |
789 | if (*dec->cur == '}') |
888 | if (*dec->cur == '}') |
790 | { |
889 | { |
791 | ++dec->cur; |
890 | ++dec->cur; |
792 | break; |
891 | break; |
… | |
… | |
796 | ERR (", or } expected while parsing object/hash"); |
895 | ERR (", or } expected while parsing object/hash"); |
797 | |
896 | |
798 | ++dec->cur; |
897 | ++dec->cur; |
799 | } |
898 | } |
800 | |
899 | |
|
|
900 | DEC_DEC_DEPTH; |
801 | return newRV_noinc ((SV *)hv); |
901 | return newRV_noinc ((SV *)hv); |
802 | |
902 | |
803 | fail: |
903 | fail: |
804 | SvREFCNT_dec (hv); |
904 | SvREFCNT_dec (hv); |
|
|
905 | DEC_DEC_DEPTH; |
805 | return 0; |
906 | return 0; |
806 | } |
907 | } |
807 | |
908 | |
808 | static SV * |
909 | static SV * |
809 | decode_sv (dec_t *dec) |
910 | decode_sv (dec_t *dec) |
810 | { |
911 | { |
811 | WS; |
912 | decode_ws (dec); |
812 | switch (*dec->cur) |
913 | switch (*dec->cur) |
813 | { |
914 | { |
814 | case '"': ++dec->cur; return decode_str (dec); |
915 | case '"': ++dec->cur; return decode_str (dec); |
815 | case '[': ++dec->cur; return decode_av (dec); |
916 | case '[': ++dec->cur; return decode_av (dec); |
816 | case '{': ++dec->cur; return decode_hv (dec); |
917 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
861 | fail: |
962 | fail: |
862 | return 0; |
963 | return 0; |
863 | } |
964 | } |
864 | |
965 | |
865 | static SV * |
966 | static SV * |
866 | decode_json (SV *string, UV flags) |
967 | decode_json (SV *string, U32 flags) |
867 | { |
968 | { |
868 | SV *sv; |
969 | SV *sv; |
869 | |
970 | |
870 | if (flags & F_UTF8) |
971 | if (flags & F_UTF8) |
871 | sv_utf8_downgrade (string, 0); |
972 | sv_utf8_downgrade (string, 0); |
… | |
… | |
873 | sv_utf8_upgrade (string); |
974 | sv_utf8_upgrade (string); |
874 | |
975 | |
875 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
976 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
876 | |
977 | |
877 | dec_t dec; |
978 | dec_t dec; |
878 | dec.flags = flags; |
979 | dec.flags = flags; |
879 | dec.cur = SvPVX (string); |
980 | dec.cur = SvPVX (string); |
880 | dec.end = SvEND (string); |
981 | dec.end = SvEND (string); |
881 | dec.err = 0; |
982 | dec.err = 0; |
|
|
983 | dec.depth = 0; |
|
|
984 | dec.maxdepth = DEC_DEPTH (dec.flags); |
882 | |
985 | |
|
|
986 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
883 | sv = decode_sv (&dec); |
987 | sv = decode_sv (&dec); |
884 | |
988 | |
885 | if (!sv) |
989 | if (!sv) |
886 | { |
990 | { |
887 | IV offset = dec.flags & F_UTF8 |
991 | IV offset = dec.flags & F_UTF8 |
… | |
… | |
910 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1014 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
911 | |
1015 | |
912 | return sv; |
1016 | return sv; |
913 | } |
1017 | } |
914 | |
1018 | |
|
|
1019 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1020 | // XS interface functions |
|
|
1021 | |
915 | MODULE = JSON::XS PACKAGE = JSON::XS |
1022 | MODULE = JSON::XS PACKAGE = JSON::XS |
916 | |
1023 | |
917 | BOOT: |
1024 | BOOT: |
918 | { |
1025 | { |
919 | int i; |
1026 | int i; |
920 | |
1027 | |
921 | memset (decode_hexdigit, 0xff, 256); |
1028 | memset (decode_hexdigit, 0xff, 256); |
|
|
1029 | |
922 | for (i = 10; i--; ) |
1030 | for (i = 0; i < 256; ++i) |
923 | decode_hexdigit ['0' + i] = i; |
1031 | decode_hexdigit [i] = |
924 | |
1032 | i >= '0' && i <= '9' ? i - '0' |
925 | for (i = 7; i--; ) |
1033 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
926 | { |
1034 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
927 | decode_hexdigit ['a' + i] = 10 + i; |
1035 | : -1; |
928 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
929 | } |
|
|
930 | |
1036 | |
931 | json_stash = gv_stashpv ("JSON::XS", 1); |
1037 | json_stash = gv_stashpv ("JSON::XS", 1); |
932 | } |
1038 | } |
933 | |
1039 | |
934 | PROTOTYPES: DISABLE |
1040 | PROTOTYPES: DISABLE |
… | |
… | |
961 | RETVAL = newSVsv (self); |
1067 | RETVAL = newSVsv (self); |
962 | } |
1068 | } |
963 | OUTPUT: |
1069 | OUTPUT: |
964 | RETVAL |
1070 | RETVAL |
965 | |
1071 | |
|
|
1072 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
|
|
1073 | CODE: |
|
|
1074 | { |
|
|
1075 | UV *uv = SvJSON (self); |
|
|
1076 | UV log2 = 0; |
|
|
1077 | |
|
|
1078 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1079 | |
|
|
1080 | while ((1UL << log2) < max_depth) |
|
|
1081 | ++log2; |
|
|
1082 | |
|
|
1083 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1084 | |
|
|
1085 | RETVAL = newSVsv (self); |
|
|
1086 | } |
|
|
1087 | OUTPUT: |
|
|
1088 | RETVAL |
|
|
1089 | |
966 | void encode (SV *self, SV *scalar) |
1090 | void encode (SV *self, SV *scalar) |
967 | PPCODE: |
1091 | PPCODE: |
968 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1092 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
969 | |
1093 | |
970 | void decode (SV *self, SV *jsonstr) |
1094 | void decode (SV *self, SV *jsonstr) |
… | |
… | |
972 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1096 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
973 | |
1097 | |
974 | PROTOTYPES: ENABLE |
1098 | PROTOTYPES: ENABLE |
975 | |
1099 | |
976 | void to_json (SV *scalar) |
1100 | void to_json (SV *scalar) |
|
|
1101 | ALIAS: |
|
|
1102 | objToJson = 0 |
977 | PPCODE: |
1103 | PPCODE: |
978 | XPUSHs (encode_json (scalar, F_UTF8)); |
1104 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
979 | |
1105 | |
980 | void from_json (SV *jsonstr) |
1106 | void from_json (SV *jsonstr) |
|
|
1107 | ALIAS: |
|
|
1108 | jsonToObj = 0 |
981 | PPCODE: |
1109 | PPCODE: |
982 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1110 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
983 | |
1111 | |