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