ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.115 by root, Fri Dec 21 05:16:19 2012 UTC vs.
Revision 1.118 by root, Fri Oct 25 20:27:57 2013 UTC

483 483
484 if (enc->indent >= enc->json.max_depth) 484 if (enc->indent >= enc->json.max_depth)
485 croak (ERR_NESTING_EXCEEDED); 485 croak (ERR_NESTING_EXCEEDED);
486 486
487 encode_ch (enc, '['); 487 encode_ch (enc, '[');
488 488
489 if (len >= 0) 489 if (len >= 0)
490 { 490 {
491 encode_nl (enc); ++enc->indent; 491 encode_nl (enc); ++enc->indent;
492 492
493 for (i = 0; i <= len; ++i) 493 for (i = 0; i <= len; ++i)
505 encode_comma (enc); 505 encode_comma (enc);
506 } 506 }
507 507
508 encode_nl (enc); --enc->indent; encode_indent (enc); 508 encode_nl (enc); --enc->indent; encode_indent (enc);
509 } 509 }
510 510
511 encode_ch (enc, ']'); 511 encode_ch (enc, ']');
512} 512}
513 513
514static void 514static void
515encode_hk (enc_t *enc, HE *he) 515encode_hk (enc_t *enc, HE *he)
519 if (HeKLEN (he) == HEf_SVKEY) 519 if (HeKLEN (he) == HEf_SVKEY)
520 { 520 {
521 SV *sv = HeSVKEY (he); 521 SV *sv = HeSVKEY (he);
522 STRLEN len; 522 STRLEN len;
523 char *str; 523 char *str;
524 524
525 SvGETMAGIC (sv); 525 SvGETMAGIC (sv);
526 str = SvPV (sv, len); 526 str = SvPV (sv, len);
527 527
528 encode_str (enc, str, len, SvUTF8 (sv)); 528 encode_str (enc, str, len, SvUTF8 (sv));
529 } 529 }
597 if (count) 597 if (count)
598 { 598 {
599 int i, fast = 1; 599 int i, fast = 1;
600 HE *hes_stack [STACK_HES]; 600 HE *hes_stack [STACK_HES];
601 HE **hes = hes_stack; 601 HE **hes = hes_stack;
602 602
603 // allocate larger arrays on the heap 603 // allocate larger arrays on the heap
604 if (count > STACK_HES) 604 if (count > STACK_HES)
605 { 605 {
606 SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes))); 606 SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes)));
607 hes = (HE **)SvPVX (sv); 607 hes = (HE **)SvPVX (sv);
843 else if (SvROK (sv)) 843 else if (SvROK (sv))
844 encode_rv (enc, SvRV (sv)); 844 encode_rv (enc, SvRV (sv));
845 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) 845 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN)
846 encode_str (enc, "null", 4, 0); 846 encode_str (enc, "null", 4, 0);
847 else 847 else
848 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 848 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data",
849 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); 849 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
850} 850}
851 851
852static SV * 852static SV *
853encode_json (SV *scalar, JSON *json) 853encode_json (SV *scalar, JSON *json)
1266 if (*dec->cur == ']') 1266 if (*dec->cur == ']')
1267 { 1267 {
1268 ++dec->cur; 1268 ++dec->cur;
1269 break; 1269 break;
1270 } 1270 }
1271 1271
1272 if (*dec->cur != ',') 1272 if (*dec->cur != ',')
1273 ERR (", or ] expected while parsing array"); 1273 ERR (", or ] expected while parsing array");
1274 1274
1275 ++dec->cur; 1275 ++dec->cur;
1276 1276
1464{ 1464{
1465 // the beauty of JSON: you need exactly one character lookahead 1465 // the beauty of JSON: you need exactly one character lookahead
1466 // to parse everything. 1466 // to parse everything.
1467 switch (*dec->cur) 1467 switch (*dec->cur)
1468 { 1468 {
1469 case '"': ++dec->cur; return decode_str (dec); 1469 case '"': ++dec->cur; return decode_str (dec);
1470 case '[': ++dec->cur; return decode_av (dec); 1470 case '[': ++dec->cur; return decode_av (dec);
1471 case '{': ++dec->cur; return decode_hv (dec); 1471 case '{': ++dec->cur; return decode_hv (dec);
1472 1472
1473 case '-': 1473 case '-':
1474 case '0': case '1': case '2': case '3': case '4': 1474 case '0': case '1': case '2': case '3': case '4':
1475 case '5': case '6': case '7': case '8': case '9': 1475 case '5': case '6': case '7': case '8': case '9':
1528{ 1528{
1529 dec_t dec; 1529 dec_t dec;
1530 SV *sv; 1530 SV *sv;
1531 1531
1532 /* work around bugs in 5.10 where manipulating magic values 1532 /* work around bugs in 5.10 where manipulating magic values
1533 * will perl ignore the magic in subsequent accesses. 1533 * makes perl ignore the magic in subsequent accesses.
1534 * also make a copy of non-PV values, to get them into a clean 1534 * also make a copy of non-PV values, to get them into a clean
1535 * state (SvPV should do that, but it's buggy, see below). 1535 * state (SvPV should do that, but it's buggy, see below).
1536 */ 1536 */
1537 /*SvGETMAGIC (string);*/ 1537 /*SvGETMAGIC (string);*/
1538 if (SvMAGICAL (string) || !SvPOK (string)) 1538 if (SvMAGICAL (string) || !SvPOK (string))
2071 SvREFCNT_dec (self->incr_text); 2071 SvREFCNT_dec (self->incr_text);
2072 2072
2073PROTOTYPES: ENABLE 2073PROTOTYPES: ENABLE
2074 2074
2075void encode_json (SV *scalar) 2075void encode_json (SV *scalar)
2076 ALIAS:
2077 to_json_ = 0
2078 encode_json = F_UTF8
2079 PPCODE: 2076 PPCODE:
2080{ 2077{
2081 JSON json; 2078 JSON json;
2082 json_init (&json); 2079 json_init (&json);
2083 json.flags |= ix; 2080 json.flags |= F_UTF8;
2084 PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; 2081 PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN;
2085 XPUSHs (scalar); 2082 XPUSHs (scalar);
2086} 2083}
2087 2084
2088void decode_json (SV *jsonstr) 2085void decode_json (SV *jsonstr)
2089 ALIAS:
2090 from_json_ = 0
2091 decode_json = F_UTF8
2092 PPCODE: 2086 PPCODE:
2093{ 2087{
2094 JSON json; 2088 JSON json;
2095 json_init (&json); 2089 json_init (&json);
2096 json.flags |= ix; 2090 json.flags |= F_UTF8;
2097 PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; 2091 PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN;
2098 XPUSHs (jsonstr); 2092 XPUSHs (jsonstr);
2099} 2093}
2100 2094

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines