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