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