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> |
|
|
9 | #include <limits.h> |
|
|
10 | #include <float.h> |
8 | |
11 | |
|
|
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
14 | #endif |
|
|
15 | |
|
|
16 | // some old perls do not have this, try to make it work, no |
|
|
17 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
18 | #ifndef UTF8_MAXBYTES |
|
|
19 | # define UTF8_MAXBYTES 13 |
|
|
20 | #endif |
|
|
21 | |
|
|
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
|
|
23 | |
9 | #define F_ASCII 0x00000001UL |
24 | #define F_ASCII 0x00000001UL |
|
|
25 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002UL |
26 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004UL |
27 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008UL |
28 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010UL |
29 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020UL |
30 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080UL |
31 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100UL |
32 | #define F_SHRINK 0x00000200UL |
|
|
33 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
34 | #define F_CONV_BLESSED 0x00000800UL |
17 | #define F_MAXDEPTH 0xf8000000UL |
35 | #define F_RELAXED 0x00001000UL |
18 | #define S_MAXDEPTH 27 |
36 | #define F_ALLOW_UNKNOWN 0x00002000UL |
19 | |
37 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
20 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
21 | |
|
|
22 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
23 | // F_BLESSED? <=> { $__class__$ => } |
|
|
24 | |
38 | |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
26 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
|
|
27 | |
40 | |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
41 | #define INIT_SIZE 32 // initial scalar size to be allocated |
29 | #define INDENT_STEP 3 // spaces per indentation level |
42 | #define INDENT_STEP 3 // spaces per indentation level |
30 | |
43 | |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
32 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
44 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
33 | |
45 | |
34 | #define SB do { |
46 | #define SB do { |
35 | #define SE } while (0) |
47 | #define SE } while (0) |
36 | |
48 | |
|
|
49 | #if __GNUC__ >= 3 |
|
|
50 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
|
|
51 | # define INLINE static inline |
|
|
52 | #else |
|
|
53 | # define expect(expr,value) (expr) |
|
|
54 | # define INLINE static |
|
|
55 | #endif |
|
|
56 | |
|
|
57 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
58 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
59 | |
|
|
60 | #define IN_RANGE_INC(type,val,beg,end) \ |
|
|
61 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
|
|
62 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
|
|
63 | |
|
|
64 | #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
|
|
65 | |
|
|
66 | #ifdef USE_ITHREADS |
|
|
67 | # define JSON_SLOW 1 |
|
|
68 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
69 | #else |
|
|
70 | # define JSON_SLOW 0 |
|
|
71 | # define JSON_STASH json_stash |
|
|
72 | #endif |
|
|
73 | |
37 | static HV *json_stash; // JSON::XS:: |
74 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
75 | static SV *json_true, *json_false; |
|
|
76 | |
|
|
77 | enum { |
|
|
78 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
|
|
79 | INCR_M_STR, // inside string |
|
|
80 | INCR_M_BS, // inside backslash |
|
|
81 | INCR_M_JSON // outside anything, count nesting |
|
|
82 | }; |
|
|
83 | |
|
|
84 | #define INCR_DONE(json) (!(json)->incr_nest && (json)->incr_mode == INCR_M_JSON) |
|
|
85 | |
|
|
86 | typedef struct { |
|
|
87 | U32 flags; |
|
|
88 | U32 max_depth; |
|
|
89 | STRLEN max_size; |
|
|
90 | |
|
|
91 | SV *cb_object; |
|
|
92 | HV *cb_sk_object; |
|
|
93 | |
|
|
94 | // for the incremental parser |
|
|
95 | SV *incr_text; // the source text so far |
|
|
96 | STRLEN incr_pos; // the current offset into the text |
|
|
97 | unsigned char incr_nest; // {[]}-nesting level |
|
|
98 | unsigned char incr_mode; |
|
|
99 | } JSON; |
|
|
100 | |
|
|
101 | INLINE void |
|
|
102 | json_init (JSON *json) |
|
|
103 | { |
|
|
104 | Zero (json, 1, JSON); |
|
|
105 | json->max_depth = 512; |
|
|
106 | } |
38 | |
107 | |
39 | ///////////////////////////////////////////////////////////////////////////// |
108 | ///////////////////////////////////////////////////////////////////////////// |
40 | // utility functions |
109 | // utility functions |
41 | |
110 | |
42 | static UV * |
111 | INLINE SV * |
43 | SvJSON (SV *sv) |
112 | get_bool (const char *name) |
44 | { |
113 | { |
45 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
114 | SV *sv = get_sv (name, 1); |
46 | croak ("object is not of type JSON::XS"); |
|
|
47 | |
115 | |
48 | return &SvUVX (SvRV (sv)); |
116 | SvREADONLY_on (sv); |
49 | } |
117 | SvREADONLY_on (SvRV (sv)); |
50 | |
118 | |
51 | static void |
119 | return sv; |
|
|
120 | } |
|
|
121 | |
|
|
122 | INLINE void |
52 | shrink (SV *sv) |
123 | shrink (SV *sv) |
53 | { |
124 | { |
54 | sv_utf8_downgrade (sv, 1); |
125 | sv_utf8_downgrade (sv, 1); |
|
|
126 | |
55 | if (SvLEN (sv) > SvCUR (sv) + 1) |
127 | if (SvLEN (sv) > SvCUR (sv) + 1) |
56 | { |
128 | { |
57 | #ifdef SvPV_shrink_to_cur |
129 | #ifdef SvPV_shrink_to_cur |
58 | SvPV_shrink_to_cur (sv); |
130 | SvPV_shrink_to_cur (sv); |
59 | #elif defined (SvPV_renew) |
131 | #elif defined (SvPV_renew) |
… | |
… | |
65 | // decode an utf-8 character and return it, or (UV)-1 in |
137 | // decode an utf-8 character and return it, or (UV)-1 in |
66 | // case of an error. |
138 | // case of an error. |
67 | // we special-case "safe" characters from U+80 .. U+7FF, |
139 | // we special-case "safe" characters from U+80 .. U+7FF, |
68 | // but use the very good perl function to parse anything else. |
140 | // but use the very good perl function to parse anything else. |
69 | // note that we never call this function for a ascii codepoints |
141 | // note that we never call this function for a ascii codepoints |
70 | static UV |
142 | INLINE UV |
71 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
143 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
72 | { |
144 | { |
73 | if (s[0] > 0xdf || s[0] < 0xc2) |
145 | if (expect_true (len >= 2 |
74 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
146 | && IN_RANGE_INC (char, s[0], 0xc2, 0xdf) |
75 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
147 | && IN_RANGE_INC (char, s[1], 0x80, 0xbf))) |
76 | { |
148 | { |
77 | *clen = 2; |
149 | *clen = 2; |
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
150 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
79 | } |
151 | } |
80 | else |
152 | else |
81 | { |
153 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
82 | *clen = (STRLEN)-1; |
154 | } |
83 | return (UV)-1; |
155 | |
84 | } |
156 | // likewise for encoding, also never called for ascii codepoints |
|
|
157 | // this function takes advantage of this fact, although current gccs |
|
|
158 | // seem to optimise the check for >= 0x80 away anyways |
|
|
159 | INLINE unsigned char * |
|
|
160 | encode_utf8 (unsigned char *s, UV ch) |
|
|
161 | { |
|
|
162 | if (expect_false (ch < 0x000080)) |
|
|
163 | *s++ = ch; |
|
|
164 | else if (expect_true (ch < 0x000800)) |
|
|
165 | *s++ = 0xc0 | ( ch >> 6), |
|
|
166 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
167 | else if ( ch < 0x010000) |
|
|
168 | *s++ = 0xe0 | ( ch >> 12), |
|
|
169 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
170 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
171 | else if ( ch < 0x110000) |
|
|
172 | *s++ = 0xf0 | ( ch >> 18), |
|
|
173 | *s++ = 0x80 | ((ch >> 12) & 0x3f), |
|
|
174 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
175 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
176 | |
|
|
177 | return s; |
85 | } |
178 | } |
86 | |
179 | |
87 | ///////////////////////////////////////////////////////////////////////////// |
180 | ///////////////////////////////////////////////////////////////////////////// |
88 | // encoder |
181 | // encoder |
89 | |
182 | |
… | |
… | |
91 | typedef struct |
184 | typedef struct |
92 | { |
185 | { |
93 | char *cur; // SvPVX (sv) + current output position |
186 | char *cur; // SvPVX (sv) + current output position |
94 | char *end; // SvEND (sv) |
187 | char *end; // SvEND (sv) |
95 | SV *sv; // result scalar |
188 | SV *sv; // result scalar |
96 | U32 flags; // F_* |
189 | JSON json; |
97 | U32 indent; // indentation level |
190 | U32 indent; // indentation level |
98 | U32 maxdepth; // max. indentation/recursion level |
191 | UV limit; // escape character values >= this value when encoding |
99 | } enc_t; |
192 | } enc_t; |
100 | |
193 | |
101 | static void |
194 | INLINE void |
102 | need (enc_t *enc, STRLEN len) |
195 | need (enc_t *enc, STRLEN len) |
103 | { |
196 | { |
104 | if (enc->cur + len >= enc->end) |
197 | if (expect_false (enc->cur + len >= enc->end)) |
105 | { |
198 | { |
106 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
199 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
107 | SvGROW (enc->sv, cur + len + 1); |
200 | SvGROW (enc->sv, cur + len + 1); |
108 | enc->cur = SvPVX (enc->sv) + cur; |
201 | enc->cur = SvPVX (enc->sv) + cur; |
109 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
202 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
110 | } |
203 | } |
111 | } |
204 | } |
112 | |
205 | |
113 | static void |
206 | INLINE void |
114 | encode_ch (enc_t *enc, char ch) |
207 | encode_ch (enc_t *enc, char ch) |
115 | { |
208 | { |
116 | need (enc, 1); |
209 | need (enc, 1); |
117 | *enc->cur++ = ch; |
210 | *enc->cur++ = ch; |
118 | } |
211 | } |
… | |
… | |
126 | |
219 | |
127 | while (str < end) |
220 | while (str < end) |
128 | { |
221 | { |
129 | unsigned char ch = *(unsigned char *)str; |
222 | unsigned char ch = *(unsigned char *)str; |
130 | |
223 | |
131 | if (ch >= 0x20 && ch < 0x80) // most common case |
224 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
132 | { |
225 | { |
133 | if (ch == '"') // but with slow exceptions |
226 | if (expect_false (ch == '"')) // but with slow exceptions |
134 | { |
227 | { |
135 | need (enc, len += 1); |
228 | need (enc, len += 1); |
136 | *enc->cur++ = '\\'; |
229 | *enc->cur++ = '\\'; |
137 | *enc->cur++ = '"'; |
230 | *enc->cur++ = '"'; |
138 | } |
231 | } |
139 | else if (ch == '\\') |
232 | else if (expect_false (ch == '\\')) |
140 | { |
233 | { |
141 | need (enc, len += 1); |
234 | need (enc, len += 1); |
142 | *enc->cur++ = '\\'; |
235 | *enc->cur++ = '\\'; |
143 | *enc->cur++ = '\\'; |
236 | *enc->cur++ = '\\'; |
144 | } |
237 | } |
… | |
… | |
162 | STRLEN clen; |
255 | STRLEN clen; |
163 | UV uch; |
256 | UV uch; |
164 | |
257 | |
165 | if (is_utf8) |
258 | if (is_utf8) |
166 | { |
259 | { |
167 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
168 | uch = decode_utf8 (str, end - str, &clen); |
260 | uch = decode_utf8 (str, end - str, &clen); |
169 | if (clen == (STRLEN)-1) |
261 | if (clen == (STRLEN)-1) |
170 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
262 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
171 | } |
263 | } |
172 | else |
264 | else |
173 | { |
265 | { |
174 | uch = ch; |
266 | uch = ch; |
175 | clen = 1; |
267 | clen = 1; |
176 | } |
268 | } |
177 | |
269 | |
178 | if (uch > 0x10FFFFUL) |
270 | if (uch < 0x80/*0x20*/ || uch >= enc->limit) |
179 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
180 | |
|
|
181 | if (uch < 0x80 || enc->flags & F_ASCII) |
|
|
182 | { |
271 | { |
183 | if (uch > 0xFFFFUL) |
272 | if (uch >= 0x10000UL) |
184 | { |
273 | { |
|
|
274 | if (uch >= 0x110000UL) |
|
|
275 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
276 | |
185 | need (enc, len += 11); |
277 | need (enc, len += 11); |
186 | sprintf (enc->cur, "\\u%04x\\u%04x", |
278 | sprintf (enc->cur, "\\u%04x\\u%04x", |
187 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
279 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
188 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
280 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
189 | enc->cur += 12; |
281 | enc->cur += 12; |
… | |
… | |
200 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
292 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
201 | } |
293 | } |
202 | |
294 | |
203 | str += clen; |
295 | str += clen; |
204 | } |
296 | } |
|
|
297 | else if (enc->json.flags & F_LATIN1) |
|
|
298 | { |
|
|
299 | *enc->cur++ = uch; |
|
|
300 | str += clen; |
|
|
301 | } |
205 | else if (is_utf8) |
302 | else if (is_utf8) |
206 | { |
303 | { |
207 | need (enc, len += clen); |
304 | need (enc, len += clen); |
208 | do |
305 | do |
209 | { |
306 | { |
… | |
… | |
211 | } |
308 | } |
212 | while (--clen); |
309 | while (--clen); |
213 | } |
310 | } |
214 | else |
311 | else |
215 | { |
312 | { |
216 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
313 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
217 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
314 | enc->cur = encode_utf8 (enc->cur, uch); |
218 | ++str; |
315 | ++str; |
219 | } |
316 | } |
220 | } |
317 | } |
221 | } |
318 | } |
222 | } |
319 | } |
223 | |
320 | |
224 | --len; |
321 | --len; |
225 | } |
322 | } |
226 | } |
323 | } |
227 | |
324 | |
228 | static void |
325 | INLINE void |
229 | encode_indent (enc_t *enc) |
326 | encode_indent (enc_t *enc) |
230 | { |
327 | { |
231 | if (enc->flags & F_INDENT) |
328 | if (enc->json.flags & F_INDENT) |
232 | { |
329 | { |
233 | int spaces = enc->indent * INDENT_STEP; |
330 | int spaces = enc->indent * INDENT_STEP; |
234 | |
331 | |
235 | need (enc, spaces); |
332 | need (enc, spaces); |
236 | memset (enc->cur, ' ', spaces); |
333 | memset (enc->cur, ' ', spaces); |
237 | enc->cur += spaces; |
334 | enc->cur += spaces; |
238 | } |
335 | } |
239 | } |
336 | } |
240 | |
337 | |
241 | static void |
338 | INLINE void |
242 | encode_space (enc_t *enc) |
339 | encode_space (enc_t *enc) |
243 | { |
340 | { |
244 | need (enc, 1); |
341 | need (enc, 1); |
245 | encode_ch (enc, ' '); |
342 | encode_ch (enc, ' '); |
246 | } |
343 | } |
247 | |
344 | |
248 | static void |
345 | INLINE void |
249 | encode_nl (enc_t *enc) |
346 | encode_nl (enc_t *enc) |
250 | { |
347 | { |
251 | if (enc->flags & F_INDENT) |
348 | if (enc->json.flags & F_INDENT) |
252 | { |
349 | { |
253 | need (enc, 1); |
350 | need (enc, 1); |
254 | encode_ch (enc, '\n'); |
351 | encode_ch (enc, '\n'); |
255 | } |
352 | } |
256 | } |
353 | } |
257 | |
354 | |
258 | static void |
355 | INLINE void |
259 | encode_comma (enc_t *enc) |
356 | encode_comma (enc_t *enc) |
260 | { |
357 | { |
261 | encode_ch (enc, ','); |
358 | encode_ch (enc, ','); |
262 | |
359 | |
263 | if (enc->flags & F_INDENT) |
360 | if (enc->json.flags & F_INDENT) |
264 | encode_nl (enc); |
361 | encode_nl (enc); |
265 | else if (enc->flags & F_SPACE_AFTER) |
362 | else if (enc->json.flags & F_SPACE_AFTER) |
266 | encode_space (enc); |
363 | encode_space (enc); |
267 | } |
364 | } |
268 | |
365 | |
269 | static void encode_sv (enc_t *enc, SV *sv); |
366 | static void encode_sv (enc_t *enc, SV *sv); |
270 | |
367 | |
271 | static void |
368 | static void |
272 | encode_av (enc_t *enc, AV *av) |
369 | encode_av (enc_t *enc, AV *av) |
273 | { |
370 | { |
274 | int i, len = av_len (av); |
371 | int i, len = av_len (av); |
275 | |
372 | |
276 | if (enc->indent >= enc->maxdepth) |
373 | if (enc->indent >= enc->json.max_depth) |
277 | croak ("data structure too deep (hit recursion limit)"); |
374 | croak (ERR_NESTING_EXCEEDED); |
278 | |
375 | |
279 | encode_ch (enc, '['); encode_nl (enc); |
376 | encode_ch (enc, '['); |
280 | ++enc->indent; |
377 | |
|
|
378 | if (len >= 0) |
|
|
379 | { |
|
|
380 | encode_nl (enc); ++enc->indent; |
281 | |
381 | |
282 | for (i = 0; i <= len; ++i) |
382 | for (i = 0; i <= len; ++i) |
283 | { |
383 | { |
|
|
384 | SV **svp = av_fetch (av, i, 0); |
|
|
385 | |
284 | encode_indent (enc); |
386 | encode_indent (enc); |
285 | encode_sv (enc, *av_fetch (av, i, 0)); |
|
|
286 | |
387 | |
|
|
388 | if (svp) |
|
|
389 | encode_sv (enc, *svp); |
|
|
390 | else |
|
|
391 | encode_str (enc, "null", 4, 0); |
|
|
392 | |
287 | if (i < len) |
393 | if (i < len) |
288 | encode_comma (enc); |
394 | encode_comma (enc); |
289 | } |
395 | } |
290 | |
396 | |
|
|
397 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
398 | } |
|
|
399 | |
291 | encode_nl (enc); |
400 | encode_ch (enc, ']'); |
292 | |
|
|
293 | --enc->indent; |
|
|
294 | encode_indent (enc); encode_ch (enc, ']'); |
|
|
295 | } |
401 | } |
296 | |
402 | |
297 | static void |
403 | static void |
298 | encode_he (enc_t *enc, HE *he) |
404 | encode_hk (enc_t *enc, HE *he) |
299 | { |
405 | { |
300 | encode_ch (enc, '"'); |
406 | encode_ch (enc, '"'); |
301 | |
407 | |
302 | if (HeKLEN (he) == HEf_SVKEY) |
408 | if (HeKLEN (he) == HEf_SVKEY) |
303 | { |
409 | { |
… | |
… | |
313 | else |
419 | else |
314 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
420 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
315 | |
421 | |
316 | encode_ch (enc, '"'); |
422 | encode_ch (enc, '"'); |
317 | |
423 | |
318 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
424 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
319 | encode_ch (enc, ':'); |
425 | encode_ch (enc, ':'); |
320 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
426 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
321 | encode_sv (enc, HeVAL (he)); |
|
|
322 | } |
427 | } |
323 | |
428 | |
324 | // compare hash entries, used when all keys are bytestrings |
429 | // compare hash entries, used when all keys are bytestrings |
325 | static int |
430 | static int |
326 | he_cmp_fast (const void *a_, const void *b_) |
431 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
331 | HE *b = *(HE **)b_; |
436 | HE *b = *(HE **)b_; |
332 | |
437 | |
333 | STRLEN la = HeKLEN (a); |
438 | STRLEN la = HeKLEN (a); |
334 | STRLEN lb = HeKLEN (b); |
439 | STRLEN lb = HeKLEN (b); |
335 | |
440 | |
336 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
441 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
337 | cmp = la - lb; |
442 | cmp = lb - la; |
338 | |
443 | |
339 | return cmp; |
444 | return cmp; |
340 | } |
445 | } |
341 | |
446 | |
342 | // compare hash entries, used when some keys are sv's or utf-x |
447 | // compare hash entries, used when some keys are sv's or utf-x |
343 | static int |
448 | static int |
344 | he_cmp_slow (const void *a, const void *b) |
449 | he_cmp_slow (const void *a, const void *b) |
345 | { |
450 | { |
346 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
451 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
347 | } |
452 | } |
348 | |
453 | |
349 | static void |
454 | static void |
350 | encode_hv (enc_t *enc, HV *hv) |
455 | encode_hv (enc_t *enc, HV *hv) |
351 | { |
456 | { |
352 | int count, i; |
457 | HE *he; |
353 | |
458 | |
354 | if (enc->indent >= enc->maxdepth) |
459 | if (enc->indent >= enc->json.max_depth) |
355 | croak ("data structure too deep (hit recursion limit)"); |
460 | croak (ERR_NESTING_EXCEEDED); |
356 | |
461 | |
357 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
462 | encode_ch (enc, '{'); |
358 | |
463 | |
359 | if ((count = hv_iterinit (hv))) |
|
|
360 | { |
|
|
361 | // for canonical output we have to sort by keys first |
464 | // for canonical output we have to sort by keys first |
362 | // actually, this is mostly due to the stupid so-called |
465 | // actually, this is mostly due to the stupid so-called |
363 | // security workaround added somewhere in 5.8.x. |
466 | // security workaround added somewhere in 5.8.x. |
364 | // that randomises hash orderings |
467 | // that randomises hash orderings |
365 | if (enc->flags & F_CANONICAL) |
468 | if (enc->json.flags & F_CANONICAL) |
|
|
469 | { |
|
|
470 | int count = hv_iterinit (hv); |
|
|
471 | |
|
|
472 | if (SvMAGICAL (hv)) |
366 | { |
473 | { |
|
|
474 | // need to count by iterating. could improve by dynamically building the vector below |
|
|
475 | // but I don't care for the speed of this special case. |
|
|
476 | // note also that we will run into undefined behaviour when the two iterations |
|
|
477 | // do not result in the same count, something I might care for in some later release. |
|
|
478 | |
|
|
479 | count = 0; |
|
|
480 | while (hv_iternext (hv)) |
|
|
481 | ++count; |
|
|
482 | |
|
|
483 | hv_iterinit (hv); |
|
|
484 | } |
|
|
485 | |
|
|
486 | if (count) |
|
|
487 | { |
|
|
488 | int i, fast = 1; |
|
|
489 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
490 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
491 | #else |
367 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
492 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
368 | int fast = 1; |
493 | #endif |
369 | |
494 | |
370 | i = 0; |
495 | i = 0; |
371 | while ((he = hv_iternext (hv))) |
496 | while ((he = hv_iternext (hv))) |
372 | { |
497 | { |
373 | hes [i++] = he; |
498 | hes [i++] = he; |
… | |
… | |
395 | |
520 | |
396 | FREETMPS; |
521 | FREETMPS; |
397 | LEAVE; |
522 | LEAVE; |
398 | } |
523 | } |
399 | |
524 | |
400 | for (i = 0; i < count; ++i) |
525 | encode_nl (enc); ++enc->indent; |
|
|
526 | |
|
|
527 | while (count--) |
401 | { |
528 | { |
402 | encode_indent (enc); |
529 | encode_indent (enc); |
|
|
530 | he = hes [count]; |
403 | encode_he (enc, hes [i]); |
531 | encode_hk (enc, he); |
|
|
532 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
404 | |
533 | |
405 | if (i < count - 1) |
534 | if (count) |
406 | encode_comma (enc); |
535 | encode_comma (enc); |
407 | } |
536 | } |
408 | |
537 | |
|
|
538 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
539 | } |
|
|
540 | } |
|
|
541 | else |
|
|
542 | { |
|
|
543 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
|
|
544 | if ((he = hv_iternext (hv))) |
|
|
545 | { |
|
|
546 | encode_nl (enc); ++enc->indent; |
|
|
547 | |
|
|
548 | for (;;) |
|
|
549 | { |
409 | encode_nl (enc); |
550 | encode_indent (enc); |
|
|
551 | encode_hk (enc, he); |
|
|
552 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
|
|
553 | |
|
|
554 | if (!(he = hv_iternext (hv))) |
|
|
555 | break; |
|
|
556 | |
|
|
557 | encode_comma (enc); |
|
|
558 | } |
|
|
559 | |
|
|
560 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
561 | } |
|
|
562 | } |
|
|
563 | |
|
|
564 | encode_ch (enc, '}'); |
|
|
565 | } |
|
|
566 | |
|
|
567 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
568 | static void |
|
|
569 | encode_rv (enc_t *enc, SV *sv) |
|
|
570 | { |
|
|
571 | svtype svt; |
|
|
572 | |
|
|
573 | SvGETMAGIC (sv); |
|
|
574 | svt = SvTYPE (sv); |
|
|
575 | |
|
|
576 | if (expect_false (SvOBJECT (sv))) |
|
|
577 | { |
|
|
578 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
579 | ? json_boolean_stash |
|
|
580 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
581 | |
|
|
582 | if (SvSTASH (sv) == stash) |
|
|
583 | { |
|
|
584 | if (SvIV (sv)) |
|
|
585 | encode_str (enc, "true", 4, 0); |
|
|
586 | else |
|
|
587 | encode_str (enc, "false", 5, 0); |
410 | } |
588 | } |
411 | else |
589 | else |
412 | { |
590 | { |
413 | SV *sv; |
591 | #if 0 |
414 | HE *he = hv_iternext (hv); |
592 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
415 | |
|
|
416 | for (;;) |
|
|
417 | { |
593 | { |
418 | encode_indent (enc); |
594 | // not yet |
419 | encode_he (enc, he); |
|
|
420 | |
|
|
421 | if (!(he = hv_iternext (hv))) |
|
|
422 | break; |
|
|
423 | |
|
|
424 | encode_comma (enc); |
|
|
425 | } |
595 | } |
|
|
596 | #endif |
|
|
597 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
598 | { |
|
|
599 | // we re-bless the reference to get overload and other niceties right |
|
|
600 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
426 | |
601 | |
|
|
602 | if (to_json) |
|
|
603 | { |
|
|
604 | dSP; |
|
|
605 | |
|
|
606 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
607 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
608 | |
|
|
609 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
610 | PUTBACK; |
|
|
611 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
612 | SPAGAIN; |
|
|
613 | |
|
|
614 | // catch this surprisingly common error |
|
|
615 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
616 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
617 | |
|
|
618 | sv = POPs; |
|
|
619 | PUTBACK; |
|
|
620 | |
427 | encode_nl (enc); |
621 | encode_sv (enc, sv); |
|
|
622 | |
|
|
623 | FREETMPS; LEAVE; |
|
|
624 | } |
|
|
625 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
626 | encode_str (enc, "null", 4, 0); |
|
|
627 | else |
|
|
628 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
629 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
630 | } |
|
|
631 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
632 | encode_str (enc, "null", 4, 0); |
|
|
633 | else |
|
|
634 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
635 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
428 | } |
636 | } |
429 | } |
637 | } |
430 | |
|
|
431 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
432 | } |
|
|
433 | |
|
|
434 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
435 | static void |
|
|
436 | encode_rv (enc_t *enc, SV *sv) |
|
|
437 | { |
|
|
438 | SvGETMAGIC (sv); |
|
|
439 | |
|
|
440 | svtype svt = SvTYPE (sv); |
|
|
441 | |
|
|
442 | if (svt == SVt_PVHV) |
638 | else if (svt == SVt_PVHV) |
443 | encode_hv (enc, (HV *)sv); |
639 | encode_hv (enc, (HV *)sv); |
444 | else if (svt == SVt_PVAV) |
640 | else if (svt == SVt_PVAV) |
445 | encode_av (enc, (AV *)sv); |
641 | encode_av (enc, (AV *)sv); |
446 | else if (svt < SVt_PVAV) |
642 | else if (svt < SVt_PVAV) |
447 | { |
643 | { |
448 | if (SvNIOK (sv) && SvIV (sv) == 0) |
644 | STRLEN len = 0; |
|
|
645 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
646 | |
|
|
647 | if (len == 1 && *pv == '1') |
|
|
648 | encode_str (enc, "true", 4, 0); |
|
|
649 | else if (len == 1 && *pv == '0') |
449 | encode_str (enc, "false", 5, 0); |
650 | encode_str (enc, "false", 5, 0); |
450 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
651 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
451 | encode_str (enc, "true", 4, 0); |
652 | encode_str (enc, "null", 4, 0); |
452 | else |
653 | else |
453 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
654 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
454 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
655 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
455 | } |
656 | } |
|
|
657 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
|
|
658 | encode_str (enc, "null", 4, 0); |
456 | else |
659 | else |
457 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
660 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
458 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
661 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
459 | } |
662 | } |
460 | |
663 | |
… | |
… | |
471 | encode_str (enc, str, len, SvUTF8 (sv)); |
674 | encode_str (enc, str, len, SvUTF8 (sv)); |
472 | encode_ch (enc, '"'); |
675 | encode_ch (enc, '"'); |
473 | } |
676 | } |
474 | else if (SvNOKp (sv)) |
677 | else if (SvNOKp (sv)) |
475 | { |
678 | { |
|
|
679 | // trust that perl will do the right thing w.r.t. JSON syntax. |
476 | need (enc, NV_DIG + 32); |
680 | need (enc, NV_DIG + 32); |
477 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
681 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
478 | enc->cur += strlen (enc->cur); |
682 | enc->cur += strlen (enc->cur); |
479 | } |
683 | } |
480 | else if (SvIOKp (sv)) |
684 | else if (SvIOKp (sv)) |
481 | { |
685 | { |
|
|
686 | // we assume we can always read an IV as a UV and vice versa |
|
|
687 | // we assume two's complement |
|
|
688 | // we assume no aliasing issues in the union |
|
|
689 | if (SvIsUV (sv) ? SvUVX (sv) <= 59000 |
|
|
690 | : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000) |
|
|
691 | { |
|
|
692 | // optimise the "small number case" |
|
|
693 | // code will likely be branchless and use only a single multiplication |
|
|
694 | // works for numbers up to 59074 |
|
|
695 | I32 i = SvIVX (sv); |
|
|
696 | U32 u; |
|
|
697 | char digit, nz = 0; |
|
|
698 | |
482 | need (enc, 64); |
699 | need (enc, 6); |
|
|
700 | |
|
|
701 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
702 | u = i < 0 ? -i : i; |
|
|
703 | |
|
|
704 | // convert to 4.28 fixed-point representation |
|
|
705 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
706 | |
|
|
707 | // now output digit by digit, each time masking out the integer part |
|
|
708 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
709 | // resulting in a net multiplication by 10. |
|
|
710 | // we always write the digit to memory but conditionally increment |
|
|
711 | // the pointer, to enable the use of conditional move instructions. |
|
|
712 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; |
|
|
713 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; |
|
|
714 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; |
|
|
715 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; |
|
|
716 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
717 | } |
|
|
718 | else |
|
|
719 | { |
|
|
720 | // large integer, use the (rather slow) snprintf way. |
|
|
721 | need (enc, IVUV_MAXCHARS); |
483 | enc->cur += |
722 | enc->cur += |
484 | SvIsUV(sv) |
723 | SvIsUV(sv) |
485 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
724 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
486 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
725 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
|
|
726 | } |
487 | } |
727 | } |
488 | else if (SvROK (sv)) |
728 | else if (SvROK (sv)) |
489 | encode_rv (enc, SvRV (sv)); |
729 | encode_rv (enc, SvRV (sv)); |
490 | else if (!SvOK (sv)) |
730 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
491 | encode_str (enc, "null", 4, 0); |
731 | encode_str (enc, "null", 4, 0); |
492 | else |
732 | else |
493 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
733 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
494 | SvPV_nolen (sv), SvFLAGS (sv)); |
734 | SvPV_nolen (sv), SvFLAGS (sv)); |
495 | } |
735 | } |
496 | |
736 | |
497 | static SV * |
737 | static SV * |
498 | encode_json (SV *scalar, U32 flags) |
738 | encode_json (SV *scalar, JSON *json) |
499 | { |
739 | { |
|
|
740 | enc_t enc; |
|
|
741 | |
500 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
742 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
501 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
743 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
502 | |
744 | |
503 | enc_t enc; |
745 | enc.json = *json; |
504 | enc.flags = flags; |
|
|
505 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
746 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
506 | enc.cur = SvPVX (enc.sv); |
747 | enc.cur = SvPVX (enc.sv); |
507 | enc.end = SvEND (enc.sv); |
748 | enc.end = SvEND (enc.sv); |
508 | enc.indent = 0; |
749 | enc.indent = 0; |
509 | enc.maxdepth = DEC_DEPTH (flags); |
750 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
|
|
751 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
|
|
752 | : 0x110000UL; |
510 | |
753 | |
511 | SvPOK_only (enc.sv); |
754 | SvPOK_only (enc.sv); |
512 | encode_sv (&enc, scalar); |
755 | encode_sv (&enc, scalar); |
513 | |
756 | |
|
|
757 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
758 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
759 | |
514 | if (!(flags & (F_ASCII | F_UTF8))) |
760 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
515 | SvUTF8_on (enc.sv); |
761 | SvUTF8_on (enc.sv); |
516 | |
762 | |
517 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
518 | |
|
|
519 | if (enc.flags & F_SHRINK) |
763 | if (enc.json.flags & F_SHRINK) |
520 | shrink (enc.sv); |
764 | shrink (enc.sv); |
521 | |
765 | |
522 | return enc.sv; |
766 | return enc.sv; |
523 | } |
767 | } |
524 | |
768 | |
… | |
… | |
529 | typedef struct |
773 | typedef struct |
530 | { |
774 | { |
531 | char *cur; // current parser pointer |
775 | char *cur; // current parser pointer |
532 | char *end; // end of input string |
776 | char *end; // end of input string |
533 | const char *err; // parse error, if != 0 |
777 | const char *err; // parse error, if != 0 |
534 | U32 flags; // F_* |
778 | JSON json; |
535 | U32 depth; // recursion depth |
779 | U32 depth; // recursion depth |
536 | U32 maxdepth; // recursion depth limit |
780 | U32 maxdepth; // recursion depth limit |
537 | } dec_t; |
781 | } dec_t; |
538 | |
782 | |
539 | static void |
783 | INLINE void |
|
|
784 | decode_comment (dec_t *dec) |
|
|
785 | { |
|
|
786 | // only '#'-style comments allowed a.t.m. |
|
|
787 | |
|
|
788 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
|
|
789 | ++dec->cur; |
|
|
790 | } |
|
|
791 | |
|
|
792 | INLINE void |
540 | decode_ws (dec_t *dec) |
793 | decode_ws (dec_t *dec) |
541 | { |
794 | { |
542 | for (;;) |
795 | for (;;) |
543 | { |
796 | { |
544 | char ch = *dec->cur; |
797 | char ch = *dec->cur; |
545 | |
798 | |
546 | if (ch > 0x20 |
799 | if (ch > 0x20) |
|
|
800 | { |
|
|
801 | if (expect_false (ch == '#')) |
|
|
802 | { |
|
|
803 | if (dec->json.flags & F_RELAXED) |
|
|
804 | decode_comment (dec); |
|
|
805 | else |
|
|
806 | break; |
|
|
807 | } |
|
|
808 | else |
|
|
809 | break; |
|
|
810 | } |
547 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
811 | else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
548 | break; |
812 | break; // parse error, but let higher level handle it, gives better error messages |
549 | |
813 | |
550 | ++dec->cur; |
814 | ++dec->cur; |
551 | } |
815 | } |
552 | } |
816 | } |
553 | |
817 | |
… | |
… | |
557 | if (*dec->cur != ch) \ |
821 | if (*dec->cur != ch) \ |
558 | ERR (# ch " expected"); \ |
822 | ERR (# ch " expected"); \ |
559 | ++dec->cur; \ |
823 | ++dec->cur; \ |
560 | SE |
824 | SE |
561 | |
825 | |
562 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
826 | #define DEC_INC_DEPTH if (++dec->depth > dec->json.max_depth) ERR (ERR_NESTING_EXCEEDED) |
563 | #define DEC_DEC_DEPTH --dec->depth |
827 | #define DEC_DEC_DEPTH --dec->depth |
564 | |
828 | |
565 | static SV *decode_sv (dec_t *dec); |
829 | static SV *decode_sv (dec_t *dec); |
566 | |
830 | |
567 | static signed char decode_hexdigit[256]; |
831 | static signed char decode_hexdigit[256]; |
… | |
… | |
570 | decode_4hex (dec_t *dec) |
834 | decode_4hex (dec_t *dec) |
571 | { |
835 | { |
572 | signed char d1, d2, d3, d4; |
836 | signed char d1, d2, d3, d4; |
573 | unsigned char *cur = (unsigned char *)dec->cur; |
837 | unsigned char *cur = (unsigned char *)dec->cur; |
574 | |
838 | |
575 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
839 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
576 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
840 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
577 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
841 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
578 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
842 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
579 | |
843 | |
580 | dec->cur += 4; |
844 | dec->cur += 4; |
581 | |
845 | |
582 | return ((UV)d1) << 12 |
846 | return ((UV)d1) << 12 |
583 | | ((UV)d2) << 8 |
847 | | ((UV)d2) << 8 |
… | |
… | |
591 | static SV * |
855 | static SV * |
592 | decode_str (dec_t *dec) |
856 | decode_str (dec_t *dec) |
593 | { |
857 | { |
594 | SV *sv = 0; |
858 | SV *sv = 0; |
595 | int utf8 = 0; |
859 | int utf8 = 0; |
|
|
860 | char *dec_cur = dec->cur; |
596 | |
861 | |
597 | do |
862 | do |
598 | { |
863 | { |
599 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
864 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
600 | char *cur = buf; |
865 | char *cur = buf; |
601 | |
866 | |
602 | do |
867 | do |
603 | { |
868 | { |
604 | unsigned char ch = *(unsigned char *)dec->cur++; |
869 | unsigned char ch = *(unsigned char *)dec_cur++; |
605 | |
870 | |
606 | if (ch == '"') |
871 | if (expect_false (ch == '"')) |
607 | { |
872 | { |
608 | --dec->cur; |
873 | --dec_cur; |
609 | break; |
874 | break; |
610 | } |
875 | } |
611 | else if (ch == '\\') |
876 | else if (expect_false (ch == '\\')) |
612 | { |
877 | { |
613 | switch (*dec->cur) |
878 | switch (*dec_cur) |
614 | { |
879 | { |
615 | case '\\': |
880 | case '\\': |
616 | case '/': |
881 | case '/': |
617 | case '"': *cur++ = *dec->cur++; break; |
882 | case '"': *cur++ = *dec_cur++; break; |
618 | |
883 | |
619 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
884 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
620 | case 't': ++dec->cur; *cur++ = '\011'; break; |
885 | case 't': ++dec_cur; *cur++ = '\011'; break; |
621 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
886 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
622 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
887 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
623 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
888 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
624 | |
889 | |
625 | case 'u': |
890 | case 'u': |
626 | { |
891 | { |
627 | UV lo, hi; |
892 | UV lo, hi; |
628 | ++dec->cur; |
893 | ++dec_cur; |
629 | |
894 | |
|
|
895 | dec->cur = dec_cur; |
630 | hi = decode_4hex (dec); |
896 | hi = decode_4hex (dec); |
|
|
897 | dec_cur = dec->cur; |
631 | if (hi == (UV)-1) |
898 | if (hi == (UV)-1) |
632 | goto fail; |
899 | goto fail; |
633 | |
900 | |
634 | // possibly a surrogate pair |
901 | // possibly a surrogate pair |
635 | if (hi >= 0xd800) |
902 | if (hi >= 0xd800) |
636 | if (hi < 0xdc00) |
903 | if (hi < 0xdc00) |
637 | { |
904 | { |
638 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
905 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
639 | ERR ("missing low surrogate character in surrogate pair"); |
906 | ERR ("missing low surrogate character in surrogate pair"); |
640 | |
907 | |
641 | dec->cur += 2; |
908 | dec_cur += 2; |
642 | |
909 | |
|
|
910 | dec->cur = dec_cur; |
643 | lo = decode_4hex (dec); |
911 | lo = decode_4hex (dec); |
|
|
912 | dec_cur = dec->cur; |
644 | if (lo == (UV)-1) |
913 | if (lo == (UV)-1) |
645 | goto fail; |
914 | goto fail; |
646 | |
915 | |
647 | if (lo < 0xdc00 || lo >= 0xe000) |
916 | if (lo < 0xdc00 || lo >= 0xe000) |
648 | ERR ("surrogate pair expected"); |
917 | ERR ("surrogate pair expected"); |
… | |
… | |
654 | |
923 | |
655 | if (hi >= 0x80) |
924 | if (hi >= 0x80) |
656 | { |
925 | { |
657 | utf8 = 1; |
926 | utf8 = 1; |
658 | |
927 | |
659 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
928 | cur = encode_utf8 (cur, hi); |
660 | } |
929 | } |
661 | else |
930 | else |
662 | *cur++ = hi; |
931 | *cur++ = hi; |
663 | } |
932 | } |
664 | break; |
933 | break; |
665 | |
934 | |
666 | default: |
935 | default: |
667 | --dec->cur; |
936 | --dec_cur; |
668 | ERR ("illegal backslash escape sequence in string"); |
937 | ERR ("illegal backslash escape sequence in string"); |
669 | } |
938 | } |
670 | } |
939 | } |
671 | else if (ch >= 0x20 && ch <= 0x7f) |
940 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
672 | *cur++ = ch; |
941 | *cur++ = ch; |
673 | else if (ch >= 0x80) |
942 | else if (ch >= 0x80) |
674 | { |
943 | { |
675 | --dec->cur; |
|
|
676 | |
|
|
677 | STRLEN clen; |
944 | STRLEN clen; |
|
|
945 | UV uch; |
|
|
946 | |
|
|
947 | --dec_cur; |
|
|
948 | |
678 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
949 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
679 | if (clen == (STRLEN)-1) |
950 | if (clen == (STRLEN)-1) |
680 | ERR ("malformed UTF-8 character in JSON string"); |
951 | ERR ("malformed UTF-8 character in JSON string"); |
681 | |
952 | |
682 | do |
953 | do |
683 | *cur++ = *dec->cur++; |
954 | *cur++ = *dec_cur++; |
684 | while (--clen); |
955 | while (--clen); |
685 | |
956 | |
686 | utf8 = 1; |
957 | utf8 = 1; |
687 | } |
958 | } |
688 | else |
959 | else |
689 | { |
960 | { |
690 | --dec->cur; |
961 | --dec_cur; |
691 | |
962 | |
692 | if (!ch) |
963 | if (!ch) |
693 | ERR ("unexpected end of string while parsing JSON string"); |
964 | ERR ("unexpected end of string while parsing JSON string"); |
694 | else |
965 | else |
695 | ERR ("invalid character encountered while parsing JSON string"); |
966 | ERR ("invalid character encountered while parsing JSON string"); |
696 | } |
967 | } |
697 | } |
968 | } |
698 | while (cur < buf + SHORT_STRING_LEN); |
969 | while (cur < buf + SHORT_STRING_LEN); |
699 | |
970 | |
|
|
971 | { |
700 | STRLEN len = cur - buf; |
972 | STRLEN len = cur - buf; |
701 | |
973 | |
702 | if (sv) |
974 | if (sv) |
703 | { |
975 | { |
704 | SvGROW (sv, SvCUR (sv) + len + 1); |
976 | SvGROW (sv, SvCUR (sv) + len + 1); |
705 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
977 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
706 | SvCUR_set (sv, SvCUR (sv) + len); |
978 | SvCUR_set (sv, SvCUR (sv) + len); |
707 | } |
979 | } |
708 | else |
980 | else |
709 | sv = newSVpvn (buf, len); |
981 | sv = newSVpvn (buf, len); |
710 | } |
982 | } |
|
|
983 | } |
711 | while (*dec->cur != '"'); |
984 | while (*dec_cur != '"'); |
712 | |
985 | |
713 | ++dec->cur; |
986 | ++dec_cur; |
714 | |
987 | |
715 | if (sv) |
988 | if (sv) |
716 | { |
989 | { |
717 | SvPOK_only (sv); |
990 | SvPOK_only (sv); |
718 | *SvEND (sv) = 0; |
991 | *SvEND (sv) = 0; |
… | |
… | |
721 | SvUTF8_on (sv); |
994 | SvUTF8_on (sv); |
722 | } |
995 | } |
723 | else |
996 | else |
724 | sv = newSVpvn ("", 0); |
997 | sv = newSVpvn ("", 0); |
725 | |
998 | |
|
|
999 | dec->cur = dec_cur; |
726 | return sv; |
1000 | return sv; |
727 | |
1001 | |
728 | fail: |
1002 | fail: |
|
|
1003 | dec->cur = dec_cur; |
729 | return 0; |
1004 | return 0; |
730 | } |
1005 | } |
731 | |
1006 | |
732 | static SV * |
1007 | static SV * |
733 | decode_num (dec_t *dec) |
1008 | decode_num (dec_t *dec) |
… | |
… | |
791 | is_nv = 1; |
1066 | is_nv = 1; |
792 | } |
1067 | } |
793 | |
1068 | |
794 | if (!is_nv) |
1069 | if (!is_nv) |
795 | { |
1070 | { |
796 | UV uv; |
1071 | int len = dec->cur - start; |
797 | int numtype = grok_number (start, dec->cur - start, &uv); |
1072 | |
798 | if (numtype & IS_NUMBER_IN_UV) |
1073 | // special case the rather common 1..5-digit-int case |
799 | if (numtype & IS_NUMBER_NEG) |
1074 | if (*start == '-') |
|
|
1075 | switch (len) |
800 | { |
1076 | { |
801 | if (uv < (UV)IV_MIN) |
1077 | case 2: return newSViv (-( start [1] - '0' * 1)); |
802 | return newSViv (-(IV)uv); |
1078 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
1079 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
1080 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1081 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
803 | } |
1082 | } |
|
|
1083 | else |
|
|
1084 | switch (len) |
|
|
1085 | { |
|
|
1086 | case 1: return newSViv ( start [0] - '0' * 1); |
|
|
1087 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
1088 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
1089 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1090 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
|
|
1091 | } |
|
|
1092 | |
|
|
1093 | { |
|
|
1094 | UV uv; |
|
|
1095 | int numtype = grok_number (start, len, &uv); |
|
|
1096 | if (numtype & IS_NUMBER_IN_UV) |
|
|
1097 | if (numtype & IS_NUMBER_NEG) |
|
|
1098 | { |
|
|
1099 | if (uv < (UV)IV_MIN) |
|
|
1100 | return newSViv (-(IV)uv); |
|
|
1101 | } |
804 | else |
1102 | else |
805 | return newSVuv (uv); |
1103 | return newSVuv (uv); |
806 | } |
1104 | } |
807 | |
1105 | |
|
|
1106 | len -= *start == '-' ? 1 : 0; |
|
|
1107 | |
|
|
1108 | // does not fit into IV or UV, try NV |
|
|
1109 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
1110 | #if defined (LDBL_DIG) |
|
|
1111 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1112 | #endif |
|
|
1113 | ) |
|
|
1114 | // fits into NV without loss of precision |
|
|
1115 | return newSVnv (Atof (start)); |
|
|
1116 | |
|
|
1117 | // everything else fails, convert it to a string |
|
|
1118 | return newSVpvn (start, dec->cur - start); |
|
|
1119 | } |
|
|
1120 | |
|
|
1121 | // loss of precision here |
808 | return newSVnv (Atof (start)); |
1122 | return newSVnv (Atof (start)); |
809 | |
1123 | |
810 | fail: |
1124 | fail: |
811 | return 0; |
1125 | return 0; |
812 | } |
1126 | } |
… | |
… | |
842 | |
1156 | |
843 | if (*dec->cur != ',') |
1157 | if (*dec->cur != ',') |
844 | ERR (", or ] expected while parsing array"); |
1158 | ERR (", or ] expected while parsing array"); |
845 | |
1159 | |
846 | ++dec->cur; |
1160 | ++dec->cur; |
|
|
1161 | |
|
|
1162 | decode_ws (dec); |
|
|
1163 | |
|
|
1164 | if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
|
|
1165 | { |
|
|
1166 | ++dec->cur; |
|
|
1167 | break; |
|
|
1168 | } |
847 | } |
1169 | } |
848 | |
1170 | |
849 | DEC_DEC_DEPTH; |
1171 | DEC_DEC_DEPTH; |
850 | return newRV_noinc ((SV *)av); |
1172 | return newRV_noinc ((SV *)av); |
851 | |
1173 | |
… | |
… | |
856 | } |
1178 | } |
857 | |
1179 | |
858 | static SV * |
1180 | static SV * |
859 | decode_hv (dec_t *dec) |
1181 | decode_hv (dec_t *dec) |
860 | { |
1182 | { |
|
|
1183 | SV *sv; |
861 | HV *hv = newHV (); |
1184 | HV *hv = newHV (); |
862 | |
1185 | |
863 | DEC_INC_DEPTH; |
1186 | DEC_INC_DEPTH; |
864 | decode_ws (dec); |
1187 | decode_ws (dec); |
865 | |
1188 | |
866 | if (*dec->cur == '}') |
1189 | if (*dec->cur == '}') |
867 | ++dec->cur; |
1190 | ++dec->cur; |
868 | else |
1191 | else |
869 | for (;;) |
1192 | for (;;) |
870 | { |
1193 | { |
|
|
1194 | EXPECT_CH ('"'); |
|
|
1195 | |
|
|
1196 | // heuristic: assume that |
|
|
1197 | // a) decode_str + hv_store_ent are abysmally slow. |
|
|
1198 | // b) most hash keys are short, simple ascii text. |
|
|
1199 | // => try to "fast-match" such strings to avoid |
|
|
1200 | // the overhead of decode_str + hv_store_ent. |
|
|
1201 | { |
871 | SV *key, *value; |
1202 | SV *value; |
|
|
1203 | char *p = dec->cur; |
|
|
1204 | char *e = p + 24; // only try up to 24 bytes |
872 | |
1205 | |
873 | decode_ws (dec); EXPECT_CH ('"'); |
1206 | for (;;) |
874 | |
|
|
875 | key = decode_str (dec); |
|
|
876 | if (!key) |
|
|
877 | goto fail; |
|
|
878 | |
|
|
879 | decode_ws (dec); EXPECT_CH (':'); |
|
|
880 | |
|
|
881 | value = decode_sv (dec); |
|
|
882 | if (!value) |
|
|
883 | { |
1207 | { |
|
|
1208 | // the >= 0x80 is false on most architectures |
|
|
1209 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1210 | { |
|
|
1211 | // slow path, back up and use decode_str |
|
|
1212 | SV *key = decode_str (dec); |
|
|
1213 | if (!key) |
|
|
1214 | goto fail; |
|
|
1215 | |
|
|
1216 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1217 | |
|
|
1218 | decode_ws (dec); |
|
|
1219 | value = decode_sv (dec); |
|
|
1220 | if (!value) |
|
|
1221 | { |
|
|
1222 | SvREFCNT_dec (key); |
|
|
1223 | goto fail; |
|
|
1224 | } |
|
|
1225 | |
|
|
1226 | hv_store_ent (hv, key, value, 0); |
884 | SvREFCNT_dec (key); |
1227 | SvREFCNT_dec (key); |
|
|
1228 | |
|
|
1229 | break; |
|
|
1230 | } |
|
|
1231 | else if (*p == '"') |
|
|
1232 | { |
|
|
1233 | // fast path, got a simple key |
|
|
1234 | char *key = dec->cur; |
|
|
1235 | int len = p - key; |
|
|
1236 | dec->cur = p + 1; |
|
|
1237 | |
|
|
1238 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1239 | |
|
|
1240 | decode_ws (dec); |
|
|
1241 | value = decode_sv (dec); |
|
|
1242 | if (!value) |
885 | goto fail; |
1243 | goto fail; |
|
|
1244 | |
|
|
1245 | hv_store (hv, key, len, value, 0); |
|
|
1246 | |
|
|
1247 | break; |
|
|
1248 | } |
|
|
1249 | |
|
|
1250 | ++p; |
886 | } |
1251 | } |
887 | |
1252 | } |
888 | hv_store_ent (hv, key, value, 0); |
|
|
889 | SvREFCNT_dec (key); |
|
|
890 | |
1253 | |
891 | decode_ws (dec); |
1254 | decode_ws (dec); |
892 | |
1255 | |
893 | if (*dec->cur == '}') |
1256 | if (*dec->cur == '}') |
894 | { |
1257 | { |
… | |
… | |
898 | |
1261 | |
899 | if (*dec->cur != ',') |
1262 | if (*dec->cur != ',') |
900 | ERR (", or } expected while parsing object/hash"); |
1263 | ERR (", or } expected while parsing object/hash"); |
901 | |
1264 | |
902 | ++dec->cur; |
1265 | ++dec->cur; |
|
|
1266 | |
|
|
1267 | decode_ws (dec); |
|
|
1268 | |
|
|
1269 | if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
|
|
1270 | { |
|
|
1271 | ++dec->cur; |
|
|
1272 | break; |
|
|
1273 | } |
903 | } |
1274 | } |
904 | |
1275 | |
905 | DEC_DEC_DEPTH; |
1276 | DEC_DEC_DEPTH; |
906 | return newRV_noinc ((SV *)hv); |
1277 | sv = newRV_noinc ((SV *)hv); |
|
|
1278 | |
|
|
1279 | // check filter callbacks |
|
|
1280 | if (dec->json.flags & F_HOOK) |
|
|
1281 | { |
|
|
1282 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1283 | { |
|
|
1284 | HE *cb, *he; |
|
|
1285 | |
|
|
1286 | hv_iterinit (hv); |
|
|
1287 | he = hv_iternext (hv); |
|
|
1288 | hv_iterinit (hv); |
|
|
1289 | |
|
|
1290 | // the next line creates a mortal sv each time its called. |
|
|
1291 | // might want to optimise this for common cases. |
|
|
1292 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1293 | |
|
|
1294 | if (cb) |
|
|
1295 | { |
|
|
1296 | dSP; |
|
|
1297 | int count; |
|
|
1298 | |
|
|
1299 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1300 | XPUSHs (HeVAL (he)); |
|
|
1301 | |
|
|
1302 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1303 | |
|
|
1304 | if (count == 1) |
|
|
1305 | { |
|
|
1306 | sv = newSVsv (POPs); |
|
|
1307 | FREETMPS; LEAVE; |
|
|
1308 | return sv; |
|
|
1309 | } |
|
|
1310 | |
|
|
1311 | FREETMPS; LEAVE; |
|
|
1312 | } |
|
|
1313 | } |
|
|
1314 | |
|
|
1315 | if (dec->json.cb_object) |
|
|
1316 | { |
|
|
1317 | dSP; |
|
|
1318 | int count; |
|
|
1319 | |
|
|
1320 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1321 | XPUSHs (sv_2mortal (sv)); |
|
|
1322 | |
|
|
1323 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1324 | |
|
|
1325 | if (count == 1) |
|
|
1326 | { |
|
|
1327 | sv = newSVsv (POPs); |
|
|
1328 | FREETMPS; LEAVE; |
|
|
1329 | return sv; |
|
|
1330 | } |
|
|
1331 | |
|
|
1332 | SvREFCNT_inc (sv); |
|
|
1333 | FREETMPS; LEAVE; |
|
|
1334 | } |
|
|
1335 | } |
|
|
1336 | |
|
|
1337 | return sv; |
907 | |
1338 | |
908 | fail: |
1339 | fail: |
909 | SvREFCNT_dec (hv); |
1340 | SvREFCNT_dec (hv); |
910 | DEC_DEC_DEPTH; |
1341 | DEC_DEC_DEPTH; |
911 | return 0; |
1342 | return 0; |
912 | } |
1343 | } |
913 | |
1344 | |
914 | static SV * |
1345 | static SV * |
915 | decode_sv (dec_t *dec) |
1346 | decode_sv (dec_t *dec) |
916 | { |
1347 | { |
917 | decode_ws (dec); |
1348 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1349 | // to parse everything. |
918 | switch (*dec->cur) |
1350 | switch (*dec->cur) |
919 | { |
1351 | { |
920 | case '"': ++dec->cur; return decode_str (dec); |
1352 | case '"': ++dec->cur; return decode_str (dec); |
921 | case '[': ++dec->cur; return decode_av (dec); |
1353 | case '[': ++dec->cur; return decode_av (dec); |
922 | case '{': ++dec->cur; return decode_hv (dec); |
1354 | case '{': ++dec->cur; return decode_hv (dec); |
923 | |
1355 | |
924 | case '-': |
1356 | case '-': |
925 | case '0': case '1': case '2': case '3': case '4': |
1357 | case '0': case '1': case '2': case '3': case '4': |
926 | case '5': case '6': case '7': case '8': case '9': |
1358 | case '5': case '6': case '7': case '8': case '9': |
927 | return decode_num (dec); |
1359 | return decode_num (dec); |
928 | |
1360 | |
929 | case 't': |
1361 | case 't': |
930 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1362 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
931 | { |
1363 | { |
932 | dec->cur += 4; |
1364 | dec->cur += 4; |
|
|
1365 | #if JSON_SLOW |
|
|
1366 | json_true = get_bool ("JSON::XS::true"); |
|
|
1367 | #endif |
933 | return newSViv (1); |
1368 | return newSVsv (json_true); |
934 | } |
1369 | } |
935 | else |
1370 | else |
936 | ERR ("'true' expected"); |
1371 | ERR ("'true' expected"); |
937 | |
1372 | |
938 | break; |
1373 | break; |
939 | |
1374 | |
940 | case 'f': |
1375 | case 'f': |
941 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1376 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
942 | { |
1377 | { |
943 | dec->cur += 5; |
1378 | dec->cur += 5; |
|
|
1379 | #if JSON_SLOW |
|
|
1380 | json_false = get_bool ("JSON::XS::false"); |
|
|
1381 | #endif |
944 | return newSViv (0); |
1382 | return newSVsv (json_false); |
945 | } |
1383 | } |
946 | else |
1384 | else |
947 | ERR ("'false' expected"); |
1385 | ERR ("'false' expected"); |
948 | |
1386 | |
949 | break; |
1387 | break; |
… | |
… | |
967 | fail: |
1405 | fail: |
968 | return 0; |
1406 | return 0; |
969 | } |
1407 | } |
970 | |
1408 | |
971 | static SV * |
1409 | static SV * |
972 | decode_json (SV *string, U32 flags) |
1410 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
973 | { |
1411 | { |
|
|
1412 | dec_t dec; |
|
|
1413 | STRLEN offset; |
974 | SV *sv; |
1414 | SV *sv; |
975 | |
1415 | |
|
|
1416 | SvGETMAGIC (string); |
976 | SvUPGRADE (string, SVt_PV); |
1417 | SvUPGRADE (string, SVt_PV); |
977 | |
1418 | |
|
|
1419 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
|
|
1420 | * assertion with -DDEBUGGING, although SvCUR is documented to |
|
|
1421 | * return the xpv_cur field which certainly exists after upgrading. |
|
|
1422 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
1423 | */ |
|
|
1424 | SvPOK (string); |
|
|
1425 | |
|
|
1426 | if (SvCUR (string) > json->max_size && json->max_size) |
|
|
1427 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1428 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
|
|
1429 | |
978 | if (flags & F_UTF8) |
1430 | if (json->flags & F_UTF8) |
979 | sv_utf8_downgrade (string, 0); |
1431 | sv_utf8_downgrade (string, 0); |
980 | else |
1432 | else |
981 | sv_utf8_upgrade (string); |
1433 | sv_utf8_upgrade (string); |
982 | |
1434 | |
983 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1435 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
984 | |
1436 | |
985 | dec_t dec; |
1437 | dec.json = *json; |
986 | dec.flags = flags; |
|
|
987 | dec.cur = SvPVX (string); |
1438 | dec.cur = SvPVX (string); |
988 | dec.end = SvEND (string); |
1439 | dec.end = SvEND (string); |
989 | dec.err = 0; |
1440 | dec.err = 0; |
990 | dec.depth = 0; |
1441 | dec.depth = 0; |
991 | dec.maxdepth = DEC_DEPTH (dec.flags); |
|
|
992 | |
1442 | |
|
|
1443 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1444 | dec.json.flags |= F_HOOK; |
|
|
1445 | |
993 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
1446 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1447 | |
|
|
1448 | decode_ws (&dec); |
994 | sv = decode_sv (&dec); |
1449 | sv = decode_sv (&dec); |
995 | |
1450 | |
|
|
1451 | if (!(offset_return || !sv)) |
|
|
1452 | { |
|
|
1453 | // check for trailing garbage |
|
|
1454 | decode_ws (&dec); |
|
|
1455 | |
|
|
1456 | if (*dec.cur) |
|
|
1457 | { |
|
|
1458 | dec.err = "garbage after JSON object"; |
|
|
1459 | SvREFCNT_dec (sv); |
|
|
1460 | sv = 0; |
|
|
1461 | } |
|
|
1462 | } |
|
|
1463 | |
|
|
1464 | if (offset_return || !sv) |
|
|
1465 | { |
|
|
1466 | offset = dec.json.flags & F_UTF8 |
|
|
1467 | ? dec.cur - SvPVX (string) |
|
|
1468 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1469 | |
|
|
1470 | if (offset_return) |
|
|
1471 | *offset_return = offset; |
|
|
1472 | } |
|
|
1473 | |
996 | if (!sv) |
1474 | if (!sv) |
997 | { |
1475 | { |
998 | IV offset = dec.flags & F_UTF8 |
|
|
999 | ? dec.cur - SvPVX (string) |
|
|
1000 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1001 | SV *uni = sv_newmortal (); |
1476 | SV *uni = sv_newmortal (); |
1002 | |
1477 | |
1003 | // horrible hack to silence warning inside pv_uni_display |
1478 | // horrible hack to silence warning inside pv_uni_display |
1004 | COP cop = *PL_curcop; |
1479 | COP cop = *PL_curcop; |
1005 | cop.cop_warnings = pWARN_NONE; |
1480 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
1015 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1490 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1016 | } |
1491 | } |
1017 | |
1492 | |
1018 | sv = sv_2mortal (sv); |
1493 | sv = sv_2mortal (sv); |
1019 | |
1494 | |
1020 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1495 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1021 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1496 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1022 | |
1497 | |
1023 | return sv; |
1498 | return sv; |
|
|
1499 | } |
|
|
1500 | |
|
|
1501 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1502 | // incremental parser |
|
|
1503 | |
|
|
1504 | static void |
|
|
1505 | incr_parse (JSON *self) |
|
|
1506 | { |
|
|
1507 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1508 | |
|
|
1509 | for (;;) |
|
|
1510 | { |
|
|
1511 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
|
|
1512 | switch (self->incr_mode) |
|
|
1513 | { |
|
|
1514 | // only used for intiial whitespace skipping |
|
|
1515 | case INCR_M_WS: |
|
|
1516 | for (;;) |
|
|
1517 | { |
|
|
1518 | if (*p > 0x20) |
|
|
1519 | { |
|
|
1520 | self->incr_mode = INCR_M_JSON; |
|
|
1521 | goto incr_m_json; |
|
|
1522 | } |
|
|
1523 | else if (!*p) |
|
|
1524 | goto interrupt; |
|
|
1525 | |
|
|
1526 | ++p; |
|
|
1527 | } |
|
|
1528 | |
|
|
1529 | // skip a single char inside a string (for \\-processing) |
|
|
1530 | case INCR_M_BS: |
|
|
1531 | if (!*p) |
|
|
1532 | goto interrupt; |
|
|
1533 | |
|
|
1534 | ++p; |
|
|
1535 | self->incr_mode = INCR_M_STR; |
|
|
1536 | goto incr_m_str; |
|
|
1537 | |
|
|
1538 | // inside a string |
|
|
1539 | case INCR_M_STR: |
|
|
1540 | incr_m_str: |
|
|
1541 | for (;;) |
|
|
1542 | { |
|
|
1543 | if (*p == '"') |
|
|
1544 | { |
|
|
1545 | ++p; |
|
|
1546 | self->incr_mode = INCR_M_JSON; |
|
|
1547 | |
|
|
1548 | if (!self->incr_nest) |
|
|
1549 | goto interrupt; |
|
|
1550 | |
|
|
1551 | goto incr_m_json; |
|
|
1552 | } |
|
|
1553 | else if (*p == '\\') |
|
|
1554 | { |
|
|
1555 | ++p; // "virtually" consumes character after \ |
|
|
1556 | |
|
|
1557 | if (!*p) // if at end of string we have to switch modes |
|
|
1558 | { |
|
|
1559 | self->incr_mode = INCR_M_BS; |
|
|
1560 | goto interrupt; |
|
|
1561 | } |
|
|
1562 | } |
|
|
1563 | else if (!*p) |
|
|
1564 | goto interrupt; |
|
|
1565 | |
|
|
1566 | ++p; |
|
|
1567 | } |
|
|
1568 | |
|
|
1569 | // after initial ws, outside string |
|
|
1570 | case INCR_M_JSON: |
|
|
1571 | incr_m_json: |
|
|
1572 | for (;;) |
|
|
1573 | { |
|
|
1574 | switch (*p++) |
|
|
1575 | { |
|
|
1576 | case 0: |
|
|
1577 | --p; |
|
|
1578 | goto interrupt; |
|
|
1579 | |
|
|
1580 | case 0x09: |
|
|
1581 | case 0x0a: |
|
|
1582 | case 0x0d: |
|
|
1583 | case 0x20: |
|
|
1584 | if (!self->incr_nest) |
|
|
1585 | { |
|
|
1586 | --p; // do not eat the whitespace, let the next round do it |
|
|
1587 | goto interrupt; |
|
|
1588 | } |
|
|
1589 | break; |
|
|
1590 | |
|
|
1591 | case '"': |
|
|
1592 | self->incr_mode = INCR_M_STR; |
|
|
1593 | goto incr_m_str; |
|
|
1594 | |
|
|
1595 | case '[': |
|
|
1596 | case '{': |
|
|
1597 | if (++self->incr_nest > self->max_depth) |
|
|
1598 | croak (ERR_NESTING_EXCEEDED); |
|
|
1599 | break; |
|
|
1600 | |
|
|
1601 | case ']': |
|
|
1602 | case '}': |
|
|
1603 | if (!--self->incr_nest) |
|
|
1604 | goto interrupt; |
|
|
1605 | } |
|
|
1606 | } |
|
|
1607 | } |
|
|
1608 | |
|
|
1609 | modechange: |
|
|
1610 | ; |
|
|
1611 | } |
|
|
1612 | |
|
|
1613 | interrupt: |
|
|
1614 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1615 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1024 | } |
1616 | } |
1025 | |
1617 | |
1026 | ///////////////////////////////////////////////////////////////////////////// |
1618 | ///////////////////////////////////////////////////////////////////////////// |
1027 | // XS interface functions |
1619 | // XS interface functions |
1028 | |
1620 | |
1029 | MODULE = JSON::XS PACKAGE = JSON::XS |
1621 | MODULE = JSON::XS PACKAGE = JSON::XS |
1030 | |
1622 | |
1031 | BOOT: |
1623 | BOOT: |
1032 | { |
1624 | { |
1033 | int i; |
1625 | int i; |
1034 | |
|
|
1035 | memset (decode_hexdigit, 0xff, 256); |
|
|
1036 | |
1626 | |
1037 | for (i = 0; i < 256; ++i) |
1627 | for (i = 0; i < 256; ++i) |
1038 | decode_hexdigit [i] = |
1628 | decode_hexdigit [i] = |
1039 | i >= '0' && i <= '9' ? i - '0' |
1629 | i >= '0' && i <= '9' ? i - '0' |
1040 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1630 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1041 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1631 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1042 | : -1; |
1632 | : -1; |
1043 | |
1633 | |
1044 | json_stash = gv_stashpv ("JSON::XS", 1); |
1634 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1635 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1636 | |
|
|
1637 | json_true = get_bool ("JSON::XS::true"); |
|
|
1638 | json_false = get_bool ("JSON::XS::false"); |
1045 | } |
1639 | } |
1046 | |
1640 | |
1047 | PROTOTYPES: DISABLE |
1641 | PROTOTYPES: DISABLE |
1048 | |
1642 | |
1049 | SV *new (char *dummy) |
1643 | void CLONE (...) |
1050 | CODE: |
1644 | CODE: |
1051 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1645 | json_stash = 0; |
|
|
1646 | json_boolean_stash = 0; |
|
|
1647 | |
|
|
1648 | void new (char *klass) |
|
|
1649 | PPCODE: |
|
|
1650 | { |
|
|
1651 | SV *pv = NEWSV (0, sizeof (JSON)); |
|
|
1652 | SvPOK_only (pv); |
|
|
1653 | json_init ((JSON *)SvPVX (pv)); |
|
|
1654 | XPUSHs (sv_2mortal (sv_bless ( |
|
|
1655 | newRV_noinc (pv), |
|
|
1656 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
|
|
1657 | ))); |
|
|
1658 | } |
|
|
1659 | |
|
|
1660 | void ascii (JSON *self, int enable = 1) |
|
|
1661 | ALIAS: |
|
|
1662 | ascii = F_ASCII |
|
|
1663 | latin1 = F_LATIN1 |
|
|
1664 | utf8 = F_UTF8 |
|
|
1665 | indent = F_INDENT |
|
|
1666 | canonical = F_CANONICAL |
|
|
1667 | space_before = F_SPACE_BEFORE |
|
|
1668 | space_after = F_SPACE_AFTER |
|
|
1669 | pretty = F_PRETTY |
|
|
1670 | allow_nonref = F_ALLOW_NONREF |
|
|
1671 | shrink = F_SHRINK |
|
|
1672 | allow_blessed = F_ALLOW_BLESSED |
|
|
1673 | convert_blessed = F_CONV_BLESSED |
|
|
1674 | relaxed = F_RELAXED |
|
|
1675 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
1676 | PPCODE: |
|
|
1677 | { |
|
|
1678 | if (enable) |
|
|
1679 | self->flags |= ix; |
|
|
1680 | else |
|
|
1681 | self->flags &= ~ix; |
|
|
1682 | |
|
|
1683 | XPUSHs (ST (0)); |
|
|
1684 | } |
|
|
1685 | |
|
|
1686 | void get_ascii (JSON *self) |
|
|
1687 | ALIAS: |
|
|
1688 | get_ascii = F_ASCII |
|
|
1689 | get_latin1 = F_LATIN1 |
|
|
1690 | get_utf8 = F_UTF8 |
|
|
1691 | get_indent = F_INDENT |
|
|
1692 | get_canonical = F_CANONICAL |
|
|
1693 | get_space_before = F_SPACE_BEFORE |
|
|
1694 | get_space_after = F_SPACE_AFTER |
|
|
1695 | get_allow_nonref = F_ALLOW_NONREF |
|
|
1696 | get_shrink = F_SHRINK |
|
|
1697 | get_allow_blessed = F_ALLOW_BLESSED |
|
|
1698 | get_convert_blessed = F_CONV_BLESSED |
|
|
1699 | get_relaxed = F_RELAXED |
|
|
1700 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
1701 | PPCODE: |
|
|
1702 | XPUSHs (boolSV (self->flags & ix)); |
|
|
1703 | |
|
|
1704 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
|
|
1705 | PPCODE: |
|
|
1706 | self->max_depth = max_depth; |
|
|
1707 | XPUSHs (ST (0)); |
|
|
1708 | |
|
|
1709 | U32 get_max_depth (JSON *self) |
|
|
1710 | CODE: |
|
|
1711 | RETVAL = self->max_depth; |
1052 | OUTPUT: |
1712 | OUTPUT: |
1053 | RETVAL |
1713 | RETVAL |
1054 | |
1714 | |
1055 | SV *ascii (SV *self, int enable = 1) |
1715 | void max_size (JSON *self, U32 max_size = 0) |
1056 | ALIAS: |
1716 | PPCODE: |
1057 | ascii = F_ASCII |
1717 | self->max_size = max_size; |
1058 | utf8 = F_UTF8 |
1718 | XPUSHs (ST (0)); |
1059 | indent = F_INDENT |
1719 | |
1060 | canonical = F_CANONICAL |
1720 | int get_max_size (JSON *self) |
1061 | space_before = F_SPACE_BEFORE |
|
|
1062 | space_after = F_SPACE_AFTER |
|
|
1063 | pretty = F_PRETTY |
|
|
1064 | allow_nonref = F_ALLOW_NONREF |
|
|
1065 | shrink = F_SHRINK |
|
|
1066 | CODE: |
1721 | CODE: |
1067 | { |
1722 | RETVAL = self->max_size; |
1068 | UV *uv = SvJSON (self); |
|
|
1069 | if (enable) |
|
|
1070 | *uv |= ix; |
|
|
1071 | else |
|
|
1072 | *uv &= ~ix; |
|
|
1073 | |
|
|
1074 | RETVAL = newSVsv (self); |
|
|
1075 | } |
|
|
1076 | OUTPUT: |
1723 | OUTPUT: |
1077 | RETVAL |
1724 | RETVAL |
1078 | |
1725 | |
1079 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
1726 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
|
|
1727 | PPCODE: |
|
|
1728 | { |
|
|
1729 | SvREFCNT_dec (self->cb_object); |
|
|
1730 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1731 | |
|
|
1732 | XPUSHs (ST (0)); |
|
|
1733 | } |
|
|
1734 | |
|
|
1735 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1736 | PPCODE: |
|
|
1737 | { |
|
|
1738 | if (!self->cb_sk_object) |
|
|
1739 | self->cb_sk_object = newHV (); |
|
|
1740 | |
|
|
1741 | if (SvOK (cb)) |
|
|
1742 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1743 | else |
|
|
1744 | { |
|
|
1745 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1746 | |
|
|
1747 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1748 | { |
|
|
1749 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1750 | self->cb_sk_object = 0; |
|
|
1751 | } |
|
|
1752 | } |
|
|
1753 | |
|
|
1754 | XPUSHs (ST (0)); |
|
|
1755 | } |
|
|
1756 | |
|
|
1757 | void encode (JSON *self, SV *scalar) |
|
|
1758 | PPCODE: |
|
|
1759 | XPUSHs (encode_json (scalar, self)); |
|
|
1760 | |
|
|
1761 | void decode (JSON *self, SV *jsonstr) |
|
|
1762 | PPCODE: |
|
|
1763 | XPUSHs (decode_json (jsonstr, self, 0)); |
|
|
1764 | |
|
|
1765 | void decode_prefix (JSON *self, SV *jsonstr) |
|
|
1766 | PPCODE: |
|
|
1767 | { |
|
|
1768 | STRLEN offset; |
|
|
1769 | EXTEND (SP, 2); |
|
|
1770 | PUSHs (decode_json (jsonstr, self, &offset)); |
|
|
1771 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1772 | } |
|
|
1773 | |
|
|
1774 | void incr_parse (JSON *self, SV *jsonstr = 0) |
|
|
1775 | PPCODE: |
|
|
1776 | { |
|
|
1777 | if (!self->incr_text) |
|
|
1778 | self->incr_text = newSVpvn ("", 0); |
|
|
1779 | |
|
|
1780 | // append data, if any |
|
|
1781 | if (jsonstr) |
|
|
1782 | { |
|
|
1783 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
|
|
1784 | { |
|
|
1785 | /* utf-8-ness differs, need to upgrade */ |
|
|
1786 | sv_utf8_upgrade (self->incr_text); |
|
|
1787 | |
|
|
1788 | if (self->incr_pos) |
|
|
1789 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1790 | - (U8 *)SvPVX (self->incr_text); |
|
|
1791 | } |
|
|
1792 | |
|
|
1793 | { |
|
|
1794 | STRLEN len; |
|
|
1795 | const char *str = SvPV (jsonstr, len); |
|
|
1796 | SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); |
|
|
1797 | Move (str, SvEND (self->incr_text), len, char); |
|
|
1798 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
|
|
1799 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1800 | } |
|
|
1801 | } |
|
|
1802 | |
|
|
1803 | if (GIMME_V != G_VOID) |
|
|
1804 | do |
|
|
1805 | { |
|
|
1806 | STRLEN offset; |
|
|
1807 | |
|
|
1808 | if (!INCR_DONE (self)) |
|
|
1809 | { |
|
|
1810 | incr_parse (self); |
|
|
1811 | |
|
|
1812 | if (self->incr_pos > self->max_size && self->max_size) |
|
|
1813 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1814 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
|
|
1815 | |
|
|
1816 | if (!INCR_DONE (self)) |
|
|
1817 | break; |
|
|
1818 | } |
|
|
1819 | |
|
|
1820 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
|
|
1821 | |
|
|
1822 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1823 | self->incr_pos -= offset; |
|
|
1824 | self->incr_nest = 0; |
|
|
1825 | self->incr_mode = 0; |
|
|
1826 | } |
|
|
1827 | while (GIMME_V == G_ARRAY); |
|
|
1828 | } |
|
|
1829 | |
|
|
1830 | SV *incr_text (JSON *self) |
|
|
1831 | ATTRS: lvalue |
1080 | CODE: |
1832 | CODE: |
1081 | { |
1833 | { |
1082 | UV *uv = SvJSON (self); |
1834 | if (self->incr_pos) |
1083 | UV log2 = 0; |
1835 | croak ("incr_text can not be called when the incremental parser already started parsing"); |
1084 | |
1836 | |
1085 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1837 | RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef; |
1086 | |
|
|
1087 | while ((1UL << log2) < max_depth) |
|
|
1088 | ++log2; |
|
|
1089 | |
|
|
1090 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1091 | |
|
|
1092 | RETVAL = newSVsv (self); |
|
|
1093 | } |
1838 | } |
1094 | OUTPUT: |
1839 | OUTPUT: |
1095 | RETVAL |
1840 | RETVAL |
1096 | |
1841 | |
|
|
1842 | void incr_skip (JSON *self) |
|
|
1843 | CODE: |
|
|
1844 | { |
|
|
1845 | if (self->incr_pos) |
|
|
1846 | { |
|
|
1847 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + self->incr_pos); |
|
|
1848 | self->incr_pos = 0; |
|
|
1849 | self->incr_nest = 0; |
|
|
1850 | self->incr_mode = 0; |
|
|
1851 | } |
|
|
1852 | } |
|
|
1853 | |
|
|
1854 | void incr_reset (JSON *self) |
|
|
1855 | CODE: |
|
|
1856 | { |
|
|
1857 | SvREFCNT_dec (self->incr_text); |
|
|
1858 | self->incr_text = 0; |
|
|
1859 | self->incr_pos = 0; |
|
|
1860 | self->incr_nest = 0; |
|
|
1861 | self->incr_mode = 0; |
|
|
1862 | } |
|
|
1863 | |
|
|
1864 | void DESTROY (JSON *self) |
|
|
1865 | CODE: |
|
|
1866 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1867 | SvREFCNT_dec (self->cb_object); |
|
|
1868 | SvREFCNT_dec (self->incr_text); |
|
|
1869 | |
|
|
1870 | PROTOTYPES: ENABLE |
|
|
1871 | |
1097 | void encode (SV *self, SV *scalar) |
1872 | void encode_json (SV *scalar) |
|
|
1873 | ALIAS: |
|
|
1874 | to_json_ = 0 |
|
|
1875 | encode_json = F_UTF8 |
1098 | PPCODE: |
1876 | PPCODE: |
|
|
1877 | { |
|
|
1878 | JSON json; |
|
|
1879 | json_init (&json); |
|
|
1880 | json.flags |= ix; |
1099 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1881 | XPUSHs (encode_json (scalar, &json)); |
|
|
1882 | } |
1100 | |
1883 | |
1101 | void decode (SV *self, SV *jsonstr) |
1884 | void decode_json (SV *jsonstr) |
|
|
1885 | ALIAS: |
|
|
1886 | from_json_ = 0 |
|
|
1887 | decode_json = F_UTF8 |
1102 | PPCODE: |
1888 | PPCODE: |
|
|
1889 | { |
|
|
1890 | JSON json; |
|
|
1891 | json_init (&json); |
|
|
1892 | json.flags |= ix; |
1103 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1893 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1894 | } |
1104 | |
1895 | |
1105 | PROTOTYPES: ENABLE |
|
|
1106 | |
1896 | |
1107 | void to_json (SV *scalar) |
|
|
1108 | ALIAS: |
|
|
1109 | objToJson = 0 |
|
|
1110 | PPCODE: |
|
|
1111 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
|
|
1112 | |
|
|
1113 | void from_json (SV *jsonstr) |
|
|
1114 | ALIAS: |
|
|
1115 | jsonToObj = 0 |
|
|
1116 | PPCODE: |
|
|
1117 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
|
|
1118 | |
|
|