Changeset 1725

Show
Ignore:
Timestamp:
01/10/06 21:30:49
Author:
miyagawa
Message:

merge Audrey's changes; Test fixed and document updated

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • JSON-Syck/trunk/Changes

    r1706 r1725  
    1 Revision history for JSON::Syck 
     1[Changes for 0.17 - 2006-01-11] 
     2 
     3* Use the correct style for dual vars and tied scalars. 
     4  Reported by: Tatsuhiko Miyagawa 
     5 
     6[Changes for 0.16 - 2006-01-11] 
    27 
    380.01 
  • JSON-Syck/trunk/lib/JSON/Syck.pm

    r1724 r1725  
    4747 
    4848JSON.pm comes with dozens of ways to do the same thing and lots of 
    49 options, while JSON::Syck doesn't. 
     49options, while JSON::Syck doesn't. There's only C<Load> and C<Dump> 
    5050 
    51 JSON::Syck doesn't use camelCase method names :-) 
     51Oh, and JSON::Syck doesn't use camelCase method names :-) 
    5252 
    53 =head1 BUG
     53=head1 AUTHOR
    5454 
    55 C<Load> function in JSON::Syck is actually the same with that of 
    56 YAML's. That means, when you give a valid YAML but non-valid JSON 
    57 data, it just accepts and is able to parse the string. 
    58  
    59 =head1 AUTHOR 
     55Audrey Tang E<lt>autrijus@autrijus.orgE<gt> 
    6056 
    6157Tatsuhiko Miyagawa E<lt>miyagawa@gmail.comE<gt> 
    6258 
    63 This module is forked from Audrey Tang's excellent YAML::Syck module 
    64 and 99% of the XS code is written by Audrey. 
     59This module is originally forked from Audrey Tang's excellent 
     60YAML::Syck module and 99.9% of the XS code is written by Audrey. 
    6561 
    6662The F<libsyck> code bundled with this module is written by I<why the 
  • JSON-Syck/trunk/perl_syck.h

    r1724 r1725  
    1717#  define MAP_NONE      map_inline 
    1818#  define TYPE_IS_NULL(x) ((x == NULL) || (strcmp( x, "str" ) == 0)) 
     19#  define OBJOF(a)        (a) 
    1920#else 
    2021#  define PACKAGE_NAME  "YAML::Syck" 
     
    2627#  define MAP_NONE      map_none 
    2728#  define TYPE_IS_NULL(x) (x == NULL) 
     29#  define OBJOF(a)        (*tag ? tag : a) 
    2830#endif 
    2931 
     
    221223} 
    222224 
     225void perl_json_postprocess(SV *sv) { 
     226    int i; 
     227    char ch; 
     228    bool in_string = 0; 
     229    bool in_quote  = 0; 
     230    char *pos; 
     231    char *s = SvPVX(sv); 
     232    STRLEN len = sv_len(sv); 
     233    STRLEN final_len = len; 
     234 
     235    pos = s; 
     236 
     237    for (i = 0; i < len; i++) { 
     238        ch = *(s+i); 
     239        *pos++ = ch; 
     240        if (in_quote) { 
     241            in_quote = !in_quote; 
     242        } 
     243        else if (ch == '\"') { 
     244            in_string = !in_string; 
     245        } 
     246        else if ((ch == ':' || ch == ',') && !in_string) { 
     247            i++; /* has to be a space afterwards */ 
     248            final_len--; 
     249        } 
     250    } 
     251    *pos = '\0'; 
     252 
     253    SvCUR_set(sv, final_len); 
     254} 
     255 
    223256static SV * Load(char *s) { 
    224257    SYMID v; 
     
    269302    } 
    270303 
     304#ifndef YAML_IS_JSON 
    271305    if (sv_isobject(sv)) { 
    272306        ref = savepv(sv_reftype(SvRV(sv), TRUE)); 
     
    281315        strcat(tag, ref); 
    282316    } 
     317#endif 
    283318 
    284319    if (SvROK(sv)) { 
     
    288323    } 
    289324 
    290 #define OBJOF(a) (*tag ? tag : a) 
    291325    switch (SvTYPE(sv)) { 
    292326        case SVt_NULL: { return; } 
    293         case SVt_PV: 
    294327        case SVt_PVIV: 
    295328        case SVt_PVNV: { 
    296             if (SvCUR(sv) > 0) { 
    297                 syck_emit_scalar(e, OBJOF("string"), SCALAR_STRING, 0, 0, 0, SvPVX(sv), SvCUR(sv)); 
     329            if (sv_len(sv) > 0) { 
     330                syck_emit_scalar(e, OBJOF("string"), SvNIOK(sv) ? SCALAR_NUMBER : SCALAR_STRING, 0, 0, 0, SvPV_nolen(sv), sv_len(sv)); 
    298331            } 
    299332            else { 
     
    303336        } 
    304337        case SVt_IV: 
    305         case SVt_NV: 
     338        case SVt_NV: { 
     339            if (sv_len(sv) > 0) { 
     340                syck_emit_scalar(e, OBJOF("string"), SCALAR_NUMBER, 0, 0, 0, SvPV_nolen(sv), sv_len(sv)); 
     341            } 
     342            else { 
     343                syck_emit_scalar(e, OBJOF("string"), SCALAR_QUOTED, 0, 0, 0, "", 0); 
     344            } 
     345            break; 
     346        } 
     347        case SVt_PV: 
    306348        case SVt_PVMG: 
    307349        case SVt_PVBM: 
    308350        case SVt_PVLV: { 
    309351            if (sv_len(sv) > 0) { 
    310                 syck_emit_scalar(e, OBJOF("string"), SCALAR_NUMBER, 0, 0, 0, SvPV_nolen(sv), sv_len(sv)); 
     352                syck_emit_scalar(e, OBJOF("string"), SCALAR_STRING, 0, 0, 0, SvPV_nolen(sv), sv_len(sv)); 
    311353            } 
    312354            else { 
     
    403445#endif 
    404446 
     447#ifdef YAML_IS_JSON 
     448    perl_json_postprocess(out); 
     449#endif 
     450 
    405451    return out; 
    406452} 
  • JSON-Syck/trunk/t/01_json.t

    r1724 r1725  
    33use Test::More; 
    44use JSON::Syck; 
    5 use JSON; 
    65use Storable; 
     6 
     7our $HAS_JSON = 0; 
     8eval { require JSON; $HAS_JSON = 1 }; 
    79 
    810$Data::Dumper::Indent = 0; 
     
    2426); 
    2527 
    26 plan tests => scalar @tests
     28plan tests => scalar @tests * (1 + $HAS_JSON)
    2729 
    28 my $conv = JSON::Converter->new
     30my $conv = $HAS_JSON ? JSON::Converter->new : undef
    2931 
    3032for my $test (@tests) { 
    3133    my $data = eval { JSON::Syck::Load($test) }; 
    3234    my $json = JSON::Syck::Dump($data); 
     35 
     36    # don't bother white spaces 
     37    for ($test, $json) { 
     38        s/([,:]) /$1/eg; 
     39    } 
    3340    is $json, $test, "roundtrip $test -> " . Dumper($data) . " -> $json"; 
    3441 
    3542    # try parsing the data with JSON.pm 
    36     my $data_pp = JSON::jsonToObj($json); 
    37     is_deeply $data_pp, $data, "compatibility with JSON.pm $test"; 
     43    if ($HAS_JSON) { 
     44        my $data_pp = eval { JSON::jsonToObj($json) }; 
     45        is_deeply $data_pp, $data, "compatibility with JSON.pm $test"; 
     46    } 
    3847}