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 <float.h> |
9 | |
10 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
13 | #endif |
13 | |
14 | |
|
|
15 | // some old perls do not have this, try to make it work, no |
|
|
16 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
17 | #ifndef UTF8_MAXBYTES |
|
|
18 | # define UTF8_MAXBYTES 13 |
|
|
19 | #endif |
|
|
20 | |
14 | #define F_ASCII 0x00000001UL |
21 | #define F_ASCII 0x00000001UL |
15 | #define F_LATIN1 0x00000002UL |
22 | #define F_LATIN1 0x00000002UL |
16 | #define F_UTF8 0x00000004UL |
23 | #define F_UTF8 0x00000004UL |
17 | #define F_INDENT 0x00000008UL |
24 | #define F_INDENT 0x00000008UL |
18 | #define F_CANONICAL 0x00000010UL |
25 | #define F_CANONICAL 0x00000010UL |
19 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_BEFORE 0x00000020UL |
20 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_SPACE_AFTER 0x00000040UL |
21 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_ALLOW_NONREF 0x00000100UL |
22 | #define F_SHRINK 0x00000200UL |
29 | #define F_SHRINK 0x00000200UL |
|
|
30 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
31 | #define F_CONV_BLESSED 0x00000800UL |
|
|
32 | #define F_RELAXED 0x00001000UL |
|
|
33 | |
23 | #define F_MAXDEPTH 0xf8000000UL |
34 | #define F_MAXDEPTH 0xf8000000UL |
24 | #define S_MAXDEPTH 27 |
35 | #define S_MAXDEPTH 27 |
|
|
36 | #define F_MAXSIZE 0x01f00000UL |
|
|
37 | #define S_MAXSIZE 20 |
|
|
38 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
25 | |
39 | |
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
40 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
27 | |
41 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
28 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
29 | // F_BLESSED? <=> { $__class__$ => } |
|
|
30 | |
42 | |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
43 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
44 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
33 | |
45 | |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
46 | #define INIT_SIZE 32 // initial scalar size to be allocated |
35 | #define INDENT_STEP 3 // spaces per indentation level |
47 | #define INDENT_STEP 3 // spaces per indentation level |
36 | |
48 | |
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
49 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
38 | |
50 | |
39 | #define SB do { |
51 | #define SB do { |
40 | #define SE } while (0) |
52 | #define SE } while (0) |
41 | |
53 | |
|
|
54 | #if __GNUC__ >= 3 |
|
|
55 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
|
|
56 | # define INLINE static inline |
|
|
57 | #else |
|
|
58 | # define expect(expr,value) (expr) |
|
|
59 | # define INLINE static |
|
|
60 | #endif |
|
|
61 | |
|
|
62 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
63 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
64 | |
|
|
65 | #ifdef USE_ITHREADS |
|
|
66 | # define JSON_SLOW 1 |
|
|
67 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
68 | #else |
|
|
69 | # define JSON_SLOW 0 |
|
|
70 | # define JSON_STASH json_stash |
|
|
71 | #endif |
|
|
72 | |
42 | static HV *json_stash; // JSON::XS:: |
73 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
74 | static SV *json_true, *json_false; |
|
|
75 | |
|
|
76 | typedef struct { |
|
|
77 | U32 flags; |
|
|
78 | SV *cb_object; |
|
|
79 | HV *cb_sk_object; |
|
|
80 | } JSON; |
43 | |
81 | |
44 | ///////////////////////////////////////////////////////////////////////////// |
82 | ///////////////////////////////////////////////////////////////////////////// |
45 | // utility functions |
83 | // utility functions |
46 | |
84 | |
47 | static UV * |
85 | INLINE void |
48 | SvJSON (SV *sv) |
|
|
49 | { |
|
|
50 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
51 | croak ("object is not of type JSON::XS"); |
|
|
52 | |
|
|
53 | return &SvUVX (SvRV (sv)); |
|
|
54 | } |
|
|
55 | |
|
|
56 | static void |
|
|
57 | shrink (SV *sv) |
86 | shrink (SV *sv) |
58 | { |
87 | { |
59 | sv_utf8_downgrade (sv, 1); |
88 | sv_utf8_downgrade (sv, 1); |
60 | if (SvLEN (sv) > SvCUR (sv) + 1) |
89 | if (SvLEN (sv) > SvCUR (sv) + 1) |
61 | { |
90 | { |
… | |
… | |
70 | // decode an utf-8 character and return it, or (UV)-1 in |
99 | // decode an utf-8 character and return it, or (UV)-1 in |
71 | // case of an error. |
100 | // case of an error. |
72 | // we special-case "safe" characters from U+80 .. U+7FF, |
101 | // we special-case "safe" characters from U+80 .. U+7FF, |
73 | // but use the very good perl function to parse anything else. |
102 | // but use the very good perl function to parse anything else. |
74 | // note that we never call this function for a ascii codepoints |
103 | // note that we never call this function for a ascii codepoints |
75 | static UV |
104 | INLINE UV |
76 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
105 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
77 | { |
106 | { |
78 | if (s[0] > 0xdf || s[0] < 0xc2) |
107 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
79 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
108 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
80 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
109 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
81 | { |
110 | { |
82 | *clen = 2; |
111 | *clen = 2; |
83 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
112 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
… | |
… | |
96 | typedef struct |
125 | typedef struct |
97 | { |
126 | { |
98 | char *cur; // SvPVX (sv) + current output position |
127 | char *cur; // SvPVX (sv) + current output position |
99 | char *end; // SvEND (sv) |
128 | char *end; // SvEND (sv) |
100 | SV *sv; // result scalar |
129 | SV *sv; // result scalar |
101 | U32 flags; // F_* |
130 | JSON json; |
102 | U32 indent; // indentation level |
131 | U32 indent; // indentation level |
103 | U32 maxdepth; // max. indentation/recursion level |
132 | U32 maxdepth; // max. indentation/recursion level |
|
|
133 | UV limit; // escape character values >= this value when encoding |
104 | } enc_t; |
134 | } enc_t; |
105 | |
135 | |
106 | static void |
136 | INLINE void |
107 | need (enc_t *enc, STRLEN len) |
137 | need (enc_t *enc, STRLEN len) |
108 | { |
138 | { |
109 | if (enc->cur + len >= enc->end) |
139 | if (expect_false (enc->cur + len >= enc->end)) |
110 | { |
140 | { |
111 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
141 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
112 | SvGROW (enc->sv, cur + len + 1); |
142 | SvGROW (enc->sv, cur + len + 1); |
113 | enc->cur = SvPVX (enc->sv) + cur; |
143 | enc->cur = SvPVX (enc->sv) + cur; |
114 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
144 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
115 | } |
145 | } |
116 | } |
146 | } |
117 | |
147 | |
118 | static void |
148 | INLINE void |
119 | encode_ch (enc_t *enc, char ch) |
149 | encode_ch (enc_t *enc, char ch) |
120 | { |
150 | { |
121 | need (enc, 1); |
151 | need (enc, 1); |
122 | *enc->cur++ = ch; |
152 | *enc->cur++ = ch; |
123 | } |
153 | } |
… | |
… | |
131 | |
161 | |
132 | while (str < end) |
162 | while (str < end) |
133 | { |
163 | { |
134 | unsigned char ch = *(unsigned char *)str; |
164 | unsigned char ch = *(unsigned char *)str; |
135 | |
165 | |
136 | if (ch >= 0x20 && ch < 0x80) // most common case |
166 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
137 | { |
167 | { |
138 | if (ch == '"') // but with slow exceptions |
168 | if (expect_false (ch == '"')) // but with slow exceptions |
139 | { |
169 | { |
140 | need (enc, len += 1); |
170 | need (enc, len += 1); |
141 | *enc->cur++ = '\\'; |
171 | *enc->cur++ = '\\'; |
142 | *enc->cur++ = '"'; |
172 | *enc->cur++ = '"'; |
143 | } |
173 | } |
144 | else if (ch == '\\') |
174 | else if (expect_false (ch == '\\')) |
145 | { |
175 | { |
146 | need (enc, len += 1); |
176 | need (enc, len += 1); |
147 | *enc->cur++ = '\\'; |
177 | *enc->cur++ = '\\'; |
148 | *enc->cur++ = '\\'; |
178 | *enc->cur++ = '\\'; |
149 | } |
179 | } |
… | |
… | |
167 | STRLEN clen; |
197 | STRLEN clen; |
168 | UV uch; |
198 | UV uch; |
169 | |
199 | |
170 | if (is_utf8) |
200 | if (is_utf8) |
171 | { |
201 | { |
172 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
173 | uch = decode_utf8 (str, end - str, &clen); |
202 | uch = decode_utf8 (str, end - str, &clen); |
174 | if (clen == (STRLEN)-1) |
203 | if (clen == (STRLEN)-1) |
175 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
204 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
176 | } |
205 | } |
177 | else |
206 | else |
178 | { |
207 | { |
179 | uch = ch; |
208 | uch = ch; |
180 | clen = 1; |
209 | clen = 1; |
181 | } |
210 | } |
182 | |
211 | |
183 | if (uch > 0x10FFFFUL) |
212 | if (uch < 0x20 || uch >= enc->limit) |
184 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
185 | |
|
|
186 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
|
|
187 | { |
213 | { |
188 | if (uch > 0xFFFFUL) |
214 | if (uch > 0xFFFFUL) |
189 | { |
215 | { |
|
|
216 | if (uch > 0x10FFFFUL) |
|
|
217 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
218 | |
190 | need (enc, len += 11); |
219 | need (enc, len += 11); |
191 | sprintf (enc->cur, "\\u%04x\\u%04x", |
220 | sprintf (enc->cur, "\\u%04x\\u%04x", |
192 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
221 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
193 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
222 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
194 | enc->cur += 12; |
223 | enc->cur += 12; |
… | |
… | |
205 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
234 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
206 | } |
235 | } |
207 | |
236 | |
208 | str += clen; |
237 | str += clen; |
209 | } |
238 | } |
210 | else if (enc->flags & F_LATIN1) |
239 | else if (enc->json.flags & F_LATIN1) |
211 | { |
240 | { |
212 | *enc->cur++ = uch; |
241 | *enc->cur++ = uch; |
213 | str += clen; |
242 | str += clen; |
214 | } |
243 | } |
215 | else if (is_utf8) |
244 | else if (is_utf8) |
… | |
… | |
233 | |
262 | |
234 | --len; |
263 | --len; |
235 | } |
264 | } |
236 | } |
265 | } |
237 | |
266 | |
238 | static void |
267 | INLINE void |
239 | encode_indent (enc_t *enc) |
268 | encode_indent (enc_t *enc) |
240 | { |
269 | { |
241 | if (enc->flags & F_INDENT) |
270 | if (enc->json.flags & F_INDENT) |
242 | { |
271 | { |
243 | int spaces = enc->indent * INDENT_STEP; |
272 | int spaces = enc->indent * INDENT_STEP; |
244 | |
273 | |
245 | need (enc, spaces); |
274 | need (enc, spaces); |
246 | memset (enc->cur, ' ', spaces); |
275 | memset (enc->cur, ' ', spaces); |
247 | enc->cur += spaces; |
276 | enc->cur += spaces; |
248 | } |
277 | } |
249 | } |
278 | } |
250 | |
279 | |
251 | static void |
280 | INLINE void |
252 | encode_space (enc_t *enc) |
281 | encode_space (enc_t *enc) |
253 | { |
282 | { |
254 | need (enc, 1); |
283 | need (enc, 1); |
255 | encode_ch (enc, ' '); |
284 | encode_ch (enc, ' '); |
256 | } |
285 | } |
257 | |
286 | |
258 | static void |
287 | INLINE void |
259 | encode_nl (enc_t *enc) |
288 | encode_nl (enc_t *enc) |
260 | { |
289 | { |
261 | if (enc->flags & F_INDENT) |
290 | if (enc->json.flags & F_INDENT) |
262 | { |
291 | { |
263 | need (enc, 1); |
292 | need (enc, 1); |
264 | encode_ch (enc, '\n'); |
293 | encode_ch (enc, '\n'); |
265 | } |
294 | } |
266 | } |
295 | } |
267 | |
296 | |
268 | static void |
297 | INLINE void |
269 | encode_comma (enc_t *enc) |
298 | encode_comma (enc_t *enc) |
270 | { |
299 | { |
271 | encode_ch (enc, ','); |
300 | encode_ch (enc, ','); |
272 | |
301 | |
273 | if (enc->flags & F_INDENT) |
302 | if (enc->json.flags & F_INDENT) |
274 | encode_nl (enc); |
303 | encode_nl (enc); |
275 | else if (enc->flags & F_SPACE_AFTER) |
304 | else if (enc->json.flags & F_SPACE_AFTER) |
276 | encode_space (enc); |
305 | encode_space (enc); |
277 | } |
306 | } |
278 | |
307 | |
279 | static void encode_sv (enc_t *enc, SV *sv); |
308 | static void encode_sv (enc_t *enc, SV *sv); |
280 | |
309 | |
… | |
… | |
284 | int i, len = av_len (av); |
313 | int i, len = av_len (av); |
285 | |
314 | |
286 | if (enc->indent >= enc->maxdepth) |
315 | if (enc->indent >= enc->maxdepth) |
287 | croak ("data structure too deep (hit recursion limit)"); |
316 | croak ("data structure too deep (hit recursion limit)"); |
288 | |
317 | |
289 | encode_ch (enc, '['); encode_nl (enc); |
318 | encode_ch (enc, '['); |
290 | ++enc->indent; |
319 | |
|
|
320 | if (len >= 0) |
|
|
321 | { |
|
|
322 | encode_nl (enc); ++enc->indent; |
291 | |
323 | |
292 | for (i = 0; i <= len; ++i) |
324 | for (i = 0; i <= len; ++i) |
293 | { |
325 | { |
|
|
326 | SV **svp = av_fetch (av, i, 0); |
|
|
327 | |
294 | encode_indent (enc); |
328 | encode_indent (enc); |
295 | encode_sv (enc, *av_fetch (av, i, 0)); |
|
|
296 | |
329 | |
|
|
330 | if (svp) |
|
|
331 | encode_sv (enc, *svp); |
|
|
332 | else |
|
|
333 | encode_str (enc, "null", 4, 0); |
|
|
334 | |
297 | if (i < len) |
335 | if (i < len) |
298 | encode_comma (enc); |
336 | encode_comma (enc); |
299 | } |
337 | } |
300 | |
338 | |
|
|
339 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
340 | } |
|
|
341 | |
301 | encode_nl (enc); |
342 | encode_ch (enc, ']'); |
302 | |
|
|
303 | --enc->indent; |
|
|
304 | encode_indent (enc); encode_ch (enc, ']'); |
|
|
305 | } |
343 | } |
306 | |
344 | |
307 | static void |
345 | static void |
308 | encode_he (enc_t *enc, HE *he) |
346 | encode_hk (enc_t *enc, HE *he) |
309 | { |
347 | { |
310 | encode_ch (enc, '"'); |
348 | encode_ch (enc, '"'); |
311 | |
349 | |
312 | if (HeKLEN (he) == HEf_SVKEY) |
350 | if (HeKLEN (he) == HEf_SVKEY) |
313 | { |
351 | { |
… | |
… | |
323 | else |
361 | else |
324 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
362 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
325 | |
363 | |
326 | encode_ch (enc, '"'); |
364 | encode_ch (enc, '"'); |
327 | |
365 | |
328 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
366 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
329 | encode_ch (enc, ':'); |
367 | encode_ch (enc, ':'); |
330 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
368 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
331 | encode_sv (enc, HeVAL (he)); |
|
|
332 | } |
369 | } |
333 | |
370 | |
334 | // compare hash entries, used when all keys are bytestrings |
371 | // compare hash entries, used when all keys are bytestrings |
335 | static int |
372 | static int |
336 | he_cmp_fast (const void *a_, const void *b_) |
373 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
341 | HE *b = *(HE **)b_; |
378 | HE *b = *(HE **)b_; |
342 | |
379 | |
343 | STRLEN la = HeKLEN (a); |
380 | STRLEN la = HeKLEN (a); |
344 | STRLEN lb = HeKLEN (b); |
381 | STRLEN lb = HeKLEN (b); |
345 | |
382 | |
346 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
383 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
347 | cmp = la - lb; |
384 | cmp = lb - la; |
348 | |
385 | |
349 | return cmp; |
386 | return cmp; |
350 | } |
387 | } |
351 | |
388 | |
352 | // compare hash entries, used when some keys are sv's or utf-x |
389 | // compare hash entries, used when some keys are sv's or utf-x |
353 | static int |
390 | static int |
354 | he_cmp_slow (const void *a, const void *b) |
391 | he_cmp_slow (const void *a, const void *b) |
355 | { |
392 | { |
356 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
393 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
357 | } |
394 | } |
358 | |
395 | |
359 | static void |
396 | static void |
360 | encode_hv (enc_t *enc, HV *hv) |
397 | encode_hv (enc_t *enc, HV *hv) |
361 | { |
398 | { |
|
|
399 | HE *he; |
362 | int count, i; |
400 | int count; |
363 | |
401 | |
364 | if (enc->indent >= enc->maxdepth) |
402 | if (enc->indent >= enc->maxdepth) |
365 | croak ("data structure too deep (hit recursion limit)"); |
403 | croak ("data structure too deep (hit recursion limit)"); |
366 | |
404 | |
367 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
405 | encode_ch (enc, '{'); |
368 | |
406 | |
369 | if ((count = hv_iterinit (hv))) |
|
|
370 | { |
|
|
371 | // for canonical output we have to sort by keys first |
407 | // for canonical output we have to sort by keys first |
372 | // actually, this is mostly due to the stupid so-called |
408 | // actually, this is mostly due to the stupid so-called |
373 | // security workaround added somewhere in 5.8.x. |
409 | // security workaround added somewhere in 5.8.x. |
374 | // that randomises hash orderings |
410 | // that randomises hash orderings |
375 | if (enc->flags & F_CANONICAL) |
411 | if (enc->json.flags & F_CANONICAL) |
|
|
412 | { |
|
|
413 | int count = hv_iterinit (hv); |
|
|
414 | |
|
|
415 | if (SvMAGICAL (hv)) |
376 | { |
416 | { |
|
|
417 | // need to count by iterating. could improve by dynamically building the vector below |
|
|
418 | // but I don't care for the speed of this special case. |
|
|
419 | // note also that we will run into undefined behaviour when the two iterations |
|
|
420 | // do not result in the same count, something I might care for in some later release. |
|
|
421 | |
|
|
422 | count = 0; |
|
|
423 | while (hv_iternext (hv)) |
|
|
424 | ++count; |
|
|
425 | |
|
|
426 | hv_iterinit (hv); |
|
|
427 | } |
|
|
428 | |
|
|
429 | if (count) |
|
|
430 | { |
377 | int fast = 1; |
431 | int i, fast = 1; |
378 | HE *he; |
432 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
379 | #if WIN32 |
|
|
380 | HE **hes = _alloca (count * sizeof (HE)); |
433 | HE **hes = _alloca (count * sizeof (HE)); |
381 | #else |
434 | #else |
382 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
435 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
383 | #endif |
436 | #endif |
384 | |
437 | |
… | |
… | |
410 | |
463 | |
411 | FREETMPS; |
464 | FREETMPS; |
412 | LEAVE; |
465 | LEAVE; |
413 | } |
466 | } |
414 | |
467 | |
415 | for (i = 0; i < count; ++i) |
468 | encode_nl (enc); ++enc->indent; |
|
|
469 | |
|
|
470 | while (count--) |
416 | { |
471 | { |
417 | encode_indent (enc); |
472 | encode_indent (enc); |
|
|
473 | he = hes [count]; |
418 | encode_he (enc, hes [i]); |
474 | encode_hk (enc, he); |
|
|
475 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
419 | |
476 | |
420 | if (i < count - 1) |
477 | if (count) |
421 | encode_comma (enc); |
478 | encode_comma (enc); |
422 | } |
479 | } |
423 | |
480 | |
|
|
481 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
482 | } |
|
|
483 | } |
|
|
484 | else |
|
|
485 | { |
|
|
486 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
|
|
487 | if ((he = hv_iternext (hv))) |
|
|
488 | { |
|
|
489 | encode_nl (enc); ++enc->indent; |
|
|
490 | |
|
|
491 | for (;;) |
|
|
492 | { |
424 | encode_nl (enc); |
493 | encode_indent (enc); |
|
|
494 | encode_hk (enc, he); |
|
|
495 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
|
|
496 | |
|
|
497 | if (!(he = hv_iternext (hv))) |
|
|
498 | break; |
|
|
499 | |
|
|
500 | encode_comma (enc); |
|
|
501 | } |
|
|
502 | |
|
|
503 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
504 | } |
|
|
505 | } |
|
|
506 | |
|
|
507 | encode_ch (enc, '}'); |
|
|
508 | } |
|
|
509 | |
|
|
510 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
511 | static void |
|
|
512 | encode_rv (enc_t *enc, SV *sv) |
|
|
513 | { |
|
|
514 | svtype svt; |
|
|
515 | |
|
|
516 | SvGETMAGIC (sv); |
|
|
517 | svt = SvTYPE (sv); |
|
|
518 | |
|
|
519 | if (expect_false (SvOBJECT (sv))) |
|
|
520 | { |
|
|
521 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
522 | ? json_boolean_stash |
|
|
523 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
524 | |
|
|
525 | if (SvSTASH (sv) == stash) |
|
|
526 | { |
|
|
527 | if (SvIV (sv)) |
|
|
528 | encode_str (enc, "true", 4, 0); |
|
|
529 | else |
|
|
530 | encode_str (enc, "false", 5, 0); |
425 | } |
531 | } |
426 | else |
532 | else |
427 | { |
533 | { |
428 | HE *he = hv_iternext (hv); |
534 | #if 0 |
429 | |
535 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
430 | for (;;) |
|
|
431 | { |
536 | { |
432 | encode_indent (enc); |
537 | // not yet |
433 | encode_he (enc, he); |
|
|
434 | |
|
|
435 | if (!(he = hv_iternext (hv))) |
|
|
436 | break; |
|
|
437 | |
|
|
438 | encode_comma (enc); |
|
|
439 | } |
538 | } |
|
|
539 | #endif |
|
|
540 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
541 | { |
|
|
542 | // we re-bless the reference to get overload and other niceties right |
|
|
543 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
440 | |
544 | |
|
|
545 | if (to_json) |
|
|
546 | { |
|
|
547 | dSP; |
|
|
548 | |
|
|
549 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
550 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
551 | |
|
|
552 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
553 | PUTBACK; |
|
|
554 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
555 | SPAGAIN; |
|
|
556 | |
|
|
557 | // catch this surprisingly common error |
|
|
558 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
559 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
560 | |
|
|
561 | sv = POPs; |
|
|
562 | PUTBACK; |
|
|
563 | |
441 | encode_nl (enc); |
564 | encode_sv (enc, sv); |
|
|
565 | |
|
|
566 | FREETMPS; LEAVE; |
|
|
567 | } |
|
|
568 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
569 | encode_str (enc, "null", 4, 0); |
|
|
570 | else |
|
|
571 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
572 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
573 | } |
|
|
574 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
575 | encode_str (enc, "null", 4, 0); |
|
|
576 | else |
|
|
577 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
578 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
442 | } |
579 | } |
443 | } |
580 | } |
444 | |
|
|
445 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
446 | } |
|
|
447 | |
|
|
448 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
449 | static void |
|
|
450 | encode_rv (enc_t *enc, SV *sv) |
|
|
451 | { |
|
|
452 | svtype svt; |
|
|
453 | |
|
|
454 | SvGETMAGIC (sv); |
|
|
455 | svt = SvTYPE (sv); |
|
|
456 | |
|
|
457 | if (svt == SVt_PVHV) |
581 | else if (svt == SVt_PVHV) |
458 | encode_hv (enc, (HV *)sv); |
582 | encode_hv (enc, (HV *)sv); |
459 | else if (svt == SVt_PVAV) |
583 | else if (svt == SVt_PVAV) |
460 | encode_av (enc, (AV *)sv); |
584 | encode_av (enc, (AV *)sv); |
461 | else if (svt < SVt_PVAV) |
585 | else if (svt < SVt_PVAV) |
462 | { |
586 | { |
463 | if (SvNIOK (sv) && SvIV (sv) == 0) |
587 | STRLEN len = 0; |
|
|
588 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
589 | |
|
|
590 | if (len == 1 && *pv == '1') |
|
|
591 | encode_str (enc, "true", 4, 0); |
|
|
592 | else if (len == 1 && *pv == '0') |
464 | encode_str (enc, "false", 5, 0); |
593 | encode_str (enc, "false", 5, 0); |
465 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
466 | encode_str (enc, "true", 4, 0); |
|
|
467 | else |
594 | else |
468 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
595 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
469 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
596 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
470 | } |
597 | } |
471 | else |
598 | else |
… | |
… | |
486 | encode_str (enc, str, len, SvUTF8 (sv)); |
613 | encode_str (enc, str, len, SvUTF8 (sv)); |
487 | encode_ch (enc, '"'); |
614 | encode_ch (enc, '"'); |
488 | } |
615 | } |
489 | else if (SvNOKp (sv)) |
616 | else if (SvNOKp (sv)) |
490 | { |
617 | { |
|
|
618 | // trust that perl will do the right thing w.r.t. JSON syntax. |
491 | need (enc, NV_DIG + 32); |
619 | need (enc, NV_DIG + 32); |
492 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
620 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
493 | enc->cur += strlen (enc->cur); |
621 | enc->cur += strlen (enc->cur); |
494 | } |
622 | } |
495 | else if (SvIOKp (sv)) |
623 | else if (SvIOKp (sv)) |
496 | { |
624 | { |
497 | need (enc, 64); |
625 | // we assume we can always read an IV as a UV |
|
|
626 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
627 | { |
|
|
628 | // large integer, use the (rather slow) snprintf way. |
|
|
629 | need (enc, sizeof (UV) * 3); |
498 | enc->cur += |
630 | enc->cur += |
499 | SvIsUV(sv) |
631 | SvIsUV(sv) |
500 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
632 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
501 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
633 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
634 | } |
|
|
635 | else |
|
|
636 | { |
|
|
637 | // optimise the "small number case" |
|
|
638 | // code will likely be branchless and use only a single multiplication |
|
|
639 | I32 i = SvIV (sv); |
|
|
640 | U32 u; |
|
|
641 | char digit, nz = 0; |
|
|
642 | |
|
|
643 | need (enc, 6); |
|
|
644 | |
|
|
645 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
646 | u = i < 0 ? -i : i; |
|
|
647 | |
|
|
648 | // convert to 4.28 fixed-point representation |
|
|
649 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
650 | |
|
|
651 | // now output digit by digit, each time masking out the integer part |
|
|
652 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
653 | // resulting in a net multiplication by 10. |
|
|
654 | // we always write the digit to memory but conditionally increment |
|
|
655 | // the pointer, to ease the usage of conditional move instructions. |
|
|
656 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
657 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
658 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
659 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
660 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
661 | } |
502 | } |
662 | } |
503 | else if (SvROK (sv)) |
663 | else if (SvROK (sv)) |
504 | encode_rv (enc, SvRV (sv)); |
664 | encode_rv (enc, SvRV (sv)); |
505 | else if (!SvOK (sv)) |
665 | else if (!SvOK (sv)) |
506 | encode_str (enc, "null", 4, 0); |
666 | encode_str (enc, "null", 4, 0); |
… | |
… | |
508 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
668 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
509 | SvPV_nolen (sv), SvFLAGS (sv)); |
669 | SvPV_nolen (sv), SvFLAGS (sv)); |
510 | } |
670 | } |
511 | |
671 | |
512 | static SV * |
672 | static SV * |
513 | encode_json (SV *scalar, U32 flags) |
673 | encode_json (SV *scalar, JSON *json) |
514 | { |
674 | { |
515 | enc_t enc; |
675 | enc_t enc; |
516 | |
676 | |
517 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
677 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
518 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
678 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
519 | |
679 | |
520 | enc.flags = flags; |
680 | enc.json = *json; |
521 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
681 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
522 | enc.cur = SvPVX (enc.sv); |
682 | enc.cur = SvPVX (enc.sv); |
523 | enc.end = SvEND (enc.sv); |
683 | enc.end = SvEND (enc.sv); |
524 | enc.indent = 0; |
684 | enc.indent = 0; |
525 | enc.maxdepth = DEC_DEPTH (flags); |
685 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
|
|
686 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
|
|
687 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
|
|
688 | : 0x10FFFFUL; |
526 | |
689 | |
527 | SvPOK_only (enc.sv); |
690 | SvPOK_only (enc.sv); |
528 | encode_sv (&enc, scalar); |
691 | encode_sv (&enc, scalar); |
529 | |
692 | |
530 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
693 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
531 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
694 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
532 | |
695 | |
533 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
696 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
534 | SvUTF8_on (enc.sv); |
697 | SvUTF8_on (enc.sv); |
535 | |
698 | |
536 | if (enc.flags & F_SHRINK) |
699 | if (enc.json.flags & F_SHRINK) |
537 | shrink (enc.sv); |
700 | shrink (enc.sv); |
538 | |
701 | |
539 | return enc.sv; |
702 | return enc.sv; |
540 | } |
703 | } |
541 | |
704 | |
… | |
… | |
546 | typedef struct |
709 | typedef struct |
547 | { |
710 | { |
548 | char *cur; // current parser pointer |
711 | char *cur; // current parser pointer |
549 | char *end; // end of input string |
712 | char *end; // end of input string |
550 | const char *err; // parse error, if != 0 |
713 | const char *err; // parse error, if != 0 |
551 | U32 flags; // F_* |
714 | JSON json; |
552 | U32 depth; // recursion depth |
715 | U32 depth; // recursion depth |
553 | U32 maxdepth; // recursion depth limit |
716 | U32 maxdepth; // recursion depth limit |
554 | } dec_t; |
717 | } dec_t; |
555 | |
718 | |
556 | static void |
719 | INLINE void |
|
|
720 | decode_comment (dec_t *dec) |
|
|
721 | { |
|
|
722 | // only '#'-style comments allowed a.t.m. |
|
|
723 | |
|
|
724 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
|
|
725 | ++dec->cur; |
|
|
726 | } |
|
|
727 | |
|
|
728 | INLINE void |
557 | decode_ws (dec_t *dec) |
729 | decode_ws (dec_t *dec) |
558 | { |
730 | { |
559 | for (;;) |
731 | for (;;) |
560 | { |
732 | { |
561 | char ch = *dec->cur; |
733 | char ch = *dec->cur; |
562 | |
734 | |
563 | if (ch > 0x20 |
735 | if (ch > 0x20) |
|
|
736 | { |
|
|
737 | if (expect_false (ch == '#')) |
|
|
738 | { |
|
|
739 | if (dec->json.flags & F_RELAXED) |
|
|
740 | decode_comment (dec); |
|
|
741 | else |
|
|
742 | break; |
|
|
743 | } |
|
|
744 | else |
|
|
745 | break; |
|
|
746 | } |
564 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
747 | else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
565 | break; |
748 | break; // parse error, but let higher level handle it, gives better error messages |
566 | |
749 | |
567 | ++dec->cur; |
750 | ++dec->cur; |
568 | } |
751 | } |
569 | } |
752 | } |
570 | |
753 | |
… | |
… | |
587 | decode_4hex (dec_t *dec) |
770 | decode_4hex (dec_t *dec) |
588 | { |
771 | { |
589 | signed char d1, d2, d3, d4; |
772 | signed char d1, d2, d3, d4; |
590 | unsigned char *cur = (unsigned char *)dec->cur; |
773 | unsigned char *cur = (unsigned char *)dec->cur; |
591 | |
774 | |
592 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
775 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
593 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
776 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
594 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
777 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
595 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
778 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
596 | |
779 | |
597 | dec->cur += 4; |
780 | dec->cur += 4; |
598 | |
781 | |
599 | return ((UV)d1) << 12 |
782 | return ((UV)d1) << 12 |
600 | | ((UV)d2) << 8 |
783 | | ((UV)d2) << 8 |
… | |
… | |
608 | static SV * |
791 | static SV * |
609 | decode_str (dec_t *dec) |
792 | decode_str (dec_t *dec) |
610 | { |
793 | { |
611 | SV *sv = 0; |
794 | SV *sv = 0; |
612 | int utf8 = 0; |
795 | int utf8 = 0; |
|
|
796 | char *dec_cur = dec->cur; |
613 | |
797 | |
614 | do |
798 | do |
615 | { |
799 | { |
616 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
800 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
617 | char *cur = buf; |
801 | char *cur = buf; |
618 | |
802 | |
619 | do |
803 | do |
620 | { |
804 | { |
621 | unsigned char ch = *(unsigned char *)dec->cur++; |
805 | unsigned char ch = *(unsigned char *)dec_cur++; |
622 | |
806 | |
623 | if (ch == '"') |
807 | if (expect_false (ch == '"')) |
624 | { |
808 | { |
625 | --dec->cur; |
809 | --dec_cur; |
626 | break; |
810 | break; |
627 | } |
811 | } |
628 | else if (ch == '\\') |
812 | else if (expect_false (ch == '\\')) |
629 | { |
813 | { |
630 | switch (*dec->cur) |
814 | switch (*dec_cur) |
631 | { |
815 | { |
632 | case '\\': |
816 | case '\\': |
633 | case '/': |
817 | case '/': |
634 | case '"': *cur++ = *dec->cur++; break; |
818 | case '"': *cur++ = *dec_cur++; break; |
635 | |
819 | |
636 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
820 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
637 | case 't': ++dec->cur; *cur++ = '\011'; break; |
821 | case 't': ++dec_cur; *cur++ = '\011'; break; |
638 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
822 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
639 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
823 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
640 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
824 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
641 | |
825 | |
642 | case 'u': |
826 | case 'u': |
643 | { |
827 | { |
644 | UV lo, hi; |
828 | UV lo, hi; |
645 | ++dec->cur; |
829 | ++dec_cur; |
646 | |
830 | |
|
|
831 | dec->cur = dec_cur; |
647 | hi = decode_4hex (dec); |
832 | hi = decode_4hex (dec); |
|
|
833 | dec_cur = dec->cur; |
648 | if (hi == (UV)-1) |
834 | if (hi == (UV)-1) |
649 | goto fail; |
835 | goto fail; |
650 | |
836 | |
651 | // possibly a surrogate pair |
837 | // possibly a surrogate pair |
652 | if (hi >= 0xd800) |
838 | if (hi >= 0xd800) |
653 | if (hi < 0xdc00) |
839 | if (hi < 0xdc00) |
654 | { |
840 | { |
655 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
841 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
656 | ERR ("missing low surrogate character in surrogate pair"); |
842 | ERR ("missing low surrogate character in surrogate pair"); |
657 | |
843 | |
658 | dec->cur += 2; |
844 | dec_cur += 2; |
659 | |
845 | |
|
|
846 | dec->cur = dec_cur; |
660 | lo = decode_4hex (dec); |
847 | lo = decode_4hex (dec); |
|
|
848 | dec_cur = dec->cur; |
661 | if (lo == (UV)-1) |
849 | if (lo == (UV)-1) |
662 | goto fail; |
850 | goto fail; |
663 | |
851 | |
664 | if (lo < 0xdc00 || lo >= 0xe000) |
852 | if (lo < 0xdc00 || lo >= 0xe000) |
665 | ERR ("surrogate pair expected"); |
853 | ERR ("surrogate pair expected"); |
… | |
… | |
679 | *cur++ = hi; |
867 | *cur++ = hi; |
680 | } |
868 | } |
681 | break; |
869 | break; |
682 | |
870 | |
683 | default: |
871 | default: |
684 | --dec->cur; |
872 | --dec_cur; |
685 | ERR ("illegal backslash escape sequence in string"); |
873 | ERR ("illegal backslash escape sequence in string"); |
686 | } |
874 | } |
687 | } |
875 | } |
688 | else if (ch >= 0x20 && ch <= 0x7f) |
876 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
689 | *cur++ = ch; |
877 | *cur++ = ch; |
690 | else if (ch >= 0x80) |
878 | else if (ch >= 0x80) |
691 | { |
879 | { |
692 | STRLEN clen; |
880 | STRLEN clen; |
693 | UV uch; |
881 | UV uch; |
694 | |
882 | |
695 | --dec->cur; |
883 | --dec_cur; |
696 | |
884 | |
697 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
885 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
698 | if (clen == (STRLEN)-1) |
886 | if (clen == (STRLEN)-1) |
699 | ERR ("malformed UTF-8 character in JSON string"); |
887 | ERR ("malformed UTF-8 character in JSON string"); |
700 | |
888 | |
701 | do |
889 | do |
702 | *cur++ = *dec->cur++; |
890 | *cur++ = *dec_cur++; |
703 | while (--clen); |
891 | while (--clen); |
704 | |
892 | |
705 | utf8 = 1; |
893 | utf8 = 1; |
706 | } |
894 | } |
707 | else |
895 | else |
708 | { |
896 | { |
709 | --dec->cur; |
897 | --dec_cur; |
710 | |
898 | |
711 | if (!ch) |
899 | if (!ch) |
712 | ERR ("unexpected end of string while parsing JSON string"); |
900 | ERR ("unexpected end of string while parsing JSON string"); |
713 | else |
901 | else |
714 | ERR ("invalid character encountered while parsing JSON string"); |
902 | ERR ("invalid character encountered while parsing JSON string"); |
… | |
… | |
727 | } |
915 | } |
728 | else |
916 | else |
729 | sv = newSVpvn (buf, len); |
917 | sv = newSVpvn (buf, len); |
730 | } |
918 | } |
731 | } |
919 | } |
732 | while (*dec->cur != '"'); |
920 | while (*dec_cur != '"'); |
733 | |
921 | |
734 | ++dec->cur; |
922 | ++dec_cur; |
735 | |
923 | |
736 | if (sv) |
924 | if (sv) |
737 | { |
925 | { |
738 | SvPOK_only (sv); |
926 | SvPOK_only (sv); |
739 | *SvEND (sv) = 0; |
927 | *SvEND (sv) = 0; |
… | |
… | |
742 | SvUTF8_on (sv); |
930 | SvUTF8_on (sv); |
743 | } |
931 | } |
744 | else |
932 | else |
745 | sv = newSVpvn ("", 0); |
933 | sv = newSVpvn ("", 0); |
746 | |
934 | |
|
|
935 | dec->cur = dec_cur; |
747 | return sv; |
936 | return sv; |
748 | |
937 | |
749 | fail: |
938 | fail: |
|
|
939 | dec->cur = dec_cur; |
750 | return 0; |
940 | return 0; |
751 | } |
941 | } |
752 | |
942 | |
753 | static SV * |
943 | static SV * |
754 | decode_num (dec_t *dec) |
944 | decode_num (dec_t *dec) |
… | |
… | |
812 | is_nv = 1; |
1002 | is_nv = 1; |
813 | } |
1003 | } |
814 | |
1004 | |
815 | if (!is_nv) |
1005 | if (!is_nv) |
816 | { |
1006 | { |
817 | UV uv; |
1007 | int len = dec->cur - start; |
818 | int numtype = grok_number (start, dec->cur - start, &uv); |
1008 | |
819 | if (numtype & IS_NUMBER_IN_UV) |
1009 | // special case the rather common 1..5-digit-int case |
820 | if (numtype & IS_NUMBER_NEG) |
1010 | if (*start == '-') |
|
|
1011 | switch (len) |
821 | { |
1012 | { |
822 | if (uv < (UV)IV_MIN) |
1013 | case 2: return newSViv (-( start [1] - '0' * 1)); |
823 | return newSViv (-(IV)uv); |
1014 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
1015 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
1016 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1017 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
824 | } |
1018 | } |
|
|
1019 | else |
|
|
1020 | switch (len) |
|
|
1021 | { |
|
|
1022 | case 1: return newSViv ( start [0] - '0' * 1); |
|
|
1023 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
1024 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
1025 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1026 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
|
|
1027 | } |
|
|
1028 | |
|
|
1029 | { |
|
|
1030 | UV uv; |
|
|
1031 | int numtype = grok_number (start, len, &uv); |
|
|
1032 | if (numtype & IS_NUMBER_IN_UV) |
|
|
1033 | if (numtype & IS_NUMBER_NEG) |
|
|
1034 | { |
|
|
1035 | if (uv < (UV)IV_MIN) |
|
|
1036 | return newSViv (-(IV)uv); |
|
|
1037 | } |
825 | else |
1038 | else |
826 | return newSVuv (uv); |
1039 | return newSVuv (uv); |
827 | } |
1040 | } |
828 | |
1041 | |
|
|
1042 | len -= *start == '-' ? 1 : 0; |
|
|
1043 | |
|
|
1044 | // does not fit into IV or UV, try NV |
|
|
1045 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
1046 | #if defined (LDBL_DIG) |
|
|
1047 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1048 | #endif |
|
|
1049 | ) |
|
|
1050 | // fits into NV without loss of precision |
|
|
1051 | return newSVnv (Atof (start)); |
|
|
1052 | |
|
|
1053 | // everything else fails, convert it to a string |
|
|
1054 | return newSVpvn (start, dec->cur - start); |
|
|
1055 | } |
|
|
1056 | |
|
|
1057 | // loss of precision here |
829 | return newSVnv (Atof (start)); |
1058 | return newSVnv (Atof (start)); |
830 | |
1059 | |
831 | fail: |
1060 | fail: |
832 | return 0; |
1061 | return 0; |
833 | } |
1062 | } |
… | |
… | |
863 | |
1092 | |
864 | if (*dec->cur != ',') |
1093 | if (*dec->cur != ',') |
865 | ERR (", or ] expected while parsing array"); |
1094 | ERR (", or ] expected while parsing array"); |
866 | |
1095 | |
867 | ++dec->cur; |
1096 | ++dec->cur; |
|
|
1097 | |
|
|
1098 | decode_ws (dec); |
|
|
1099 | |
|
|
1100 | if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
|
|
1101 | { |
|
|
1102 | ++dec->cur; |
|
|
1103 | break; |
|
|
1104 | } |
868 | } |
1105 | } |
869 | |
1106 | |
870 | DEC_DEC_DEPTH; |
1107 | DEC_DEC_DEPTH; |
871 | return newRV_noinc ((SV *)av); |
1108 | return newRV_noinc ((SV *)av); |
872 | |
1109 | |
… | |
… | |
877 | } |
1114 | } |
878 | |
1115 | |
879 | static SV * |
1116 | static SV * |
880 | decode_hv (dec_t *dec) |
1117 | decode_hv (dec_t *dec) |
881 | { |
1118 | { |
|
|
1119 | SV *sv; |
882 | HV *hv = newHV (); |
1120 | HV *hv = newHV (); |
883 | |
1121 | |
884 | DEC_INC_DEPTH; |
1122 | DEC_INC_DEPTH; |
885 | decode_ws (dec); |
1123 | decode_ws (dec); |
886 | |
1124 | |
887 | if (*dec->cur == '}') |
1125 | if (*dec->cur == '}') |
888 | ++dec->cur; |
1126 | ++dec->cur; |
889 | else |
1127 | else |
890 | for (;;) |
1128 | for (;;) |
891 | { |
1129 | { |
|
|
1130 | EXPECT_CH ('"'); |
|
|
1131 | |
|
|
1132 | // heuristic: assume that |
|
|
1133 | // a) decode_str + hv_store_ent are abysmally slow. |
|
|
1134 | // b) most hash keys are short, simple ascii text. |
|
|
1135 | // => try to "fast-match" such strings to avoid |
|
|
1136 | // the overhead of decode_str + hv_store_ent. |
|
|
1137 | { |
892 | SV *key, *value; |
1138 | SV *value; |
|
|
1139 | char *p = dec->cur; |
|
|
1140 | char *e = p + 24; // only try up to 24 bytes |
893 | |
1141 | |
894 | decode_ws (dec); EXPECT_CH ('"'); |
1142 | for (;;) |
895 | |
|
|
896 | key = decode_str (dec); |
|
|
897 | if (!key) |
|
|
898 | goto fail; |
|
|
899 | |
|
|
900 | decode_ws (dec); EXPECT_CH (':'); |
|
|
901 | |
|
|
902 | value = decode_sv (dec); |
|
|
903 | if (!value) |
|
|
904 | { |
1143 | { |
|
|
1144 | // the >= 0x80 is true on most architectures |
|
|
1145 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1146 | { |
|
|
1147 | // slow path, back up and use decode_str |
|
|
1148 | SV *key = decode_str (dec); |
|
|
1149 | if (!key) |
|
|
1150 | goto fail; |
|
|
1151 | |
|
|
1152 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1153 | |
|
|
1154 | decode_ws (dec); |
|
|
1155 | value = decode_sv (dec); |
|
|
1156 | if (!value) |
|
|
1157 | { |
|
|
1158 | SvREFCNT_dec (key); |
|
|
1159 | goto fail; |
|
|
1160 | } |
|
|
1161 | |
|
|
1162 | hv_store_ent (hv, key, value, 0); |
905 | SvREFCNT_dec (key); |
1163 | SvREFCNT_dec (key); |
|
|
1164 | |
|
|
1165 | break; |
|
|
1166 | } |
|
|
1167 | else if (*p == '"') |
|
|
1168 | { |
|
|
1169 | // fast path, got a simple key |
|
|
1170 | char *key = dec->cur; |
|
|
1171 | int len = p - key; |
|
|
1172 | dec->cur = p + 1; |
|
|
1173 | |
|
|
1174 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1175 | |
|
|
1176 | decode_ws (dec); |
|
|
1177 | value = decode_sv (dec); |
|
|
1178 | if (!value) |
906 | goto fail; |
1179 | goto fail; |
|
|
1180 | |
|
|
1181 | hv_store (hv, key, len, value, 0); |
|
|
1182 | |
|
|
1183 | break; |
|
|
1184 | } |
|
|
1185 | |
|
|
1186 | ++p; |
907 | } |
1187 | } |
908 | |
1188 | } |
909 | hv_store_ent (hv, key, value, 0); |
|
|
910 | SvREFCNT_dec (key); |
|
|
911 | |
1189 | |
912 | decode_ws (dec); |
1190 | decode_ws (dec); |
913 | |
1191 | |
914 | if (*dec->cur == '}') |
1192 | if (*dec->cur == '}') |
915 | { |
1193 | { |
… | |
… | |
919 | |
1197 | |
920 | if (*dec->cur != ',') |
1198 | if (*dec->cur != ',') |
921 | ERR (", or } expected while parsing object/hash"); |
1199 | ERR (", or } expected while parsing object/hash"); |
922 | |
1200 | |
923 | ++dec->cur; |
1201 | ++dec->cur; |
|
|
1202 | |
|
|
1203 | decode_ws (dec); |
|
|
1204 | |
|
|
1205 | if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
|
|
1206 | { |
|
|
1207 | ++dec->cur; |
|
|
1208 | break; |
|
|
1209 | } |
924 | } |
1210 | } |
925 | |
1211 | |
926 | DEC_DEC_DEPTH; |
1212 | DEC_DEC_DEPTH; |
927 | return newRV_noinc ((SV *)hv); |
1213 | sv = newRV_noinc ((SV *)hv); |
|
|
1214 | |
|
|
1215 | // check filter callbacks |
|
|
1216 | if (dec->json.flags & F_HOOK) |
|
|
1217 | { |
|
|
1218 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1219 | { |
|
|
1220 | HE *cb, *he; |
|
|
1221 | |
|
|
1222 | hv_iterinit (hv); |
|
|
1223 | he = hv_iternext (hv); |
|
|
1224 | hv_iterinit (hv); |
|
|
1225 | |
|
|
1226 | // the next line creates a mortal sv each time its called. |
|
|
1227 | // might want to optimise this for common cases. |
|
|
1228 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1229 | |
|
|
1230 | if (cb) |
|
|
1231 | { |
|
|
1232 | dSP; |
|
|
1233 | int count; |
|
|
1234 | |
|
|
1235 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1236 | XPUSHs (HeVAL (he)); |
|
|
1237 | |
|
|
1238 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1239 | |
|
|
1240 | if (count == 1) |
|
|
1241 | { |
|
|
1242 | sv = newSVsv (POPs); |
|
|
1243 | FREETMPS; LEAVE; |
|
|
1244 | return sv; |
|
|
1245 | } |
|
|
1246 | |
|
|
1247 | FREETMPS; LEAVE; |
|
|
1248 | } |
|
|
1249 | } |
|
|
1250 | |
|
|
1251 | if (dec->json.cb_object) |
|
|
1252 | { |
|
|
1253 | dSP; |
|
|
1254 | int count; |
|
|
1255 | |
|
|
1256 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1257 | XPUSHs (sv_2mortal (sv)); |
|
|
1258 | |
|
|
1259 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1260 | |
|
|
1261 | if (count == 1) |
|
|
1262 | { |
|
|
1263 | sv = newSVsv (POPs); |
|
|
1264 | FREETMPS; LEAVE; |
|
|
1265 | return sv; |
|
|
1266 | } |
|
|
1267 | |
|
|
1268 | SvREFCNT_inc (sv); |
|
|
1269 | FREETMPS; LEAVE; |
|
|
1270 | } |
|
|
1271 | } |
|
|
1272 | |
|
|
1273 | return sv; |
928 | |
1274 | |
929 | fail: |
1275 | fail: |
930 | SvREFCNT_dec (hv); |
1276 | SvREFCNT_dec (hv); |
931 | DEC_DEC_DEPTH; |
1277 | DEC_DEC_DEPTH; |
932 | return 0; |
1278 | return 0; |
933 | } |
1279 | } |
934 | |
1280 | |
935 | static SV * |
1281 | static SV * |
936 | decode_sv (dec_t *dec) |
1282 | decode_sv (dec_t *dec) |
937 | { |
1283 | { |
938 | decode_ws (dec); |
1284 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1285 | // to parse anything. |
939 | switch (*dec->cur) |
1286 | switch (*dec->cur) |
940 | { |
1287 | { |
941 | case '"': ++dec->cur; return decode_str (dec); |
1288 | case '"': ++dec->cur; return decode_str (dec); |
942 | case '[': ++dec->cur; return decode_av (dec); |
1289 | case '[': ++dec->cur; return decode_av (dec); |
943 | case '{': ++dec->cur; return decode_hv (dec); |
1290 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
949 | |
1296 | |
950 | case 't': |
1297 | case 't': |
951 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1298 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
952 | { |
1299 | { |
953 | dec->cur += 4; |
1300 | dec->cur += 4; |
954 | return newSViv (1); |
1301 | #if JSON_SLOW |
|
|
1302 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
|
|
1303 | #endif |
|
|
1304 | return SvREFCNT_inc (json_true); |
955 | } |
1305 | } |
956 | else |
1306 | else |
957 | ERR ("'true' expected"); |
1307 | ERR ("'true' expected"); |
958 | |
1308 | |
959 | break; |
1309 | break; |
960 | |
1310 | |
961 | case 'f': |
1311 | case 'f': |
962 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1312 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
963 | { |
1313 | { |
964 | dec->cur += 5; |
1314 | dec->cur += 5; |
965 | return newSViv (0); |
1315 | #if JSON_SLOW |
|
|
1316 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
|
|
1317 | #endif |
|
|
1318 | return SvREFCNT_inc (json_false); |
966 | } |
1319 | } |
967 | else |
1320 | else |
968 | ERR ("'false' expected"); |
1321 | ERR ("'false' expected"); |
969 | |
1322 | |
970 | break; |
1323 | break; |
… | |
… | |
988 | fail: |
1341 | fail: |
989 | return 0; |
1342 | return 0; |
990 | } |
1343 | } |
991 | |
1344 | |
992 | static SV * |
1345 | static SV * |
993 | decode_json (SV *string, U32 flags, UV *offset_return) |
1346 | decode_json (SV *string, JSON *json, UV *offset_return) |
994 | { |
1347 | { |
995 | dec_t dec; |
1348 | dec_t dec; |
996 | UV offset; |
1349 | UV offset; |
997 | SV *sv; |
1350 | SV *sv; |
998 | |
1351 | |
999 | SvGETMAGIC (string); |
1352 | SvGETMAGIC (string); |
1000 | SvUPGRADE (string, SVt_PV); |
1353 | SvUPGRADE (string, SVt_PV); |
1001 | |
1354 | |
|
|
1355 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1356 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1357 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1358 | |
1002 | if (flags & F_UTF8) |
1359 | if (json->flags & F_UTF8) |
1003 | sv_utf8_downgrade (string, 0); |
1360 | sv_utf8_downgrade (string, 0); |
1004 | else |
1361 | else |
1005 | sv_utf8_upgrade (string); |
1362 | sv_utf8_upgrade (string); |
1006 | |
1363 | |
1007 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1364 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1008 | |
1365 | |
1009 | dec.flags = flags; |
1366 | dec.json = *json; |
1010 | dec.cur = SvPVX (string); |
1367 | dec.cur = SvPVX (string); |
1011 | dec.end = SvEND (string); |
1368 | dec.end = SvEND (string); |
1012 | dec.err = 0; |
1369 | dec.err = 0; |
1013 | dec.depth = 0; |
1370 | dec.depth = 0; |
1014 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1371 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
|
|
1372 | |
|
|
1373 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1374 | dec.json.flags |= F_HOOK; |
1015 | |
1375 | |
1016 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1376 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1377 | |
|
|
1378 | decode_ws (&dec); |
1017 | sv = decode_sv (&dec); |
1379 | sv = decode_sv (&dec); |
1018 | |
1380 | |
1019 | if (!(offset_return || !sv)) |
1381 | if (!(offset_return || !sv)) |
1020 | { |
1382 | { |
1021 | // check for trailing garbage |
1383 | // check for trailing garbage |
… | |
… | |
1029 | } |
1391 | } |
1030 | } |
1392 | } |
1031 | |
1393 | |
1032 | if (offset_return || !sv) |
1394 | if (offset_return || !sv) |
1033 | { |
1395 | { |
1034 | offset = dec.flags & F_UTF8 |
1396 | offset = dec.json.flags & F_UTF8 |
1035 | ? dec.cur - SvPVX (string) |
1397 | ? dec.cur - SvPVX (string) |
1036 | : utf8_distance (dec.cur, SvPVX (string)); |
1398 | : utf8_distance (dec.cur, SvPVX (string)); |
1037 | |
1399 | |
1038 | if (offset_return) |
1400 | if (offset_return) |
1039 | *offset_return = offset; |
1401 | *offset_return = offset; |
… | |
… | |
1058 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1420 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1059 | } |
1421 | } |
1060 | |
1422 | |
1061 | sv = sv_2mortal (sv); |
1423 | sv = sv_2mortal (sv); |
1062 | |
1424 | |
1063 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1425 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1064 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1426 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1065 | |
1427 | |
1066 | return sv; |
1428 | return sv; |
1067 | } |
1429 | } |
1068 | |
1430 | |
… | |
… | |
1072 | MODULE = JSON::XS PACKAGE = JSON::XS |
1434 | MODULE = JSON::XS PACKAGE = JSON::XS |
1073 | |
1435 | |
1074 | BOOT: |
1436 | BOOT: |
1075 | { |
1437 | { |
1076 | int i; |
1438 | int i; |
1077 | |
|
|
1078 | memset (decode_hexdigit, 0xff, 256); |
|
|
1079 | |
1439 | |
1080 | for (i = 0; i < 256; ++i) |
1440 | for (i = 0; i < 256; ++i) |
1081 | decode_hexdigit [i] = |
1441 | decode_hexdigit [i] = |
1082 | i >= '0' && i <= '9' ? i - '0' |
1442 | i >= '0' && i <= '9' ? i - '0' |
1083 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1443 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1084 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1444 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1085 | : -1; |
1445 | : -1; |
1086 | |
1446 | |
1087 | json_stash = gv_stashpv ("JSON::XS", 1); |
1447 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1448 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1449 | |
|
|
1450 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1451 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1088 | } |
1452 | } |
1089 | |
1453 | |
1090 | PROTOTYPES: DISABLE |
1454 | PROTOTYPES: DISABLE |
1091 | |
1455 | |
1092 | SV *new (char *dummy) |
1456 | void CLONE (...) |
1093 | CODE: |
1457 | CODE: |
1094 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1458 | json_stash = 0; |
|
|
1459 | json_boolean_stash = 0; |
|
|
1460 | |
|
|
1461 | void new (char *klass) |
|
|
1462 | PPCODE: |
|
|
1463 | { |
|
|
1464 | SV *pv = NEWSV (0, sizeof (JSON)); |
|
|
1465 | SvPOK_only (pv); |
|
|
1466 | Zero (SvPVX (pv), 1, JSON); |
|
|
1467 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1468 | XPUSHs (sv_2mortal (sv_bless ( |
|
|
1469 | newRV_noinc (pv), |
|
|
1470 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
|
|
1471 | ))); |
|
|
1472 | } |
|
|
1473 | |
|
|
1474 | void ascii (JSON *self, int enable = 1) |
|
|
1475 | ALIAS: |
|
|
1476 | ascii = F_ASCII |
|
|
1477 | latin1 = F_LATIN1 |
|
|
1478 | utf8 = F_UTF8 |
|
|
1479 | indent = F_INDENT |
|
|
1480 | canonical = F_CANONICAL |
|
|
1481 | space_before = F_SPACE_BEFORE |
|
|
1482 | space_after = F_SPACE_AFTER |
|
|
1483 | pretty = F_PRETTY |
|
|
1484 | allow_nonref = F_ALLOW_NONREF |
|
|
1485 | shrink = F_SHRINK |
|
|
1486 | allow_blessed = F_ALLOW_BLESSED |
|
|
1487 | convert_blessed = F_CONV_BLESSED |
|
|
1488 | relaxed = F_RELAXED |
|
|
1489 | PPCODE: |
|
|
1490 | { |
|
|
1491 | if (enable) |
|
|
1492 | self->flags |= ix; |
|
|
1493 | else |
|
|
1494 | self->flags &= ~ix; |
|
|
1495 | |
|
|
1496 | XPUSHs (ST (0)); |
|
|
1497 | } |
|
|
1498 | |
|
|
1499 | void get_ascii (JSON *self) |
|
|
1500 | ALIAS: |
|
|
1501 | get_ascii = F_ASCII |
|
|
1502 | get_latin1 = F_LATIN1 |
|
|
1503 | get_utf8 = F_UTF8 |
|
|
1504 | get_indent = F_INDENT |
|
|
1505 | get_canonical = F_CANONICAL |
|
|
1506 | get_space_before = F_SPACE_BEFORE |
|
|
1507 | get_space_after = F_SPACE_AFTER |
|
|
1508 | get_allow_nonref = F_ALLOW_NONREF |
|
|
1509 | get_shrink = F_SHRINK |
|
|
1510 | get_allow_blessed = F_ALLOW_BLESSED |
|
|
1511 | get_convert_blessed = F_CONV_BLESSED |
|
|
1512 | get_relaxed = F_RELAXED |
|
|
1513 | PPCODE: |
|
|
1514 | XPUSHs (boolSV (self->flags & ix)); |
|
|
1515 | |
|
|
1516 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
|
|
1517 | PPCODE: |
|
|
1518 | { |
|
|
1519 | UV log2 = 0; |
|
|
1520 | |
|
|
1521 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1522 | |
|
|
1523 | while ((1UL << log2) < max_depth) |
|
|
1524 | ++log2; |
|
|
1525 | |
|
|
1526 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1527 | |
|
|
1528 | XPUSHs (ST (0)); |
|
|
1529 | } |
|
|
1530 | |
|
|
1531 | U32 get_max_depth (JSON *self) |
|
|
1532 | CODE: |
|
|
1533 | RETVAL = DEC_DEPTH (self->flags); |
1095 | OUTPUT: |
1534 | OUTPUT: |
1096 | RETVAL |
1535 | RETVAL |
1097 | |
1536 | |
1098 | SV *ascii (SV *self, int enable = 1) |
1537 | void max_size (JSON *self, UV max_size = 0) |
1099 | ALIAS: |
1538 | PPCODE: |
1100 | ascii = F_ASCII |
1539 | { |
1101 | latin1 = F_LATIN1 |
1540 | UV log2 = 0; |
1102 | utf8 = F_UTF8 |
1541 | |
1103 | indent = F_INDENT |
1542 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1104 | canonical = F_CANONICAL |
1543 | if (max_size == 1) max_size = 2; |
1105 | space_before = F_SPACE_BEFORE |
1544 | |
1106 | space_after = F_SPACE_AFTER |
1545 | while ((1UL << log2) < max_size) |
1107 | pretty = F_PRETTY |
1546 | ++log2; |
1108 | allow_nonref = F_ALLOW_NONREF |
1547 | |
1109 | shrink = F_SHRINK |
1548 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1549 | |
|
|
1550 | XPUSHs (ST (0)); |
|
|
1551 | } |
|
|
1552 | |
|
|
1553 | int get_max_size (JSON *self) |
1110 | CODE: |
1554 | CODE: |
1111 | { |
1555 | RETVAL = DEC_SIZE (self->flags); |
1112 | UV *uv = SvJSON (self); |
|
|
1113 | if (enable) |
|
|
1114 | *uv |= ix; |
|
|
1115 | else |
|
|
1116 | *uv &= ~ix; |
|
|
1117 | |
|
|
1118 | RETVAL = newSVsv (self); |
|
|
1119 | } |
|
|
1120 | OUTPUT: |
1556 | OUTPUT: |
1121 | RETVAL |
1557 | RETVAL |
1122 | |
1558 | |
1123 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1559 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1124 | CODE: |
|
|
1125 | { |
|
|
1126 | UV *uv = SvJSON (self); |
|
|
1127 | UV log2 = 0; |
|
|
1128 | |
|
|
1129 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1130 | |
|
|
1131 | while ((1UL << log2) < max_depth) |
|
|
1132 | ++log2; |
|
|
1133 | |
|
|
1134 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1135 | |
|
|
1136 | RETVAL = newSVsv (self); |
|
|
1137 | } |
|
|
1138 | OUTPUT: |
|
|
1139 | RETVAL |
|
|
1140 | |
|
|
1141 | void encode (SV *self, SV *scalar) |
|
|
1142 | PPCODE: |
1560 | PPCODE: |
1143 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1561 | { |
|
|
1562 | SvREFCNT_dec (self->cb_object); |
|
|
1563 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1144 | |
1564 | |
1145 | void decode (SV *self, SV *jsonstr) |
1565 | XPUSHs (ST (0)); |
|
|
1566 | } |
|
|
1567 | |
|
|
1568 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1146 | PPCODE: |
1569 | PPCODE: |
|
|
1570 | { |
|
|
1571 | if (!self->cb_sk_object) |
|
|
1572 | self->cb_sk_object = newHV (); |
|
|
1573 | |
|
|
1574 | if (SvOK (cb)) |
|
|
1575 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1576 | else |
|
|
1577 | { |
|
|
1578 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1579 | |
|
|
1580 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1581 | { |
|
|
1582 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1583 | self->cb_sk_object = 0; |
|
|
1584 | } |
|
|
1585 | } |
|
|
1586 | |
|
|
1587 | XPUSHs (ST (0)); |
|
|
1588 | } |
|
|
1589 | |
|
|
1590 | void encode (JSON *self, SV *scalar) |
|
|
1591 | PPCODE: |
|
|
1592 | XPUSHs (encode_json (scalar, self)); |
|
|
1593 | |
|
|
1594 | void decode (JSON *self, SV *jsonstr) |
|
|
1595 | PPCODE: |
1147 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1596 | XPUSHs (decode_json (jsonstr, self, 0)); |
1148 | |
1597 | |
1149 | void decode_prefix (SV *self, SV *jsonstr) |
1598 | void decode_prefix (JSON *self, SV *jsonstr) |
1150 | PPCODE: |
1599 | PPCODE: |
1151 | { |
1600 | { |
1152 | UV offset; |
1601 | UV offset; |
1153 | EXTEND (SP, 2); |
1602 | EXTEND (SP, 2); |
1154 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1603 | PUSHs (decode_json (jsonstr, self, &offset)); |
1155 | PUSHs (sv_2mortal (newSVuv (offset))); |
1604 | PUSHs (sv_2mortal (newSVuv (offset))); |
1156 | } |
1605 | } |
1157 | |
1606 | |
|
|
1607 | void DESTROY (JSON *self) |
|
|
1608 | CODE: |
|
|
1609 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1610 | SvREFCNT_dec (self->cb_object); |
|
|
1611 | |
1158 | PROTOTYPES: ENABLE |
1612 | PROTOTYPES: ENABLE |
1159 | |
1613 | |
1160 | void to_json (SV *scalar) |
1614 | void encode_json (SV *scalar) |
1161 | ALIAS: |
|
|
1162 | objToJson = 0 |
|
|
1163 | PPCODE: |
1615 | PPCODE: |
|
|
1616 | { |
|
|
1617 | JSON json = { F_DEFAULT | F_UTF8 }; |
1164 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1618 | XPUSHs (encode_json (scalar, &json)); |
|
|
1619 | } |
1165 | |
1620 | |
1166 | void from_json (SV *jsonstr) |
1621 | void decode_json (SV *jsonstr) |
1167 | ALIAS: |
|
|
1168 | jsonToObj = 0 |
|
|
1169 | PPCODE: |
1622 | PPCODE: |
|
|
1623 | { |
|
|
1624 | JSON json = { F_DEFAULT | F_UTF8 }; |
1170 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1625 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1626 | } |
1171 | |
1627 | |