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