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