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