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