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