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