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