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