… | |
… | |
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 == (STRLEN)-1) |
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 | 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 | } |
225 | } |
179 | } |
226 | } |
180 | |
227 | |
181 | --len; |
228 | --len; |
182 | } |
229 | } |
183 | } |
230 | } |
184 | |
231 | |
185 | #define INDENT SB \ |
232 | static void |
|
|
233 | encode_indent (enc_t *enc) |
|
|
234 | { |
186 | if (enc->flags & F_INDENT) \ |
235 | if (enc->flags & F_INDENT) |
187 | { \ |
236 | { |
188 | int i_; \ |
237 | 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 | |
238 | |
195 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
239 | need (enc, spaces); |
196 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
240 | memset (enc->cur, ' ', spaces); |
197 | #define COMMA SB \ |
241 | enc->cur += spaces; |
|
|
242 | } |
|
|
243 | } |
|
|
244 | |
|
|
245 | static void |
|
|
246 | encode_space (enc_t *enc) |
|
|
247 | { |
|
|
248 | need (enc, 1); |
198 | encode_ch (enc, ','); \ |
249 | encode_ch (enc, ' '); |
|
|
250 | } |
|
|
251 | |
|
|
252 | static void |
|
|
253 | encode_nl (enc_t *enc) |
|
|
254 | { |
199 | if (enc->flags & F_INDENT) \ |
255 | if (enc->flags & F_INDENT) |
200 | 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); |
201 | else if (enc->flags & F_SPACE_AFTER) \ |
269 | else if (enc->flags & F_SPACE_AFTER) |
202 | SPACE; \ |
270 | encode_space (enc); |
203 | SE |
271 | } |
204 | |
272 | |
205 | static void encode_sv (enc_t *enc, SV *sv); |
273 | static void encode_sv (enc_t *enc, SV *sv); |
206 | |
274 | |
207 | static void |
275 | static void |
208 | encode_av (enc_t *enc, AV *av) |
276 | encode_av (enc_t *enc, AV *av) |
209 | { |
277 | { |
210 | int i, len = av_len (av); |
278 | int i, len = av_len (av); |
211 | |
279 | |
212 | 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); |
213 | ++enc->indent; |
284 | ++enc->indent; |
214 | |
285 | |
215 | for (i = 0; i <= len; ++i) |
286 | for (i = 0; i <= len; ++i) |
216 | { |
287 | { |
217 | INDENT; |
288 | encode_indent (enc); |
218 | encode_sv (enc, *av_fetch (av, i, 0)); |
289 | encode_sv (enc, *av_fetch (av, i, 0)); |
219 | |
290 | |
220 | if (i < len) |
291 | if (i < len) |
221 | COMMA; |
292 | encode_comma (enc); |
222 | } |
293 | } |
223 | |
294 | |
224 | NL; |
295 | encode_nl (enc); |
225 | |
296 | |
226 | --enc->indent; |
297 | --enc->indent; |
227 | INDENT; encode_ch (enc, ']'); |
298 | encode_indent (enc); encode_ch (enc, ']'); |
228 | } |
299 | } |
229 | |
300 | |
230 | static void |
301 | static void |
231 | encode_he (enc_t *enc, HE *he) |
302 | encode_he (enc_t *enc, HE *he) |
232 | { |
303 | { |
… | |
… | |
246 | else |
317 | else |
247 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
318 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
248 | |
319 | |
249 | encode_ch (enc, '"'); |
320 | encode_ch (enc, '"'); |
250 | |
321 | |
251 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
322 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
252 | encode_ch (enc, ':'); |
323 | encode_ch (enc, ':'); |
253 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
324 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
254 | encode_sv (enc, HeVAL (he)); |
325 | encode_sv (enc, HeVAL (he)); |
255 | } |
326 | } |
256 | |
327 | |
257 | // compare hash entries, used when all keys are bytestrings |
328 | // compare hash entries, used when all keys are bytestrings |
258 | static int |
329 | static int |
… | |
… | |
264 | HE *b = *(HE **)b_; |
335 | HE *b = *(HE **)b_; |
265 | |
336 | |
266 | STRLEN la = HeKLEN (a); |
337 | STRLEN la = HeKLEN (a); |
267 | STRLEN lb = HeKLEN (b); |
338 | STRLEN lb = HeKLEN (b); |
268 | |
339 | |
269 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
340 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
270 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
341 | cmp = la - lb; |
271 | |
342 | |
272 | return cmp; |
343 | return cmp; |
273 | } |
344 | } |
274 | |
345 | |
275 | // 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 |
… | |
… | |
282 | static void |
353 | static void |
283 | encode_hv (enc_t *enc, HV *hv) |
354 | encode_hv (enc_t *enc, HV *hv) |
284 | { |
355 | { |
285 | int count, i; |
356 | int count, i; |
286 | |
357 | |
|
|
358 | if (enc->indent >= enc->maxdepth) |
|
|
359 | croak ("data structure too deep (hit recursion limit)"); |
|
|
360 | |
287 | encode_ch (enc, '{'); NL; ++enc->indent; |
361 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
288 | |
362 | |
289 | if ((count = hv_iterinit (hv))) |
363 | if ((count = hv_iterinit (hv))) |
290 | { |
364 | { |
291 | // for canonical output we have to sort by keys first |
365 | // for canonical output we have to sort by keys first |
292 | // actually, this is mostly due to the stupid so-called |
366 | // actually, this is mostly due to the stupid so-called |
293 | // security workaround added somewhere in 5.8.x. |
367 | // security workaround added somewhere in 5.8.x. |
294 | // that randomises hash orderings |
368 | // that randomises hash orderings |
295 | if (enc->flags & F_CANONICAL) |
369 | if (enc->flags & F_CANONICAL) |
296 | { |
370 | { |
297 | HE *he, *hes [count]; |
371 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
298 | int fast = 1; |
372 | int fast = 1; |
299 | |
373 | |
300 | i = 0; |
374 | i = 0; |
301 | while ((he = hv_iternext (hv))) |
375 | while ((he = hv_iternext (hv))) |
302 | { |
376 | { |
… | |
… | |
309 | |
383 | |
310 | if (fast) |
384 | if (fast) |
311 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
385 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
312 | else |
386 | else |
313 | { |
387 | { |
314 | // hack to disable "use bytes" |
388 | // hack to forcefully disable "use bytes" |
315 | COP *oldcop = PL_curcop, cop; |
389 | COP cop = *PL_curcop; |
316 | cop.op_private = 0; |
390 | cop.op_private = 0; |
|
|
391 | |
|
|
392 | ENTER; |
|
|
393 | SAVETMPS; |
|
|
394 | |
|
|
395 | SAVEVPTR (PL_curcop); |
317 | PL_curcop = &cop; |
396 | PL_curcop = &cop; |
318 | |
397 | |
319 | SAVETMPS; |
|
|
320 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
398 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
399 | |
321 | FREETMPS; |
400 | FREETMPS; |
322 | |
401 | LEAVE; |
323 | PL_curcop = oldcop; |
|
|
324 | } |
402 | } |
325 | |
403 | |
326 | for (i = 0; i < count; ++i) |
404 | for (i = 0; i < count; ++i) |
327 | { |
405 | { |
328 | INDENT; |
406 | encode_indent (enc); |
329 | encode_he (enc, hes [i]); |
407 | encode_he (enc, hes [i]); |
330 | |
408 | |
331 | if (i < count - 1) |
409 | if (i < count - 1) |
332 | COMMA; |
410 | encode_comma (enc); |
333 | } |
411 | } |
334 | |
412 | |
335 | NL; |
413 | encode_nl (enc); |
336 | } |
414 | } |
337 | else |
415 | else |
338 | { |
416 | { |
339 | SV *sv; |
|
|
340 | HE *he = hv_iternext (hv); |
417 | HE *he = hv_iternext (hv); |
341 | |
418 | |
342 | for (;;) |
419 | for (;;) |
343 | { |
420 | { |
344 | INDENT; |
421 | encode_indent (enc); |
345 | encode_he (enc, he); |
422 | encode_he (enc, he); |
346 | |
423 | |
347 | if (!(he = hv_iternext (hv))) |
424 | if (!(he = hv_iternext (hv))) |
348 | break; |
425 | break; |
349 | |
426 | |
350 | COMMA; |
427 | encode_comma (enc); |
351 | } |
428 | } |
352 | |
429 | |
353 | NL; |
430 | encode_nl (enc); |
354 | } |
431 | } |
355 | } |
432 | } |
356 | |
433 | |
357 | --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)))); |
358 | } |
463 | } |
359 | |
464 | |
360 | static void |
465 | static void |
361 | encode_sv (enc_t *enc, SV *sv) |
466 | encode_sv (enc_t *enc, SV *sv) |
362 | { |
467 | { |
… | |
… | |
383 | SvIsUV(sv) |
488 | SvIsUV(sv) |
384 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
489 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
385 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
490 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
386 | } |
491 | } |
387 | else if (SvROK (sv)) |
492 | else if (SvROK (sv)) |
388 | { |
493 | 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)) |
494 | else if (!SvOK (sv)) |
404 | encode_str (enc, "null", 4, 0); |
495 | encode_str (enc, "null", 4, 0); |
405 | else |
496 | else |
406 | 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)); |
407 | } |
499 | } |
408 | |
500 | |
409 | static SV * |
501 | static SV * |
410 | encode_json (SV *scalar, UV flags) |
502 | encode_json (SV *scalar, U32 flags) |
411 | { |
503 | { |
|
|
504 | enc_t enc; |
|
|
505 | |
412 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
506 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
413 | 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)"); |
414 | |
508 | |
415 | enc_t enc; |
|
|
416 | enc.flags = flags; |
509 | enc.flags = flags; |
417 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
510 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
418 | enc.cur = SvPVX (enc.sv); |
511 | enc.cur = SvPVX (enc.sv); |
419 | enc.end = SvEND (enc.sv); |
512 | enc.end = SvEND (enc.sv); |
420 | enc.max_recurse = 0; |
|
|
421 | enc.indent = 0; |
513 | enc.indent = 0; |
|
|
514 | enc.maxdepth = DEC_DEPTH (flags); |
422 | |
515 | |
423 | SvPOK_only (enc.sv); |
516 | SvPOK_only (enc.sv); |
424 | encode_sv (&enc, scalar); |
517 | encode_sv (&enc, scalar); |
425 | |
518 | |
426 | if (!(flags & (F_ASCII | F_UTF8))) |
519 | if (!(flags & (F_ASCII | F_UTF8))) |
427 | SvUTF8_on (enc.sv); |
520 | SvUTF8_on (enc.sv); |
428 | |
521 | |
429 | 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 | |
430 | return enc.sv; |
528 | return enc.sv; |
431 | } |
529 | } |
432 | |
530 | |
433 | ///////////////////////////////////////////////////////////////////////////// |
531 | ///////////////////////////////////////////////////////////////////////////// |
|
|
532 | // decoder |
434 | |
533 | |
435 | #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 | { |
436 | for (;;) \ |
548 | for (;;) |
437 | { \ |
549 | { |
438 | char ch = *dec->cur; \ |
550 | char ch = *dec->cur; |
|
|
551 | |
439 | if (ch > 0x20 \ |
552 | if (ch > 0x20 |
440 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
553 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
441 | break; \ |
554 | break; |
|
|
555 | |
442 | ++dec->cur; \ |
556 | ++dec->cur; |
443 | } |
557 | } |
|
|
558 | } |
444 | |
559 | |
445 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
560 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
561 | |
446 | #define EXPECT_CH(ch) SB \ |
562 | #define EXPECT_CH(ch) SB \ |
447 | if (*dec->cur != ch) \ |
563 | if (*dec->cur != ch) \ |
448 | ERR (# ch " expected"); \ |
564 | ERR (# ch " expected"); \ |
449 | ++dec->cur; \ |
565 | ++dec->cur; \ |
450 | SE |
566 | SE |
451 | |
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 | |
452 | static SV *decode_sv (dec_t *dec); |
571 | static SV *decode_sv (dec_t *dec); |
453 | |
572 | |
454 | static signed char decode_hexdigit[256]; |
573 | static signed char decode_hexdigit[256]; |
455 | |
574 | |
456 | static UV |
575 | static UV |
457 | decode_4hex (dec_t *dec) |
576 | decode_4hex (dec_t *dec) |
458 | { |
577 | { |
459 | signed char d1, d2, d3, d4; |
578 | signed char d1, d2, d3, d4; |
|
|
579 | unsigned char *cur = (unsigned char *)dec->cur; |
460 | |
580 | |
461 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
462 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
581 | 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"); |
582 | 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"); |
583 | 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"); |
584 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
469 | |
585 | |
470 | dec->cur += 4; |
586 | dec->cur += 4; |
471 | |
587 | |
472 | return ((UV)d1) << 12 |
588 | return ((UV)d1) << 12 |
473 | | ((UV)d2) << 8 |
589 | | ((UV)d2) << 8 |
… | |
… | |
476 | |
592 | |
477 | fail: |
593 | fail: |
478 | return (UV)-1; |
594 | return (UV)-1; |
479 | } |
595 | } |
480 | |
596 | |
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 * |
597 | static SV * |
497 | decode_str (dec_t *dec) |
598 | decode_str (dec_t *dec) |
498 | { |
599 | { |
499 | SV *sv = NEWSV (0,2); |
600 | SV *sv = 0; |
500 | int utf8 = 0; |
601 | int utf8 = 0; |
501 | char *cur = SvPVX (sv); |
|
|
502 | char *end = SvEND (sv); |
|
|
503 | |
602 | |
504 | for (;;) |
603 | do |
505 | { |
604 | { |
506 | unsigned char ch = *(unsigned char *)dec->cur; |
605 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
|
|
606 | char *cur = buf; |
507 | |
607 | |
508 | if (ch == '"') |
608 | do |
509 | break; |
|
|
510 | else if (ch == '\\') |
|
|
511 | { |
609 | { |
512 | switch (*++dec->cur) |
610 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
611 | |
|
|
612 | if (ch == '"') |
513 | { |
613 | { |
514 | case '\\': |
614 | --dec->cur; |
515 | case '/': |
615 | break; |
516 | case '"': APPEND_CH (*dec->cur++); break; |
616 | } |
517 | |
617 | else if (ch == '\\') |
518 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
618 | { |
519 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
619 | 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 | { |
620 | { |
526 | UV lo, hi; |
621 | case '\\': |
527 | ++dec->cur; |
622 | case '/': |
|
|
623 | case '"': *cur++ = *dec->cur++; break; |
528 | |
624 | |
529 | hi = decode_4hex (dec); |
625 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
530 | if (hi == (UV)-1) |
626 | case 't': ++dec->cur; *cur++ = '\011'; break; |
531 | 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; |
532 | |
630 | |
533 | // possibly a surrogate pair |
631 | case 'u': |
534 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
535 | { |
632 | { |
536 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
633 | UV lo, hi; |
537 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
538 | |
|
|
539 | dec->cur += 2; |
634 | ++dec->cur; |
540 | |
635 | |
541 | lo = decode_4hex (dec); |
636 | hi = decode_4hex (dec); |
542 | if (lo == (UV)-1) |
637 | if (hi == (UV)-1) |
543 | goto fail; |
638 | goto fail; |
544 | |
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 | |
545 | if (lo < 0xdc00 || lo >= 0xe000) |
653 | if (lo < 0xdc00 || lo >= 0xe000) |
546 | ERR ("surrogate pair expected"); |
654 | ERR ("surrogate pair expected"); |
547 | |
655 | |
548 | 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; |
549 | } |
669 | } |
550 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
551 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
552 | |
|
|
553 | if (hi >= 0x80) |
|
|
554 | { |
670 | break; |
555 | utf8 = 1; |
|
|
556 | |
671 | |
557 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
558 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
559 | } |
|
|
560 | else |
672 | default: |
561 | APPEND_CH (hi); |
673 | --dec->cur; |
|
|
674 | ERR ("illegal backslash escape sequence in string"); |
562 | } |
675 | } |
563 | break; |
676 | } |
|
|
677 | else if (ch >= 0x20 && ch <= 0x7f) |
|
|
678 | *cur++ = ch; |
|
|
679 | else if (ch >= 0x80) |
|
|
680 | { |
|
|
681 | STRLEN clen; |
|
|
682 | UV uch; |
564 | |
683 | |
565 | default: |
|
|
566 | --dec->cur; |
684 | --dec->cur; |
567 | ERR ("illegal backslash escape sequence in string"); |
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"); |
|
|
702 | else |
|
|
703 | ERR ("invalid character encountered while parsing JSON string"); |
568 | } |
704 | } |
569 | } |
705 | } |
570 | else if (ch >= 0x20 && ch <= 0x7f) |
706 | while (cur < buf + SHORT_STRING_LEN); |
571 | APPEND_CH (*dec->cur++); |
707 | |
572 | else if (ch >= 0x80) |
|
|
573 | { |
708 | { |
574 | STRLEN clen; |
709 | 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 | |
710 | |
579 | APPEND_GROW (clen); |
711 | if (sv) |
580 | do |
|
|
581 | { |
712 | { |
582 | *cur++ = *dec->cur++; |
713 | SvGROW (sv, SvCUR (sv) + len + 1); |
|
|
714 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
|
|
715 | SvCUR_set (sv, SvCUR (sv) + len); |
583 | } |
716 | } |
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 |
717 | else |
591 | ERR ("invalid character encountered"); |
718 | sv = newSVpvn (buf, len); |
592 | } |
719 | } |
|
|
720 | } |
|
|
721 | while (*dec->cur != '"'); |
593 | |
722 | |
594 | ++dec->cur; |
723 | ++dec->cur; |
595 | |
724 | |
596 | SvCUR_set (sv, cur - SvPVX (sv)); |
725 | if (sv) |
597 | |
726 | { |
598 | SvPOK_only (sv); |
727 | SvPOK_only (sv); |
599 | *SvEND (sv) = 0; |
728 | *SvEND (sv) = 0; |
600 | |
729 | |
601 | if (utf8) |
730 | if (utf8) |
602 | SvUTF8_on (sv); |
731 | SvUTF8_on (sv); |
|
|
732 | } |
|
|
733 | else |
|
|
734 | sv = newSVpvn ("", 0); |
603 | |
735 | |
604 | return sv; |
736 | return sv; |
605 | |
737 | |
606 | fail: |
738 | fail: |
607 | SvREFCNT_dec (sv); |
|
|
608 | return 0; |
739 | return 0; |
609 | } |
740 | } |
610 | |
741 | |
611 | static SV * |
742 | static SV * |
612 | decode_num (dec_t *dec) |
743 | decode_num (dec_t *dec) |
… | |
… | |
693 | static SV * |
824 | static SV * |
694 | decode_av (dec_t *dec) |
825 | decode_av (dec_t *dec) |
695 | { |
826 | { |
696 | AV *av = newAV (); |
827 | AV *av = newAV (); |
697 | |
828 | |
698 | WS; |
829 | DEC_INC_DEPTH; |
|
|
830 | decode_ws (dec); |
|
|
831 | |
699 | if (*dec->cur == ']') |
832 | if (*dec->cur == ']') |
700 | ++dec->cur; |
833 | ++dec->cur; |
701 | else |
834 | else |
702 | for (;;) |
835 | for (;;) |
703 | { |
836 | { |
… | |
… | |
707 | if (!value) |
840 | if (!value) |
708 | goto fail; |
841 | goto fail; |
709 | |
842 | |
710 | av_push (av, value); |
843 | av_push (av, value); |
711 | |
844 | |
712 | WS; |
845 | decode_ws (dec); |
713 | |
846 | |
714 | if (*dec->cur == ']') |
847 | if (*dec->cur == ']') |
715 | { |
848 | { |
716 | ++dec->cur; |
849 | ++dec->cur; |
717 | break; |
850 | break; |
… | |
… | |
721 | ERR (", or ] expected while parsing array"); |
854 | ERR (", or ] expected while parsing array"); |
722 | |
855 | |
723 | ++dec->cur; |
856 | ++dec->cur; |
724 | } |
857 | } |
725 | |
858 | |
|
|
859 | DEC_DEC_DEPTH; |
726 | return newRV_noinc ((SV *)av); |
860 | return newRV_noinc ((SV *)av); |
727 | |
861 | |
728 | fail: |
862 | fail: |
729 | SvREFCNT_dec (av); |
863 | SvREFCNT_dec (av); |
|
|
864 | DEC_DEC_DEPTH; |
730 | return 0; |
865 | return 0; |
731 | } |
866 | } |
732 | |
867 | |
733 | static SV * |
868 | static SV * |
734 | decode_hv (dec_t *dec) |
869 | decode_hv (dec_t *dec) |
735 | { |
870 | { |
736 | HV *hv = newHV (); |
871 | HV *hv = newHV (); |
737 | |
872 | |
738 | WS; |
873 | DEC_INC_DEPTH; |
|
|
874 | decode_ws (dec); |
|
|
875 | |
739 | if (*dec->cur == '}') |
876 | if (*dec->cur == '}') |
740 | ++dec->cur; |
877 | ++dec->cur; |
741 | else |
878 | else |
742 | for (;;) |
879 | for (;;) |
743 | { |
880 | { |
744 | SV *key, *value; |
881 | SV *key, *value; |
745 | |
882 | |
746 | WS; EXPECT_CH ('"'); |
883 | decode_ws (dec); EXPECT_CH ('"'); |
747 | |
884 | |
748 | key = decode_str (dec); |
885 | key = decode_str (dec); |
749 | if (!key) |
886 | if (!key) |
750 | goto fail; |
887 | goto fail; |
751 | |
888 | |
752 | WS; EXPECT_CH (':'); |
889 | decode_ws (dec); EXPECT_CH (':'); |
753 | |
890 | |
754 | value = decode_sv (dec); |
891 | value = decode_sv (dec); |
755 | if (!value) |
892 | if (!value) |
756 | { |
893 | { |
757 | SvREFCNT_dec (key); |
894 | SvREFCNT_dec (key); |
758 | goto fail; |
895 | goto fail; |
759 | } |
896 | } |
760 | |
897 | |
761 | //TODO: optimise |
|
|
762 | hv_store_ent (hv, key, value, 0); |
898 | hv_store_ent (hv, key, value, 0); |
|
|
899 | SvREFCNT_dec (key); |
763 | |
900 | |
764 | WS; |
901 | decode_ws (dec); |
765 | |
902 | |
766 | if (*dec->cur == '}') |
903 | if (*dec->cur == '}') |
767 | { |
904 | { |
768 | ++dec->cur; |
905 | ++dec->cur; |
769 | break; |
906 | break; |
… | |
… | |
773 | ERR (", or } expected while parsing object/hash"); |
910 | ERR (", or } expected while parsing object/hash"); |
774 | |
911 | |
775 | ++dec->cur; |
912 | ++dec->cur; |
776 | } |
913 | } |
777 | |
914 | |
|
|
915 | DEC_DEC_DEPTH; |
778 | return newRV_noinc ((SV *)hv); |
916 | return newRV_noinc ((SV *)hv); |
779 | |
917 | |
780 | fail: |
918 | fail: |
781 | SvREFCNT_dec (hv); |
919 | SvREFCNT_dec (hv); |
|
|
920 | DEC_DEC_DEPTH; |
782 | return 0; |
921 | return 0; |
783 | } |
922 | } |
784 | |
923 | |
785 | static SV * |
924 | static SV * |
786 | decode_sv (dec_t *dec) |
925 | decode_sv (dec_t *dec) |
787 | { |
926 | { |
788 | WS; |
927 | decode_ws (dec); |
789 | switch (*dec->cur) |
928 | switch (*dec->cur) |
790 | { |
929 | { |
791 | case '"': ++dec->cur; return decode_str (dec); |
930 | case '"': ++dec->cur; return decode_str (dec); |
792 | case '[': ++dec->cur; return decode_av (dec); |
931 | case '[': ++dec->cur; return decode_av (dec); |
793 | case '{': ++dec->cur; return decode_hv (dec); |
932 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
829 | ERR ("'null' expected"); |
968 | ERR ("'null' expected"); |
830 | |
969 | |
831 | break; |
970 | break; |
832 | |
971 | |
833 | default: |
972 | default: |
834 | ERR ("malformed json string"); |
973 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
835 | break; |
974 | break; |
836 | } |
975 | } |
837 | |
976 | |
838 | fail: |
977 | fail: |
839 | return 0; |
978 | return 0; |
840 | } |
979 | } |
841 | |
980 | |
842 | static SV * |
981 | static SV * |
843 | decode_json (SV *string, UV flags) |
982 | decode_json (SV *string, U32 flags) |
844 | { |
983 | { |
|
|
984 | dec_t dec; |
845 | SV *sv; |
985 | SV *sv; |
|
|
986 | |
|
|
987 | SvGETMAGIC (string); |
|
|
988 | SvUPGRADE (string, SVt_PV); |
846 | |
989 | |
847 | if (flags & F_UTF8) |
990 | if (flags & F_UTF8) |
848 | sv_utf8_downgrade (string, 0); |
991 | sv_utf8_downgrade (string, 0); |
849 | else |
992 | else |
850 | sv_utf8_upgrade (string); |
993 | sv_utf8_upgrade (string); |
851 | |
994 | |
852 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
995 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
853 | |
996 | |
854 | dec_t dec; |
|
|
855 | dec.flags = flags; |
997 | dec.flags = flags; |
856 | dec.cur = SvPVX (string); |
998 | dec.cur = SvPVX (string); |
857 | dec.end = SvEND (string); |
999 | dec.end = SvEND (string); |
858 | dec.err = 0; |
1000 | dec.err = 0; |
|
|
1001 | dec.depth = 0; |
|
|
1002 | dec.maxdepth = DEC_DEPTH (dec.flags); |
859 | |
1003 | |
|
|
1004 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
860 | sv = decode_sv (&dec); |
1005 | sv = decode_sv (&dec); |
861 | |
1006 | |
862 | if (!sv) |
1007 | if (!sv) |
863 | { |
1008 | { |
|
|
1009 | IV offset = dec.flags & F_UTF8 |
|
|
1010 | ? dec.cur - SvPVX (string) |
864 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
1011 | : utf8_distance (dec.cur, SvPVX (string)); |
865 | SV *uni = sv_newmortal (); |
1012 | SV *uni = sv_newmortal (); |
|
|
1013 | |
866 | // horrible hack to silence warning inside pv_uni_display |
1014 | // horrible hack to silence warning inside pv_uni_display |
867 | COP cop; |
1015 | COP cop = *PL_curcop; |
868 | memset (&cop, 0, sizeof (cop)); |
|
|
869 | cop.cop_warnings = pWARN_NONE; |
1016 | cop.cop_warnings = pWARN_NONE; |
|
|
1017 | ENTER; |
870 | SAVEVPTR (PL_curcop); |
1018 | SAVEVPTR (PL_curcop); |
871 | PL_curcop = &cop; |
1019 | PL_curcop = &cop; |
872 | |
|
|
873 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1020 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
1021 | LEAVE; |
|
|
1022 | |
874 | croak ("%s, at character offset %d (%s)", |
1023 | croak ("%s, at character offset %d [\"%s\"]", |
875 | dec.err, |
1024 | dec.err, |
876 | (int)offset, |
1025 | (int)offset, |
877 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1026 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
878 | } |
1027 | } |
879 | |
1028 | |
880 | sv = sv_2mortal (sv); |
1029 | sv = sv_2mortal (sv); |
881 | |
1030 | |
882 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1031 | 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)"); |
1032 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
884 | |
1033 | |
885 | return sv; |
1034 | return sv; |
886 | } |
1035 | } |
887 | |
1036 | |
|
|
1037 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1038 | // XS interface functions |
|
|
1039 | |
888 | MODULE = JSON::XS PACKAGE = JSON::XS |
1040 | MODULE = JSON::XS PACKAGE = JSON::XS |
889 | |
1041 | |
890 | BOOT: |
1042 | BOOT: |
891 | { |
1043 | { |
892 | int i; |
1044 | int i; |
893 | |
1045 | |
894 | memset (decode_hexdigit, 0xff, 256); |
1046 | memset (decode_hexdigit, 0xff, 256); |
|
|
1047 | |
895 | for (i = 10; i--; ) |
1048 | for (i = 0; i < 256; ++i) |
896 | decode_hexdigit ['0' + i] = i; |
1049 | decode_hexdigit [i] = |
897 | |
1050 | i >= '0' && i <= '9' ? i - '0' |
898 | for (i = 7; i--; ) |
1051 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
899 | { |
1052 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
900 | decode_hexdigit ['a' + i] = 10 + i; |
1053 | : -1; |
901 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
902 | } |
|
|
903 | |
1054 | |
904 | json_stash = gv_stashpv ("JSON::XS", 1); |
1055 | json_stash = gv_stashpv ("JSON::XS", 1); |
905 | } |
1056 | } |
906 | |
1057 | |
907 | PROTOTYPES: DISABLE |
1058 | PROTOTYPES: DISABLE |
… | |
… | |
910 | CODE: |
1061 | CODE: |
911 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1062 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
912 | OUTPUT: |
1063 | OUTPUT: |
913 | RETVAL |
1064 | RETVAL |
914 | |
1065 | |
915 | SV *ascii (SV *self, int enable) |
1066 | SV *ascii (SV *self, int enable = 1) |
916 | ALIAS: |
1067 | ALIAS: |
917 | ascii = F_ASCII |
1068 | ascii = F_ASCII |
918 | utf8 = F_UTF8 |
1069 | utf8 = F_UTF8 |
919 | indent = F_INDENT |
1070 | indent = F_INDENT |
920 | canonical = F_CANONICAL |
1071 | canonical = F_CANONICAL |
921 | space_before = F_SPACE_BEFORE |
1072 | space_before = F_SPACE_BEFORE |
922 | space_after = F_SPACE_AFTER |
1073 | space_after = F_SPACE_AFTER |
923 | json_rpc = F_JSON_RPC |
|
|
924 | pretty = F_PRETTY |
1074 | pretty = F_PRETTY |
925 | allow_nonref = F_ALLOW_NONREF |
1075 | allow_nonref = F_ALLOW_NONREF |
|
|
1076 | shrink = F_SHRINK |
926 | CODE: |
1077 | CODE: |
927 | { |
1078 | { |
928 | UV *uv = SvJSON (self); |
1079 | UV *uv = SvJSON (self); |
929 | if (enable) |
1080 | if (enable) |
930 | *uv |= ix; |
1081 | *uv |= ix; |
… | |
… | |
934 | RETVAL = newSVsv (self); |
1085 | RETVAL = newSVsv (self); |
935 | } |
1086 | } |
936 | OUTPUT: |
1087 | OUTPUT: |
937 | RETVAL |
1088 | RETVAL |
938 | |
1089 | |
|
|
1090 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1091 | CODE: |
|
|
1092 | { |
|
|
1093 | UV *uv = SvJSON (self); |
|
|
1094 | UV log2 = 0; |
|
|
1095 | |
|
|
1096 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1097 | |
|
|
1098 | while ((1UL << log2) < max_depth) |
|
|
1099 | ++log2; |
|
|
1100 | |
|
|
1101 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1102 | |
|
|
1103 | RETVAL = newSVsv (self); |
|
|
1104 | } |
|
|
1105 | OUTPUT: |
|
|
1106 | RETVAL |
|
|
1107 | |
939 | void encode (SV *self, SV *scalar) |
1108 | void encode (SV *self, SV *scalar) |
940 | PPCODE: |
1109 | PPCODE: |
941 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1110 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
942 | |
1111 | |
943 | void decode (SV *self, SV *jsonstr) |
1112 | void decode (SV *self, SV *jsonstr) |
… | |
… | |
945 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1114 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
946 | |
1115 | |
947 | PROTOTYPES: ENABLE |
1116 | PROTOTYPES: ENABLE |
948 | |
1117 | |
949 | void to_json (SV *scalar) |
1118 | void to_json (SV *scalar) |
|
|
1119 | ALIAS: |
|
|
1120 | objToJson = 0 |
950 | PPCODE: |
1121 | PPCODE: |
951 | XPUSHs (encode_json (scalar, F_UTF8)); |
1122 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
952 | |
1123 | |
953 | void from_json (SV *jsonstr) |
1124 | void from_json (SV *jsonstr) |
|
|
1125 | ALIAS: |
|
|
1126 | jsonToObj = 0 |
954 | PPCODE: |
1127 | PPCODE: |
955 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1128 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
956 | |
1129 | |