… | |
… | |
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" |
8 | |
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
|
|
13 | |
9 | #define F_ASCII 0x00000001 |
14 | #define F_ASCII 0x00000001UL |
10 | #define F_UTF8 0x00000002 |
15 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004 |
16 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008 |
17 | #define F_CANONICAL 0x00000008UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
18 | #define F_SPACE_BEFORE 0x00000010UL |
14 | #define F_SPACE_AFTER 0x00000020 |
19 | #define F_SPACE_AFTER 0x00000020UL |
15 | #define F_JSON_RPC 0x00000040 |
|
|
16 | #define F_ALLOW_NONREF 0x00000080 |
20 | #define F_ALLOW_NONREF 0x00000080UL |
|
|
21 | #define F_SHRINK 0x00000100UL |
|
|
22 | #define F_MAXDEPTH 0xf8000000UL |
|
|
23 | #define S_MAXDEPTH 27 |
|
|
24 | |
|
|
25 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
26 | |
|
|
27 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
28 | // F_BLESSED? <=> { $__class__$ => } |
17 | |
29 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
30 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
31 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
20 | |
32 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
33 | #define INIT_SIZE 32 // initial scalar size to be allocated |
|
|
34 | #define INDENT_STEP 3 // spaces per indentation level |
|
|
35 | |
|
|
36 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
22 | |
37 | |
23 | #define SB do { |
38 | #define SB do { |
24 | #define SE } while (0) |
39 | #define SE } while (0) |
25 | |
40 | |
26 | static HV *json_stash; |
41 | static HV *json_stash; // JSON::XS:: |
|
|
42 | |
|
|
43 | ///////////////////////////////////////////////////////////////////////////// |
|
|
44 | // utility functions |
|
|
45 | |
|
|
46 | static UV * |
|
|
47 | SvJSON (SV *sv) |
|
|
48 | { |
|
|
49 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
50 | croak ("object is not of type JSON::XS"); |
|
|
51 | |
|
|
52 | return &SvUVX (SvRV (sv)); |
|
|
53 | } |
|
|
54 | |
|
|
55 | static void |
|
|
56 | shrink (SV *sv) |
|
|
57 | { |
|
|
58 | sv_utf8_downgrade (sv, 1); |
|
|
59 | if (SvLEN (sv) > SvCUR (sv) + 1) |
|
|
60 | { |
|
|
61 | #ifdef SvPV_shrink_to_cur |
|
|
62 | SvPV_shrink_to_cur (sv); |
|
|
63 | #elif defined (SvPV_renew) |
|
|
64 | SvPV_renew (sv, SvCUR (sv) + 1); |
|
|
65 | #endif |
|
|
66 | } |
|
|
67 | } |
|
|
68 | |
|
|
69 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
70 | // case of an error. |
|
|
71 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
72 | // but use the very good perl function to parse anything else. |
|
|
73 | // note that we never call this function for a ascii codepoints |
|
|
74 | static UV |
|
|
75 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
76 | { |
|
|
77 | if (s[0] > 0xdf || s[0] < 0xc2) |
|
|
78 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
79 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
80 | { |
|
|
81 | *clen = 2; |
|
|
82 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
83 | } |
|
|
84 | else |
|
|
85 | { |
|
|
86 | *clen = (STRLEN)-1; |
|
|
87 | return (UV)-1; |
|
|
88 | } |
|
|
89 | } |
|
|
90 | |
|
|
91 | ///////////////////////////////////////////////////////////////////////////// |
|
|
92 | // encoder |
27 | |
93 | |
28 | // structure used for encoding JSON |
94 | // structure used for encoding JSON |
29 | typedef struct |
95 | typedef struct |
30 | { |
96 | { |
31 | char *cur; |
97 | char *cur; // SvPVX (sv) + current output position |
32 | STRLEN len; // SvLEN (sv) |
|
|
33 | char *end; // SvEND (sv) |
98 | char *end; // SvEND (sv) |
34 | SV *sv; |
99 | SV *sv; // result scalar |
35 | UV flags; |
100 | U32 flags; // F_* |
36 | int max_recurse; |
101 | U32 indent; // indentation level |
37 | int indent; |
102 | U32 maxdepth; // max. indentation/recursion level |
38 | } enc_t; |
103 | } enc_t; |
39 | |
|
|
40 | // structure used for decoding JSON |
|
|
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 | |
104 | |
60 | static void |
105 | static void |
61 | need (enc_t *enc, STRLEN len) |
106 | need (enc_t *enc, STRLEN len) |
62 | { |
107 | { |
63 | if (enc->cur + len >= enc->end) |
108 | if (enc->cur + len >= enc->end) |
64 | { |
109 | { |
65 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
110 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
66 | SvGROW (enc->sv, cur + len + 1); |
111 | SvGROW (enc->sv, cur + len + 1); |
67 | enc->cur = SvPVX (enc->sv) + cur; |
112 | enc->cur = SvPVX (enc->sv) + cur; |
68 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
113 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
69 | } |
114 | } |
70 | } |
115 | } |
71 | |
116 | |
72 | static void |
117 | static void |
73 | encode_ch (enc_t *enc, char ch) |
118 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
85 | |
130 | |
86 | while (str < end) |
131 | while (str < end) |
87 | { |
132 | { |
88 | unsigned char ch = *(unsigned char *)str; |
133 | unsigned char ch = *(unsigned char *)str; |
89 | |
134 | |
90 | if (ch == '"') |
135 | if (ch >= 0x20 && ch < 0x80) // most common case |
91 | { |
136 | { |
|
|
137 | if (ch == '"') // but with slow exceptions |
|
|
138 | { |
92 | need (enc, len += 1); |
139 | need (enc, len += 1); |
93 | *enc->cur++ = '\\'; |
140 | *enc->cur++ = '\\'; |
94 | *enc->cur++ = '"'; |
141 | *enc->cur++ = '"'; |
95 | ++str; |
|
|
96 | } |
142 | } |
97 | else if (ch == '\\') |
143 | else if (ch == '\\') |
98 | { |
144 | { |
99 | need (enc, len += 1); |
145 | need (enc, len += 1); |
100 | *enc->cur++ = '\\'; |
146 | *enc->cur++ = '\\'; |
101 | *enc->cur++ = '\\'; |
147 | *enc->cur++ = '\\'; |
102 | ++str; |
|
|
103 | } |
148 | } |
104 | else if (ch >= 0x20 && ch < 0x80) // most common case |
149 | else |
105 | { |
|
|
106 | *enc->cur++ = ch; |
150 | *enc->cur++ = ch; |
107 | ++str; |
151 | |
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; |
152 | ++str; |
122 | } |
153 | } |
123 | else |
154 | else |
124 | { |
155 | { |
125 | STRLEN clen; |
156 | switch (ch) |
126 | UV uch; |
|
|
127 | |
|
|
128 | if (is_utf8) |
|
|
129 | { |
157 | { |
130 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
158 | case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; |
131 | if (clen < 0) |
159 | 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"); |
160 | case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
133 | } |
161 | case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
134 | else |
162 | case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
135 | { |
|
|
136 | uch = ch; |
|
|
137 | clen = 1; |
|
|
138 | } |
|
|
139 | |
163 | |
140 | if (uch < 0x80 || enc->flags & F_ASCII) |
164 | default: |
141 | { |
|
|
142 | if (uch > 0xFFFFUL) |
|
|
143 | { |
165 | { |
|
|
166 | STRLEN clen; |
|
|
167 | UV uch; |
|
|
168 | |
|
|
169 | if (is_utf8) |
|
|
170 | { |
|
|
171 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
172 | uch = decode_utf8 (str, end - str, &clen); |
|
|
173 | if (clen == (STRLEN)-1) |
|
|
174 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
|
|
175 | } |
|
|
176 | else |
|
|
177 | { |
|
|
178 | uch = ch; |
|
|
179 | clen = 1; |
|
|
180 | } |
|
|
181 | |
|
|
182 | if (uch > 0x10FFFFUL) |
|
|
183 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
184 | |
|
|
185 | if (uch < 0x80 || enc->flags & F_ASCII) |
|
|
186 | { |
|
|
187 | if (uch > 0xFFFFUL) |
|
|
188 | { |
144 | need (enc, len += 11); |
189 | need (enc, len += 11); |
145 | sprintf (enc->cur, "\\u%04x\\u%04x", |
190 | sprintf (enc->cur, "\\u%04x\\u%04x", |
146 | (uch - 0x10000) / 0x400 + 0xD800, |
191 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
147 | (uch - 0x10000) % 0x400 + 0xDC00); |
192 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
148 | enc->cur += 12; |
193 | enc->cur += 12; |
|
|
194 | } |
|
|
195 | else |
|
|
196 | { |
|
|
197 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
198 | need (enc, len += 5); |
|
|
199 | *enc->cur++ = '\\'; |
|
|
200 | *enc->cur++ = 'u'; |
|
|
201 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
|
|
202 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
|
|
203 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
|
|
204 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
|
|
205 | } |
|
|
206 | |
|
|
207 | str += clen; |
|
|
208 | } |
|
|
209 | else if (is_utf8) |
|
|
210 | { |
|
|
211 | need (enc, len += clen); |
|
|
212 | do |
|
|
213 | { |
|
|
214 | *enc->cur++ = *str++; |
|
|
215 | } |
|
|
216 | while (--clen); |
|
|
217 | } |
|
|
218 | else |
|
|
219 | { |
|
|
220 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
|
|
221 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
222 | ++str; |
|
|
223 | } |
149 | } |
224 | } |
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 | while (clen--) |
|
|
168 | *enc->cur++ = *str++; |
|
|
169 | } |
|
|
170 | else |
|
|
171 | { |
|
|
172 | need (enc, 10); // never more than 11 bytes needed |
|
|
173 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
174 | ++str; |
|
|
175 | } |
225 | } |
176 | } |
226 | } |
177 | |
227 | |
178 | --len; |
228 | --len; |
179 | } |
229 | } |
180 | } |
230 | } |
181 | |
231 | |
182 | #define INDENT SB \ |
232 | static void |
|
|
233 | encode_indent (enc_t *enc) |
|
|
234 | { |
183 | if (enc->flags & F_INDENT) \ |
235 | if (enc->flags & F_INDENT) |
184 | { \ |
236 | { |
185 | int i_; \ |
237 | int spaces = enc->indent * INDENT_STEP; |
186 | need (enc, enc->indent); \ |
|
|
187 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
188 | encode_ch (enc, ' '); \ |
|
|
189 | } \ |
|
|
190 | SE |
|
|
191 | |
238 | |
192 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
239 | need (enc, spaces); |
193 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
240 | memset (enc->cur, ' ', spaces); |
194 | #define COMMA SB \ |
241 | enc->cur += spaces; |
|
|
242 | } |
|
|
243 | } |
|
|
244 | |
|
|
245 | static void |
|
|
246 | encode_space (enc_t *enc) |
|
|
247 | { |
|
|
248 | need (enc, 1); |
195 | encode_ch (enc, ','); \ |
249 | encode_ch (enc, ' '); |
|
|
250 | } |
|
|
251 | |
|
|
252 | static void |
|
|
253 | encode_nl (enc_t *enc) |
|
|
254 | { |
196 | if (enc->flags & F_INDENT) \ |
255 | if (enc->flags & F_INDENT) |
197 | NL; \ |
256 | { |
|
|
257 | need (enc, 1); |
|
|
258 | encode_ch (enc, '\n'); |
|
|
259 | } |
|
|
260 | } |
|
|
261 | |
|
|
262 | static void |
|
|
263 | encode_comma (enc_t *enc) |
|
|
264 | { |
|
|
265 | encode_ch (enc, ','); |
|
|
266 | |
|
|
267 | if (enc->flags & F_INDENT) |
|
|
268 | encode_nl (enc); |
198 | else if (enc->flags & F_SPACE_AFTER) \ |
269 | else if (enc->flags & F_SPACE_AFTER) |
199 | SPACE; \ |
270 | encode_space (enc); |
200 | SE |
271 | } |
201 | |
272 | |
202 | static void encode_sv (enc_t *enc, SV *sv); |
273 | static void encode_sv (enc_t *enc, SV *sv); |
203 | |
274 | |
204 | static void |
275 | static void |
205 | encode_av (enc_t *enc, AV *av) |
276 | encode_av (enc_t *enc, AV *av) |
206 | { |
277 | { |
207 | int i, len = av_len (av); |
278 | int i, len = av_len (av); |
208 | |
279 | |
209 | encode_ch (enc, '['); NL; |
280 | if (enc->indent >= enc->maxdepth) |
|
|
281 | croak ("data structure too deep (hit recursion limit)"); |
|
|
282 | |
|
|
283 | encode_ch (enc, '['); encode_nl (enc); |
210 | ++enc->indent; |
284 | ++enc->indent; |
211 | |
285 | |
212 | for (i = 0; i <= len; ++i) |
286 | for (i = 0; i <= len; ++i) |
213 | { |
287 | { |
214 | INDENT; |
288 | encode_indent (enc); |
215 | encode_sv (enc, *av_fetch (av, i, 0)); |
289 | encode_sv (enc, *av_fetch (av, i, 0)); |
216 | |
290 | |
217 | if (i < len) |
291 | if (i < len) |
218 | COMMA; |
292 | encode_comma (enc); |
219 | } |
293 | } |
220 | |
294 | |
221 | NL; |
295 | encode_nl (enc); |
222 | |
296 | |
223 | --enc->indent; |
297 | --enc->indent; |
224 | INDENT; encode_ch (enc, ']'); |
298 | encode_indent (enc); encode_ch (enc, ']'); |
225 | } |
299 | } |
226 | |
300 | |
227 | static void |
301 | static void |
228 | encode_he (enc_t *enc, HE *he) |
302 | encode_he (enc_t *enc, HE *he) |
229 | { |
303 | { |
… | |
… | |
243 | else |
317 | else |
244 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
318 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
245 | |
319 | |
246 | encode_ch (enc, '"'); |
320 | encode_ch (enc, '"'); |
247 | |
321 | |
248 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
322 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
249 | encode_ch (enc, ':'); |
323 | encode_ch (enc, ':'); |
250 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
324 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
251 | encode_sv (enc, HeVAL (he)); |
325 | encode_sv (enc, HeVAL (he)); |
252 | } |
326 | } |
253 | |
327 | |
254 | // compare hash entries, used when all keys are bytestrings |
328 | // compare hash entries, used when all keys are bytestrings |
255 | static int |
329 | static int |
… | |
… | |
261 | HE *b = *(HE **)b_; |
335 | HE *b = *(HE **)b_; |
262 | |
336 | |
263 | STRLEN la = HeKLEN (a); |
337 | STRLEN la = HeKLEN (a); |
264 | STRLEN lb = HeKLEN (b); |
338 | STRLEN lb = HeKLEN (b); |
265 | |
339 | |
266 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
340 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
267 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
341 | cmp = la - lb; |
268 | |
342 | |
269 | return cmp; |
343 | return cmp; |
270 | } |
344 | } |
271 | |
345 | |
272 | // compare hash entries, used when some keys are sv's or utf-x |
346 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
279 | static void |
353 | static void |
280 | encode_hv (enc_t *enc, HV *hv) |
354 | encode_hv (enc_t *enc, HV *hv) |
281 | { |
355 | { |
282 | int count, i; |
356 | int count, i; |
283 | |
357 | |
|
|
358 | if (enc->indent >= enc->maxdepth) |
|
|
359 | croak ("data structure too deep (hit recursion limit)"); |
|
|
360 | |
284 | encode_ch (enc, '{'); NL; ++enc->indent; |
361 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
285 | |
362 | |
286 | if ((count = hv_iterinit (hv))) |
363 | if ((count = hv_iterinit (hv))) |
287 | { |
364 | { |
288 | // for canonical output we have to sort by keys first |
365 | // for canonical output we have to sort by keys first |
289 | // actually, this is mostly due to the stupid so-called |
366 | // actually, this is mostly due to the stupid so-called |
290 | // security workaround added somewhere in 5.8.x. |
367 | // security workaround added somewhere in 5.8.x. |
291 | // that randomises hash orderings |
368 | // that randomises hash orderings |
292 | if (enc->flags & F_CANONICAL) |
369 | if (enc->flags & F_CANONICAL) |
293 | { |
370 | { |
294 | HE *he, *hes [count]; |
371 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
295 | int fast = 1; |
372 | int fast = 1; |
296 | |
373 | |
297 | i = 0; |
374 | i = 0; |
298 | while ((he = hv_iternext (hv))) |
375 | while ((he = hv_iternext (hv))) |
299 | { |
376 | { |
… | |
… | |
306 | |
383 | |
307 | if (fast) |
384 | if (fast) |
308 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
385 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
309 | else |
386 | else |
310 | { |
387 | { |
311 | // hack to disable "use bytes" |
388 | // hack to forcefully disable "use bytes" |
312 | COP *oldcop = PL_curcop, cop; |
389 | COP cop = *PL_curcop; |
313 | cop.op_private = 0; |
390 | cop.op_private = 0; |
|
|
391 | |
|
|
392 | ENTER; |
|
|
393 | SAVETMPS; |
|
|
394 | |
|
|
395 | SAVEVPTR (PL_curcop); |
314 | PL_curcop = &cop; |
396 | PL_curcop = &cop; |
315 | |
397 | |
316 | SAVETMPS; |
|
|
317 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
398 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
399 | |
318 | FREETMPS; |
400 | FREETMPS; |
319 | |
401 | LEAVE; |
320 | PL_curcop = oldcop; |
|
|
321 | } |
402 | } |
322 | |
403 | |
323 | for (i = 0; i < count; ++i) |
404 | for (i = 0; i < count; ++i) |
324 | { |
405 | { |
325 | INDENT; |
406 | encode_indent (enc); |
326 | encode_he (enc, hes [i]); |
407 | encode_he (enc, hes [i]); |
327 | |
408 | |
328 | if (i < count - 1) |
409 | if (i < count - 1) |
329 | COMMA; |
410 | encode_comma (enc); |
330 | } |
411 | } |
331 | |
412 | |
332 | NL; |
413 | encode_nl (enc); |
333 | } |
414 | } |
334 | else |
415 | else |
335 | { |
416 | { |
336 | SV *sv; |
|
|
337 | HE *he = hv_iternext (hv); |
417 | HE *he = hv_iternext (hv); |
338 | |
418 | |
339 | for (;;) |
419 | for (;;) |
340 | { |
420 | { |
341 | INDENT; |
421 | encode_indent (enc); |
342 | encode_he (enc, he); |
422 | encode_he (enc, he); |
343 | |
423 | |
344 | if (!(he = hv_iternext (hv))) |
424 | if (!(he = hv_iternext (hv))) |
345 | break; |
425 | break; |
346 | |
426 | |
347 | COMMA; |
427 | encode_comma (enc); |
348 | } |
428 | } |
349 | |
429 | |
350 | NL; |
430 | encode_nl (enc); |
351 | } |
431 | } |
352 | } |
432 | } |
353 | |
433 | |
354 | --enc->indent; INDENT; encode_ch (enc, '}'); |
434 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
435 | } |
|
|
436 | |
|
|
437 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
438 | static void |
|
|
439 | encode_rv (enc_t *enc, SV *sv) |
|
|
440 | { |
|
|
441 | svtype svt; |
|
|
442 | |
|
|
443 | SvGETMAGIC (sv); |
|
|
444 | svt = SvTYPE (sv); |
|
|
445 | |
|
|
446 | if (svt == SVt_PVHV) |
|
|
447 | encode_hv (enc, (HV *)sv); |
|
|
448 | else if (svt == SVt_PVAV) |
|
|
449 | encode_av (enc, (AV *)sv); |
|
|
450 | else if (svt < SVt_PVAV) |
|
|
451 | { |
|
|
452 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
453 | encode_str (enc, "false", 5, 0); |
|
|
454 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
455 | encode_str (enc, "true", 4, 0); |
|
|
456 | else |
|
|
457 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
458 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
459 | } |
|
|
460 | else |
|
|
461 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
462 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
355 | } |
463 | } |
356 | |
464 | |
357 | static void |
465 | static void |
358 | encode_sv (enc_t *enc, SV *sv) |
466 | encode_sv (enc_t *enc, SV *sv) |
359 | { |
467 | { |
… | |
… | |
380 | SvIsUV(sv) |
488 | SvIsUV(sv) |
381 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
489 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
382 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
490 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
383 | } |
491 | } |
384 | else if (SvROK (sv)) |
492 | else if (SvROK (sv)) |
385 | { |
493 | encode_rv (enc, SvRV (sv)); |
386 | if (!--enc->max_recurse) |
|
|
387 | croak ("data structure too deep (hit recursion limit)"); |
|
|
388 | |
|
|
389 | sv = SvRV (sv); |
|
|
390 | |
|
|
391 | switch (SvTYPE (sv)) |
|
|
392 | { |
|
|
393 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
|
|
394 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
|
|
395 | |
|
|
396 | default: |
|
|
397 | croak ("JSON can only represent references to arrays or hashes"); |
|
|
398 | } |
|
|
399 | } |
|
|
400 | else if (!SvOK (sv)) |
494 | else if (!SvOK (sv)) |
401 | encode_str (enc, "null", 4, 0); |
495 | encode_str (enc, "null", 4, 0); |
402 | else |
496 | else |
403 | croak ("encountered perl type that JSON cannot handle"); |
497 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
|
|
498 | SvPV_nolen (sv), SvFLAGS (sv)); |
404 | } |
499 | } |
405 | |
500 | |
406 | static SV * |
501 | static SV * |
407 | encode_json (SV *scalar, UV flags) |
502 | encode_json (SV *scalar, U32 flags) |
408 | { |
503 | { |
|
|
504 | enc_t enc; |
|
|
505 | |
409 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
506 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
410 | croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); |
507 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
411 | |
508 | |
412 | enc_t enc; |
|
|
413 | enc.flags = flags; |
509 | enc.flags = flags; |
414 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
510 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
415 | enc.cur = SvPVX (enc.sv); |
511 | enc.cur = SvPVX (enc.sv); |
416 | enc.end = SvEND (enc.sv); |
512 | enc.end = SvEND (enc.sv); |
417 | enc.max_recurse = 0; |
|
|
418 | enc.indent = 0; |
513 | enc.indent = 0; |
|
|
514 | enc.maxdepth = DEC_DEPTH (flags); |
419 | |
515 | |
420 | SvPOK_only (enc.sv); |
516 | SvPOK_only (enc.sv); |
421 | encode_sv (&enc, scalar); |
517 | encode_sv (&enc, scalar); |
422 | |
518 | |
423 | if (!(flags & (F_ASCII | F_UTF8))) |
519 | if (!(flags & (F_ASCII | F_UTF8))) |
424 | SvUTF8_on (enc.sv); |
520 | SvUTF8_on (enc.sv); |
425 | |
521 | |
426 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
522 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
523 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
524 | |
|
|
525 | if (enc.flags & F_SHRINK) |
|
|
526 | shrink (enc.sv); |
|
|
527 | |
427 | return enc.sv; |
528 | return enc.sv; |
428 | } |
529 | } |
429 | |
530 | |
430 | ///////////////////////////////////////////////////////////////////////////// |
531 | ///////////////////////////////////////////////////////////////////////////// |
|
|
532 | // decoder |
431 | |
533 | |
432 | #define WS \ |
534 | // structure used for decoding JSON |
|
|
535 | typedef struct |
|
|
536 | { |
|
|
537 | char *cur; // current parser pointer |
|
|
538 | char *end; // end of input string |
|
|
539 | const char *err; // parse error, if != 0 |
|
|
540 | U32 flags; // F_* |
|
|
541 | U32 depth; // recursion depth |
|
|
542 | U32 maxdepth; // recursion depth limit |
|
|
543 | } dec_t; |
|
|
544 | |
|
|
545 | static void |
|
|
546 | decode_ws (dec_t *dec) |
|
|
547 | { |
433 | for (;;) \ |
548 | for (;;) |
434 | { \ |
549 | { |
435 | char ch = *dec->cur; \ |
550 | char ch = *dec->cur; |
|
|
551 | |
436 | if (ch > 0x20 \ |
552 | if (ch > 0x20 |
437 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
553 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
438 | break; \ |
554 | break; |
|
|
555 | |
439 | ++dec->cur; \ |
556 | ++dec->cur; |
440 | } |
557 | } |
|
|
558 | } |
441 | |
559 | |
442 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
560 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
561 | |
443 | #define EXPECT_CH(ch) SB \ |
562 | #define EXPECT_CH(ch) SB \ |
444 | if (*dec->cur != ch) \ |
563 | if (*dec->cur != ch) \ |
445 | ERR (# ch " expected"); \ |
564 | ERR (# ch " expected"); \ |
446 | ++dec->cur; \ |
565 | ++dec->cur; \ |
447 | SE |
566 | SE |
448 | |
567 | |
|
|
568 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
569 | #define DEC_DEC_DEPTH --dec->depth |
|
|
570 | |
449 | static SV *decode_sv (dec_t *dec); |
571 | static SV *decode_sv (dec_t *dec); |
450 | |
572 | |
451 | static signed char decode_hexdigit[256]; |
573 | static signed char decode_hexdigit[256]; |
452 | |
574 | |
453 | static UV |
575 | static UV |
454 | decode_4hex (dec_t *dec) |
576 | decode_4hex (dec_t *dec) |
455 | { |
577 | { |
456 | signed char d1, d2, d3, d4; |
578 | signed char d1, d2, d3, d4; |
|
|
579 | unsigned char *cur = (unsigned char *)dec->cur; |
457 | |
580 | |
458 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
459 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
581 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
460 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
|
|
461 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
582 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
462 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
463 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
583 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
464 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
465 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
584 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
466 | |
585 | |
467 | dec->cur += 4; |
586 | dec->cur += 4; |
468 | |
587 | |
469 | return ((UV)d1) << 12 |
588 | return ((UV)d1) << 12 |
470 | | ((UV)d2) << 8 |
589 | | ((UV)d2) << 8 |
… | |
… | |
473 | |
592 | |
474 | fail: |
593 | fail: |
475 | return (UV)-1; |
594 | return (UV)-1; |
476 | } |
595 | } |
477 | |
596 | |
478 | #define APPEND_GROW(n) SB \ |
|
|
479 | if (cur + (n) >= end) \ |
|
|
480 | { \ |
|
|
481 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
482 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
483 | cur = SvPVX (sv) + ofs; \ |
|
|
484 | end = SvEND (sv); \ |
|
|
485 | } \ |
|
|
486 | SE |
|
|
487 | |
|
|
488 | #define APPEND_CH(ch) SB \ |
|
|
489 | APPEND_GROW (1); \ |
|
|
490 | *cur++ = (ch); \ |
|
|
491 | SE |
|
|
492 | |
|
|
493 | static SV * |
597 | static SV * |
494 | decode_str (dec_t *dec) |
598 | decode_str (dec_t *dec) |
495 | { |
599 | { |
496 | SV *sv = NEWSV (0,2); |
600 | SV *sv = 0; |
497 | int utf8 = 0; |
601 | int utf8 = 0; |
498 | char *cur = SvPVX (sv); |
|
|
499 | char *end = SvEND (sv); |
|
|
500 | |
602 | |
501 | for (;;) |
603 | do |
502 | { |
604 | { |
503 | unsigned char ch = *(unsigned char *)dec->cur; |
605 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
|
|
606 | char *cur = buf; |
504 | |
607 | |
505 | if (ch == '"') |
608 | do |
506 | break; |
|
|
507 | else if (ch == '\\') |
|
|
508 | { |
609 | { |
509 | switch (*++dec->cur) |
610 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
611 | |
|
|
612 | if (ch == '"') |
510 | { |
613 | { |
511 | case '\\': |
614 | --dec->cur; |
512 | case '/': |
615 | break; |
513 | case '"': APPEND_CH (*dec->cur++); break; |
616 | } |
514 | |
617 | else if (ch == '\\') |
515 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
618 | { |
516 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
619 | switch (*dec->cur) |
517 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
518 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
519 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
520 | |
|
|
521 | case 'u': |
|
|
522 | { |
620 | { |
523 | UV lo, hi; |
621 | case '\\': |
524 | ++dec->cur; |
622 | case '/': |
|
|
623 | case '"': *cur++ = *dec->cur++; break; |
525 | |
624 | |
526 | hi = decode_4hex (dec); |
625 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
527 | if (hi == (UV)-1) |
626 | case 't': ++dec->cur; *cur++ = '\011'; break; |
528 | goto fail; |
627 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
|
|
628 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
|
|
629 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
529 | |
630 | |
530 | // possibly a surrogate pair |
631 | case 'u': |
531 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
532 | { |
632 | { |
533 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
633 | UV lo, hi; |
534 | ERR ("illegal surrogate character"); |
|
|
535 | |
|
|
536 | dec->cur += 2; |
634 | ++dec->cur; |
537 | |
635 | |
538 | lo = decode_4hex (dec); |
636 | hi = decode_4hex (dec); |
539 | if (lo == (UV)-1) |
637 | if (hi == (UV)-1) |
540 | goto fail; |
638 | goto fail; |
541 | |
639 | |
|
|
640 | // possibly a surrogate pair |
|
|
641 | if (hi >= 0xd800) |
|
|
642 | if (hi < 0xdc00) |
|
|
643 | { |
|
|
644 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
|
|
645 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
646 | |
|
|
647 | dec->cur += 2; |
|
|
648 | |
|
|
649 | lo = decode_4hex (dec); |
|
|
650 | if (lo == (UV)-1) |
|
|
651 | goto fail; |
|
|
652 | |
542 | if (lo < 0xdc00 || lo >= 0xe000) |
653 | if (lo < 0xdc00 || lo >= 0xe000) |
543 | ERR ("surrogate pair expected"); |
654 | ERR ("surrogate pair expected"); |
544 | |
655 | |
545 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
656 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
657 | } |
|
|
658 | else if (hi < 0xe000) |
|
|
659 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
660 | |
|
|
661 | if (hi >= 0x80) |
|
|
662 | { |
|
|
663 | utf8 = 1; |
|
|
664 | |
|
|
665 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
666 | } |
|
|
667 | else |
|
|
668 | *cur++ = hi; |
546 | } |
669 | } |
547 | else if (lo >= 0xdc00 && lo < 0xe000) |
|
|
548 | ERR ("illegal surrogate character"); |
|
|
549 | |
|
|
550 | if (hi >= 0x80) |
|
|
551 | { |
670 | break; |
552 | utf8 = 1; |
|
|
553 | |
671 | |
554 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
555 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
556 | } |
|
|
557 | else |
672 | default: |
558 | APPEND_CH (hi); |
673 | --dec->cur; |
|
|
674 | ERR ("illegal backslash escape sequence in string"); |
559 | } |
675 | } |
|
|
676 | } |
|
|
677 | else if (ch >= 0x20 && ch <= 0x7f) |
|
|
678 | *cur++ = ch; |
|
|
679 | else if (ch >= 0x80) |
|
|
680 | { |
|
|
681 | STRLEN clen; |
|
|
682 | UV uch; |
|
|
683 | |
|
|
684 | --dec->cur; |
|
|
685 | |
|
|
686 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
|
|
687 | if (clen == (STRLEN)-1) |
|
|
688 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
689 | |
|
|
690 | do |
|
|
691 | *cur++ = *dec->cur++; |
|
|
692 | while (--clen); |
|
|
693 | |
|
|
694 | utf8 = 1; |
|
|
695 | } |
|
|
696 | else |
|
|
697 | { |
|
|
698 | --dec->cur; |
|
|
699 | |
|
|
700 | if (!ch) |
|
|
701 | ERR ("unexpected end of string while parsing JSON string"); |
560 | break; |
702 | else |
|
|
703 | ERR ("invalid character encountered while parsing JSON string"); |
561 | } |
704 | } |
562 | } |
705 | } |
563 | else if (ch >= 0x20 && ch <= 0x7f) |
706 | while (cur < buf + SHORT_STRING_LEN); |
564 | APPEND_CH (*dec->cur++); |
707 | |
565 | else if (ch >= 0x80) |
708 | { |
|
|
709 | STRLEN len = cur - buf; |
|
|
710 | |
|
|
711 | if (sv) |
566 | { |
712 | { |
567 | STRLEN clen; |
713 | SvGROW (sv, SvCUR (sv) + len + 1); |
568 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
714 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
569 | if (clen < 0) |
715 | SvCUR_set (sv, SvCUR (sv) + len); |
570 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
|
|
571 | |
|
|
572 | APPEND_GROW (clen); |
|
|
573 | memcpy (cur, dec->cur, clen); |
|
|
574 | cur += clen; |
|
|
575 | dec->cur += clen; |
|
|
576 | } |
716 | } |
577 | else |
717 | else |
578 | ERR ("invalid character encountered"); |
718 | sv = newSVpvn (buf, len); |
579 | } |
719 | } |
|
|
720 | } |
|
|
721 | while (*dec->cur != '"'); |
580 | |
722 | |
581 | ++dec->cur; |
723 | ++dec->cur; |
582 | |
724 | |
583 | SvCUR_set (sv, cur - SvPVX (sv)); |
725 | if (sv) |
584 | |
726 | { |
585 | SvPOK_only (sv); |
727 | SvPOK_only (sv); |
586 | *SvEND (sv) = 0; |
728 | *SvEND (sv) = 0; |
587 | |
729 | |
588 | if (utf8) |
730 | if (utf8) |
589 | SvUTF8_on (sv); |
731 | SvUTF8_on (sv); |
|
|
732 | } |
|
|
733 | else |
|
|
734 | sv = newSVpvn ("", 0); |
590 | |
735 | |
591 | return sv; |
736 | return sv; |
592 | |
737 | |
593 | fail: |
738 | fail: |
594 | SvREFCNT_dec (sv); |
|
|
595 | return 0; |
739 | return 0; |
596 | } |
740 | } |
597 | |
741 | |
598 | static SV * |
742 | static SV * |
599 | decode_num (dec_t *dec) |
743 | decode_num (dec_t *dec) |
… | |
… | |
609 | { |
753 | { |
610 | ++dec->cur; |
754 | ++dec->cur; |
611 | if (*dec->cur >= '0' && *dec->cur <= '9') |
755 | if (*dec->cur >= '0' && *dec->cur <= '9') |
612 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
756 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
613 | } |
757 | } |
614 | |
758 | else if (*dec->cur < '0' || *dec->cur > '9') |
615 | // int |
759 | ERR ("malformed number (no digits after initial minus)"); |
|
|
760 | else |
|
|
761 | do |
|
|
762 | { |
|
|
763 | ++dec->cur; |
|
|
764 | } |
616 | while (*dec->cur >= '0' && *dec->cur <= '9') |
765 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
617 | ++dec->cur; |
|
|
618 | |
766 | |
619 | // [frac] |
767 | // [frac] |
620 | if (*dec->cur == '.') |
768 | if (*dec->cur == '.') |
621 | { |
769 | { |
622 | is_nv = 1; |
770 | ++dec->cur; |
|
|
771 | |
|
|
772 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
773 | ERR ("malformed number (no digits after decimal point)"); |
623 | |
774 | |
624 | do |
775 | do |
625 | { |
776 | { |
626 | ++dec->cur; |
777 | ++dec->cur; |
627 | } |
778 | } |
628 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
779 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
|
|
780 | |
|
|
781 | is_nv = 1; |
629 | } |
782 | } |
630 | |
783 | |
631 | // [exp] |
784 | // [exp] |
632 | if (*dec->cur == 'e' || *dec->cur == 'E') |
785 | if (*dec->cur == 'e' || *dec->cur == 'E') |
633 | { |
786 | { |
634 | is_nv = 1; |
|
|
635 | |
|
|
636 | ++dec->cur; |
787 | ++dec->cur; |
|
|
788 | |
637 | if (*dec->cur == '-' || *dec->cur == '+') |
789 | if (*dec->cur == '-' || *dec->cur == '+') |
638 | ++dec->cur; |
790 | ++dec->cur; |
639 | |
791 | |
|
|
792 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
793 | ERR ("malformed number (no digits after exp sign)"); |
|
|
794 | |
|
|
795 | do |
|
|
796 | { |
|
|
797 | ++dec->cur; |
|
|
798 | } |
640 | while (*dec->cur >= '0' && *dec->cur <= '9') |
799 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
641 | ++dec->cur; |
800 | |
|
|
801 | is_nv = 1; |
642 | } |
802 | } |
643 | |
803 | |
644 | if (!is_nv) |
804 | if (!is_nv) |
645 | { |
805 | { |
646 | UV uv; |
806 | UV uv; |
… | |
… | |
664 | static SV * |
824 | static SV * |
665 | decode_av (dec_t *dec) |
825 | decode_av (dec_t *dec) |
666 | { |
826 | { |
667 | AV *av = newAV (); |
827 | AV *av = newAV (); |
668 | |
828 | |
|
|
829 | DEC_INC_DEPTH; |
|
|
830 | decode_ws (dec); |
|
|
831 | |
|
|
832 | if (*dec->cur == ']') |
|
|
833 | ++dec->cur; |
|
|
834 | else |
669 | for (;;) |
835 | for (;;) |
670 | { |
836 | { |
671 | SV *value; |
837 | SV *value; |
672 | |
838 | |
673 | value = decode_sv (dec); |
839 | value = decode_sv (dec); |
674 | if (!value) |
840 | if (!value) |
675 | goto fail; |
841 | goto fail; |
676 | |
842 | |
677 | av_push (av, value); |
843 | av_push (av, value); |
678 | |
844 | |
679 | WS; |
845 | decode_ws (dec); |
680 | |
846 | |
681 | if (*dec->cur == ']') |
847 | if (*dec->cur == ']') |
682 | { |
848 | { |
683 | ++dec->cur; |
849 | ++dec->cur; |
684 | break; |
850 | break; |
|
|
851 | } |
685 | } |
852 | |
686 | |
|
|
687 | if (*dec->cur != ',') |
853 | if (*dec->cur != ',') |
688 | ERR (", or ] expected while parsing array"); |
854 | ERR (", or ] expected while parsing array"); |
689 | |
855 | |
690 | ++dec->cur; |
856 | ++dec->cur; |
691 | } |
857 | } |
692 | |
858 | |
|
|
859 | DEC_DEC_DEPTH; |
693 | return newRV_noinc ((SV *)av); |
860 | return newRV_noinc ((SV *)av); |
694 | |
861 | |
695 | fail: |
862 | fail: |
696 | SvREFCNT_dec (av); |
863 | SvREFCNT_dec (av); |
|
|
864 | DEC_DEC_DEPTH; |
697 | return 0; |
865 | return 0; |
698 | } |
866 | } |
699 | |
867 | |
700 | static SV * |
868 | static SV * |
701 | decode_hv (dec_t *dec) |
869 | decode_hv (dec_t *dec) |
702 | { |
870 | { |
703 | HV *hv = newHV (); |
871 | HV *hv = newHV (); |
704 | |
872 | |
|
|
873 | DEC_INC_DEPTH; |
|
|
874 | decode_ws (dec); |
|
|
875 | |
|
|
876 | if (*dec->cur == '}') |
|
|
877 | ++dec->cur; |
|
|
878 | else |
705 | for (;;) |
879 | for (;;) |
706 | { |
880 | { |
707 | SV *key, *value; |
881 | SV *key, *value; |
708 | |
882 | |
709 | WS; EXPECT_CH ('"'); |
883 | decode_ws (dec); EXPECT_CH ('"'); |
710 | |
884 | |
711 | key = decode_str (dec); |
885 | key = decode_str (dec); |
712 | if (!key) |
886 | if (!key) |
713 | goto fail; |
|
|
714 | |
|
|
715 | WS; EXPECT_CH (':'); |
|
|
716 | |
|
|
717 | value = decode_sv (dec); |
|
|
718 | if (!value) |
|
|
719 | { |
|
|
720 | SvREFCNT_dec (key); |
|
|
721 | goto fail; |
887 | goto fail; |
|
|
888 | |
|
|
889 | decode_ws (dec); EXPECT_CH (':'); |
|
|
890 | |
|
|
891 | value = decode_sv (dec); |
|
|
892 | if (!value) |
|
|
893 | { |
|
|
894 | SvREFCNT_dec (key); |
|
|
895 | goto fail; |
722 | } |
896 | } |
723 | |
897 | |
724 | //TODO: optimise |
|
|
725 | hv_store_ent (hv, key, value, 0); |
898 | hv_store_ent (hv, key, value, 0); |
|
|
899 | SvREFCNT_dec (key); |
726 | |
900 | |
727 | WS; |
901 | decode_ws (dec); |
728 | |
902 | |
729 | if (*dec->cur == '}') |
903 | if (*dec->cur == '}') |
730 | { |
904 | { |
731 | ++dec->cur; |
905 | ++dec->cur; |
732 | break; |
906 | break; |
733 | } |
907 | } |
734 | |
908 | |
735 | if (*dec->cur != ',') |
909 | if (*dec->cur != ',') |
736 | ERR (", or } expected while parsing object/hash"); |
910 | ERR (", or } expected while parsing object/hash"); |
737 | |
911 | |
738 | ++dec->cur; |
912 | ++dec->cur; |
739 | } |
913 | } |
740 | |
914 | |
|
|
915 | DEC_DEC_DEPTH; |
741 | return newRV_noinc ((SV *)hv); |
916 | return newRV_noinc ((SV *)hv); |
742 | |
917 | |
743 | fail: |
918 | fail: |
744 | SvREFCNT_dec (hv); |
919 | SvREFCNT_dec (hv); |
|
|
920 | DEC_DEC_DEPTH; |
745 | return 0; |
921 | return 0; |
746 | } |
922 | } |
747 | |
923 | |
748 | static SV * |
924 | static SV * |
749 | decode_sv (dec_t *dec) |
925 | decode_sv (dec_t *dec) |
750 | { |
926 | { |
751 | WS; |
927 | decode_ws (dec); |
752 | switch (*dec->cur) |
928 | switch (*dec->cur) |
753 | { |
929 | { |
754 | case '"': ++dec->cur; return decode_str (dec); |
930 | case '"': ++dec->cur; return decode_str (dec); |
755 | case '[': ++dec->cur; return decode_av (dec); |
931 | case '[': ++dec->cur; return decode_av (dec); |
756 | case '{': ++dec->cur; return decode_hv (dec); |
932 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
784 | |
960 | |
785 | case 'n': |
961 | case 'n': |
786 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
962 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
787 | { |
963 | { |
788 | dec->cur += 4; |
964 | dec->cur += 4; |
789 | return newSViv (1); |
965 | return newSVsv (&PL_sv_undef); |
790 | } |
966 | } |
791 | else |
967 | else |
792 | ERR ("'null' expected"); |
968 | ERR ("'null' expected"); |
793 | |
969 | |
794 | break; |
970 | break; |
795 | |
971 | |
796 | default: |
972 | default: |
797 | ERR ("malformed json string"); |
973 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
798 | break; |
974 | break; |
799 | } |
975 | } |
800 | |
976 | |
801 | fail: |
977 | fail: |
802 | return 0; |
978 | return 0; |
803 | } |
979 | } |
804 | |
980 | |
805 | static SV * |
981 | static SV * |
806 | decode_json (SV *string, UV flags) |
982 | decode_json (SV *string, U32 flags) |
807 | { |
983 | { |
|
|
984 | dec_t dec; |
808 | SV *sv; |
985 | SV *sv; |
809 | |
986 | |
|
|
987 | SvUPGRADE (string, SVt_PV); |
|
|
988 | |
810 | if (!(flags & F_UTF8)) |
989 | if (flags & F_UTF8) |
|
|
990 | sv_utf8_downgrade (string, 0); |
|
|
991 | else |
811 | sv_utf8_upgrade (string); |
992 | sv_utf8_upgrade (string); |
812 | |
993 | |
813 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
994 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
814 | |
995 | |
815 | dec_t dec; |
|
|
816 | dec.flags = flags; |
996 | dec.flags = flags; |
817 | dec.cur = SvPVX (string); |
997 | dec.cur = SvPVX (string); |
818 | dec.end = SvEND (string); |
998 | dec.end = SvEND (string); |
819 | dec.err = 0; |
999 | dec.err = 0; |
|
|
1000 | dec.depth = 0; |
|
|
1001 | dec.maxdepth = DEC_DEPTH (dec.flags); |
820 | |
1002 | |
821 | *dec.end = 1; // invalid anywhere |
1003 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
822 | sv = decode_sv (&dec); |
1004 | sv = decode_sv (&dec); |
823 | *dec.end = 0; |
|
|
824 | |
1005 | |
825 | if (!sv) |
1006 | if (!sv) |
826 | { |
1007 | { |
|
|
1008 | IV offset = dec.flags & F_UTF8 |
|
|
1009 | ? dec.cur - SvPVX (string) |
827 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
1010 | : utf8_distance (dec.cur, SvPVX (string)); |
828 | SV *uni = sv_newmortal (); |
1011 | SV *uni = sv_newmortal (); |
829 | |
1012 | |
|
|
1013 | // horrible hack to silence warning inside pv_uni_display |
|
|
1014 | COP cop = *PL_curcop; |
|
|
1015 | cop.cop_warnings = pWARN_NONE; |
|
|
1016 | ENTER; |
|
|
1017 | SAVEVPTR (PL_curcop); |
|
|
1018 | PL_curcop = &cop; |
830 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1019 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
1020 | LEAVE; |
|
|
1021 | |
831 | croak ("%s, at character %d (%s)", |
1022 | croak ("%s, at character offset %d [\"%s\"]", |
832 | dec.err, |
1023 | dec.err, |
833 | (int)offset, |
1024 | (int)offset, |
834 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1025 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
835 | } |
1026 | } |
836 | |
1027 | |
837 | sv = sv_2mortal (sv); |
1028 | sv = sv_2mortal (sv); |
838 | |
1029 | |
839 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1030 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
840 | croak ("JSON object or array expected (but number, string, true, false or null found, use allow_nonref to allow this)"); |
1031 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
841 | |
1032 | |
842 | return sv; |
1033 | return sv; |
843 | } |
1034 | } |
844 | |
1035 | |
|
|
1036 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1037 | // XS interface functions |
|
|
1038 | |
845 | MODULE = JSON::XS PACKAGE = JSON::XS |
1039 | MODULE = JSON::XS PACKAGE = JSON::XS |
846 | |
1040 | |
847 | BOOT: |
1041 | BOOT: |
848 | { |
1042 | { |
849 | int i; |
1043 | int i; |
850 | |
1044 | |
851 | memset (decode_hexdigit, 0xff, 256); |
1045 | memset (decode_hexdigit, 0xff, 256); |
|
|
1046 | |
852 | for (i = 10; i--; ) |
1047 | for (i = 0; i < 256; ++i) |
853 | decode_hexdigit ['0' + i] = i; |
1048 | decode_hexdigit [i] = |
854 | |
1049 | i >= '0' && i <= '9' ? i - '0' |
855 | for (i = 7; i--; ) |
1050 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
856 | { |
1051 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
857 | decode_hexdigit ['a' + i] = 10 + i; |
1052 | : -1; |
858 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
859 | } |
|
|
860 | |
1053 | |
861 | json_stash = gv_stashpv ("JSON::XS", 1); |
1054 | json_stash = gv_stashpv ("JSON::XS", 1); |
862 | } |
1055 | } |
863 | |
1056 | |
864 | PROTOTYPES: DISABLE |
1057 | PROTOTYPES: DISABLE |
… | |
… | |
867 | CODE: |
1060 | CODE: |
868 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1061 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
869 | OUTPUT: |
1062 | OUTPUT: |
870 | RETVAL |
1063 | RETVAL |
871 | |
1064 | |
872 | SV *ascii (SV *self, int enable) |
1065 | SV *ascii (SV *self, int enable = 1) |
873 | ALIAS: |
1066 | ALIAS: |
874 | ascii = F_ASCII |
1067 | ascii = F_ASCII |
875 | utf8 = F_UTF8 |
1068 | utf8 = F_UTF8 |
876 | indent = F_INDENT |
1069 | indent = F_INDENT |
877 | canonical = F_CANONICAL |
1070 | canonical = F_CANONICAL |
878 | space_before = F_SPACE_BEFORE |
1071 | space_before = F_SPACE_BEFORE |
879 | space_after = F_SPACE_AFTER |
1072 | space_after = F_SPACE_AFTER |
880 | json_rpc = F_JSON_RPC |
|
|
881 | pretty = F_PRETTY |
1073 | pretty = F_PRETTY |
882 | allow_nonref = F_ALLOW_NONREF |
1074 | allow_nonref = F_ALLOW_NONREF |
|
|
1075 | shrink = F_SHRINK |
883 | CODE: |
1076 | CODE: |
884 | { |
1077 | { |
885 | UV *uv = SvJSON (self); |
1078 | UV *uv = SvJSON (self); |
886 | if (enable) |
1079 | if (enable) |
887 | *uv |= ix; |
1080 | *uv |= ix; |
… | |
… | |
891 | RETVAL = newSVsv (self); |
1084 | RETVAL = newSVsv (self); |
892 | } |
1085 | } |
893 | OUTPUT: |
1086 | OUTPUT: |
894 | RETVAL |
1087 | RETVAL |
895 | |
1088 | |
|
|
1089 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1090 | CODE: |
|
|
1091 | { |
|
|
1092 | UV *uv = SvJSON (self); |
|
|
1093 | UV log2 = 0; |
|
|
1094 | |
|
|
1095 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1096 | |
|
|
1097 | while ((1UL << log2) < max_depth) |
|
|
1098 | ++log2; |
|
|
1099 | |
|
|
1100 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1101 | |
|
|
1102 | RETVAL = newSVsv (self); |
|
|
1103 | } |
|
|
1104 | OUTPUT: |
|
|
1105 | RETVAL |
|
|
1106 | |
896 | void encode (SV *self, SV *scalar) |
1107 | void encode (SV *self, SV *scalar) |
897 | PPCODE: |
1108 | PPCODE: |
898 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1109 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
899 | |
1110 | |
900 | void decode (SV *self, SV *jsonstr) |
1111 | void decode (SV *self, SV *jsonstr) |
… | |
… | |
902 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1113 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
903 | |
1114 | |
904 | PROTOTYPES: ENABLE |
1115 | PROTOTYPES: ENABLE |
905 | |
1116 | |
906 | void to_json (SV *scalar) |
1117 | void to_json (SV *scalar) |
|
|
1118 | ALIAS: |
|
|
1119 | objToJson = 0 |
907 | PPCODE: |
1120 | PPCODE: |
908 | XPUSHs (encode_json (scalar, F_UTF8)); |
1121 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
909 | |
1122 | |
910 | void from_json (SV *jsonstr) |
1123 | void from_json (SV *jsonstr) |
|
|
1124 | ALIAS: |
|
|
1125 | jsonToObj = 0 |
911 | PPCODE: |
1126 | PPCODE: |
912 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1127 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
913 | |
1128 | |