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