Ticket #508: json.patch

File json.patch, 28.8 KB (added by bsdz, 13 years ago)

Patch to rename to JSONReader and JSONWriter

  • compilers/json/JSON.pir

     
    1 # Copyright (C) 2005-2008, Parrot Foundation. 
    2  
    3 =head1 NAME 
    4  
    5 JSON (JavaScript Object Notation) is a lightweight data-interchange format. 
    6  
    7 =head1 SYNOPSIS 
    8  
    9 Given a valid JSON string, the compiler will return a PMC containing the 
    10 appropriate values. For example: 
    11  
    12  .local pmc JSON 
    13  JSON = compreg 'JSON' 
    14  $P0 = JSON('[1,2,3]') 
    15  
    16 Will create a pmc that C<does> array, contains the values 1, 2, and 3, and 
    17 store it in register C<$P0>. 
    18  
    19 For more information about the structure of the JSON representation, see the 
    20 documentation at L<http://www.json.org/>. 
    21  
    22 =cut 
    23  
    24 .namespace [ 'JSON' ] 
    25  
    26 .sub '__onload' :load 
    27     load_bytecode 'PGE.pbc' 
    28     load_bytecode 'PGE/Util.pbc' 
    29     load_bytecode 'TGE.pbc' 
    30  
    31     load_bytecode 'compilers/json/JSON/grammar.pbc' 
    32     load_bytecode 'compilers/json/JSON/pge2pir.pbc' 
    33  
    34     $P1 = get_global '__compiler' 
    35     compreg "JSON", $P1 
    36  
    37     $P1 = new 'Hash' 
    38     $P1['\"'] = '"' 
    39     $P1['\\'] = "\\" 
    40     $P1['\/'] = '/' 
    41     $P1['\b'] = "\b" 
    42     $P1['\f'] = "\f" 
    43     $P1['\n'] = "\n" 
    44     $P1['\r'] = "\r" 
    45     $P1['\t'] = "\t" 
    46  
    47     set_root_global [ 'JSON' ], '$escapes', $P1 
    48 .end 
    49  
    50 .sub '__compiler' 
    51     .param string json_string 
    52  
    53    .local pmc parse, match 
    54    parse = get_root_global ['parrot'; 'JSON'], 'value' 
    55  
    56    $P0 = get_root_global ['parrot'; 'PGE'], 'Match' 
    57    match = $P0.'new'(json_string) 
    58    match.'to'(0) 
    59    match = parse(match) 
    60    unless match goto failed 
    61  
    62    .local pmc pirgrammar, pirbuilder, pir 
    63    pirgrammar = new ['JSON'; 'PIR'] 
    64    pirbuilder = pirgrammar.'apply'(match) 
    65    pir = pirbuilder.'get'('result') 
    66  
    67    .local pmc pirc, result 
    68    pirc = compreg "PIR" 
    69    result = pirc(pir) 
    70    .tailcall result() 
    71  
    72   failed: 
    73    $P0 = new 'Exception' 
    74    $P0[0] = "invalid JSON value" 
    75    throw $P0 
    76 .end 
    77  
    78 # Local Variables: 
    79 #   mode: pir 
    80 #   fill-column: 100 
    81 # End: 
    82 # vim: expandtab shiftwidth=4 ft=pir: 
  • compilers/json/postalcodes.pir

    Property changes on: compilers\json\JSONReader.pir
    ___________________________________________________________________
    Added: svn:mergeinfo
       Merged /branches/RELEASE_0_8_2/compilers/json/JSON.pir:r34004-34020
       Merged /branches/removing_stm/compilers/json/JSON.pir:r35464-35502
       Merged /branches/assert_args/compilers/json/JSON.pir:r34776-34857
       Merged /branches/jit_h_files/compilers/json/JSON.pir:r34166-35215
    
     
    6868    $I0 = index json_result, "\r\n\r\n" 
    6969    substr json_result, 0, $I0, "" 
    7070 
    71     load_bytecode 'compilers/json/JSON.pbc' 
     71    load_bytecode 'library/JSONReader.pbc' 
    7272    $P1 = compreg 'JSON' 
    7373    $P2 = $P1(json_result) 
    7474 
  • compilers/json/test.pir

     
    99 
    1010  load_bytecode 'PGE.pbc' 
    1111  load_bytecode 'PGE/Util.pbc' 
    12   load_bytecode 'compilers/json/JSON.pbc' 
     12  load_bytecode 'library/JSONReader.pbc' 
    1313 
    1414  .local pmc JSON 
    1515  JSON = compreg "JSON" 
  • config/gen/makefiles/json.in

     
    99PGE_DIR  := ../../compilers/pge 
    1010TGE_DIR  := ../../compilers/tge 
    1111 
     12PARROT_LIBRARY := ../../runtime/parrot/library 
     13 
    1214# the default target 
    13 all: JSON.pbc 
     15all: $(PARROT_LIBRARY)/JSONReader.pbc 
    1416 
    1517# This is a listing of all targets, that are meant to be called by users 
    1618help: 
    1719        @echo "" 
    1820        @echo "Following targets are available for the user:" 
    1921        @echo "" 
    20         @echo "  all:               JSON.pbc" 
     22        @echo "  all:               JSONReader.pbc" 
    2123        @echo "                     This is the default." 
    2224        @echo "Testing:" 
    2325        @echo "  test:              Run the test suite." 
     
    3638testclean: 
    3739        $(RM_F) "../../t/compilers/json/*.pir" 
    3840 
    39 JSON.pbc : JSON/grammar.pbc JSON/pge2pir.pbc JSON.pir 
    40         $(PARROT) --output=JSON.pbc JSON.pir 
     41$(PARROT_LIBRARY)/JSONReader.pbc : JSON/grammar.pbc JSON/pge2pir.pbc JSONReader.pir 
     42        $(PARROT) --output=$(PARROT_LIBRARY)/JSONReader.pbc JSONReader.pir 
    4143 
    4244JSON/grammar.pbc : JSON/grammar.pir 
    4345        $(PARROT) --output=JSON/grammar.pbc JSON/grammar.pir 
     
    5254        $(PARROT) $(TGE_DIR)/tgc.pir --output=JSON/pge2pir.pir JSON/pge2pir.tg 
    5355 
    5456clean : testclean 
    55         $(RM_F) "JSON/*.pbc" "JSON/*.pir" JSON.pbc 
     57        $(RM_F) "JSON/*.pbc" "JSON/*.pir" $(PARROT_LIBRARY)/JSONReader.pbc 
    5658 
    5759# Local variables: 
    5860#   mode: makefile 
  • config/gen/makefiles/root.in

     
    249249    $(LIBRARY_DIR)/dumper.pbc \ 
    250250    $(LIBRARY_DIR)/yaml_dumper.pbc \ 
    251251    $(LIBRARY_DIR)/Getopt/Obj.pbc \ 
    252     $(LIBRARY_DIR)/JSON.pbc \ 
     252    $(LIBRARY_DIR)/JSONWriter.pbc \ 
    253253    $(LIBRARY_DIR)/Math/Random/mt19937ar.pbc \ 
    254254    $(LIBRARY_DIR)/Math/Rand.pbc \ 
    255255    $(LIBRARY_DIR)/MIME/Base64.pbc \ 
  • MANIFEST

     
    5555compilers/imcc/symreg.c                                     [imcc] 
    5656compilers/imcc/symreg.h                                     [imcc] 
    5757compilers/imcc/unit.h                                       [imcc] 
    58 compilers/json/JSON.pir                                     [json] 
     58compilers/json/JSONReader.pir                               [json] 
    5959compilers/json/JSON/grammar.pg                              [json] 
    6060compilers/json/JSON/pge2pir.tg                              [json] 
    6161compilers/json/postalcodes.pir                              [json] 
     
    11521152runtime/parrot/library/Getopt/Obj.pir                       [library] 
    11531153runtime/parrot/library/HTTP/Daemon.pir                      [library] 
    11541154runtime/parrot/library/Iter.pir                             [library] 
    1155 runtime/parrot/library/JSON.pir                             [library] 
     1155runtime/parrot/library/JSONWriter.pir                       [library] 
    11561156runtime/parrot/library/MIME/Base64.pir                      [library] 
    11571157runtime/parrot/library/Math/Rand.pir                        [library] 
    11581158runtime/parrot/library/Math/Random/mt19937ar.pir            [library] 
  • MANIFEST.generated

     
    55blib/lib/libparrot.1.0.0.dylib                    [main]lib 
    66blib/lib/libparrot.a                              [main]lib 
    77blib/lib/libparrot.dylib                          [main]lib 
     8blib/lib/libparrot.so                             [main]lib 
    89blib/lib/libparrot.so.1.0.0                       [main]lib 
    9 blib/lib/libparrot.so                             [main]lib 
    1010compilers/json/JSON/grammar.pbc                   [json] 
    11 compilers/json/JSON.pbc                           [json] 
    1211compilers/json/JSON/pge2pir.pbc                   [json] 
    1312compilers/nqp/nqp.pbc                             [nqp] 
    1413config/gen/call_list/opengl.in                    [] 
     
    3635include/parrot/extend_vtable.h                    [main]include 
    3736include/parrot/feature.h                          [main]include 
    3837include/parrot/has_header.h                       [main]include 
     38include/parrot/oplib/core_ops.h                   [main]include 
    3939include/parrot/oplib/core_ops_cg.h                [main]include 
    4040include/parrot/oplib/core_ops_cgp.h               [main]include 
    41 include/parrot/oplib/core_ops.h                   [main]include 
    4241include/parrot/oplib/core_ops_switch.h            [main]include 
    4342include/parrot/oplib/ops.h                        [main]include 
    4443include/parrot/pbcversion.h                       [devel]include 
     
    4746include/parrot/platform_limits.h                  [devel]include 
    4847include/parrot/vtable.h                           [main]include 
    4948install_config.fpmc                               [main]lib 
     49installable_parrot                                [main]bin 
     50installable_parrot.exe                            [main]bin 
     51installable_parrot_config                         [main]bin 
    5052installable_parrot_config.exe                     [main]bin 
    51 installable_parrot_config                         [main]bin 
     53installable_parrot_debugger                       [main]bin 
    5254installable_parrot_debugger.exe                   [main]bin 
    53 installable_parrot_debugger                       [main]bin 
    54 installable_parrot.exe                            [main]bin 
    55 installable_parrot                                [main]bin 
     55installable_pbc_disassemble                       [main]bin 
    5656installable_pbc_disassemble.exe                   [main]bin 
    57 installable_pbc_disassemble                       [main]bin 
     57installable_pbc_dump                              [main]bin 
    5858installable_pbc_dump.exe                          [main]bin 
    59 installable_pbc_dump                              [main]bin 
     59installable_pbc_info                              [main]bin 
    6060installable_pbc_info.exe                          [main]bin 
    61 installable_pbc_info                              [main]bin 
     61installable_pbc_merge                             [main]bin 
    6262installable_pbc_merge.exe                         [main]bin 
    63 installable_pbc_merge                             [main]bin 
     63installable_pbc_to_exe                            [main]bin 
    6464installable_pbc_to_exe.exe                        [main]bin 
    65 installable_pbc_to_exe                            [main]bin 
    6665lib/Parrot/Config/Generated.pm                    [devel]lib 
    67 libparrot.dll                                     [main]bin 
    6866lib/Parrot/OpLib/core.pm                          [devel]lib 
     67lib/Parrot/PMC.pm                                 [devel]lib 
    6968lib/Parrot/Pmc2c/PCCMETHOD_BITS.pm                [devel]lib 
    70 lib/Parrot/PMC.pm                                 [devel]lib 
     69libparrot.dll                                     [main]bin 
     70library/JSONReader.pbc                            [json] 
    7171parrot.pc                                         [main]pkgconfig 
     72parrot_debugger                                   [main]bin 
     73parrot_debugger.exe                               [main]bin 
     74pbc_disassemble                                   [main]bin 
    7275pbc_disassemble.exe                               [main]bin 
    73 pbc_disassemble                                   [main]bin 
     76pbc_dump                                          [main]bin 
    7477pbc_dump.exe                                      [main]bin 
    75 pbc_dump                                          [main]bin 
     78pbc_info                                          [main]bin 
    7679pbc_info.exe                                      [main]bin 
    77 pbc_info                                          [main]bin 
     80pbc_merge                                         [main]bin 
    7881pbc_merge.exe                                     [main]bin 
    79 pbc_merge                                         [main]bin 
     82pbc_to_exe                                        [main]bin 
    8083pbc_to_exe.exe                                    [main]bin 
    81 pbc_to_exe                                        [main]bin 
    82 parrot_debugger.exe                               [main]bin 
    83 parrot_debugger                                   [main]bin 
     84pirc                                              [main]bin 
    8485pirc.exe                                          [main]bin 
    85 pirc                                              [main]bin 
    8686runtime/parrot/dynext/digest_group.bundle         [library] 
    8787runtime/parrot/dynext/digest_group.dll            [library] 
    8888runtime/parrot/dynext/digest_group.dylib          [library] 
     
    145145runtime/parrot/include/vtable_methods.pasm        [main] 
    146146runtime/parrot/include/warnings.pasm              [main] 
    147147runtime/parrot/library/CGI/QueryHash.pbc          [main] 
    148 runtime/parrot/library/config.pbc                 [main] 
    149 runtime/parrot/library/config.pir                 [main] 
     148runtime/parrot/library/Data/Dumper.pbc            [main] 
    150149runtime/parrot/library/Data/Dumper/Base.pbc       [main] 
    151150runtime/parrot/library/Data/Dumper/Default.pbc    [main] 
    152 runtime/parrot/library/Data/Dumper.pbc            [main] 
    153 runtime/parrot/library/dumper.pbc                 [main] 
    154151runtime/parrot/library/Getopt/Obj.pbc             [main] 
     152runtime/parrot/library/MIME/Base64.pbc            [main] 
     153runtime/parrot/library/Math/Rand.pbc              [main] 
    155154runtime/parrot/library/Math/Random/mt19937ar.pbc  [main] 
    156 runtime/parrot/library/Math/Rand.pbc              [main] 
    157 runtime/parrot/library/MIME/Base64.pbc            [main] 
    158155runtime/parrot/library/NCI/call_toolkit_init.pbc  [main] 
    159 runtime/parrot/library/ncurses.pbc                [main] 
     156runtime/parrot/library/OpenGL.pbc                 [main] 
    160157runtime/parrot/library/OpenGL_funcs.pir           [main] 
    161 runtime/parrot/library/OpenGL.pbc                 [main] 
    162158runtime/parrot/library/P6object.pbc               [main] 
    163 runtime/parrot/library/Parrot/Capture_PIR.pbc     [main] 
    164 runtime/parrot/library/Parrot/Coroutine.pbc       [main] 
    165 runtime/parrot/library/Parrot/Exception.pbc       [main] 
    166 runtime/parrot/library/Parrot/HLLCompiler.pbc     [main] 
    167 runtime/parrot/library/parrotlib.pbc              [main] 
    168 runtime/parrot/library/pcre.pbc                   [main] 
     159runtime/parrot/library/PCT.pbc                    [pct] 
    169160runtime/parrot/library/PCT/Grammar.pbc            [pct] 
    170161runtime/parrot/library/PCT/HLLCompiler.pbc        [pct] 
    171162runtime/parrot/library/PCT/PAST.pbc               [pct] 
    172 runtime/parrot/library/PCT.pbc                    [pct] 
     163runtime/parrot/library/PGE.pbc                    [main] 
    173164runtime/parrot/library/PGE/Dumper.pbc             [main] 
    174165runtime/parrot/library/PGE/Glob.pbc               [main] 
    175166runtime/parrot/library/PGE/Hs.pbc                 [main] 
    176 runtime/parrot/library/PGE.pbc                    [main] 
    177167runtime/parrot/library/PGE/Perl6Grammar.pbc       [main] 
    178168runtime/parrot/library/PGE/Text.pbc               [main] 
    179169runtime/parrot/library/PGE/Util.pbc               [main] 
     170runtime/parrot/library/Parrot/Capture_PIR.pbc     [main] 
     171runtime/parrot/library/Parrot/Coroutine.pbc       [main] 
     172runtime/parrot/library/Parrot/Exception.pbc       [main] 
     173runtime/parrot/library/Parrot/HLLCompiler.pbc     [main] 
    180174runtime/parrot/library/Protoobject.pbc            [main] 
    181175runtime/parrot/library/Stream/Base.pbc            [main] 
    182176runtime/parrot/library/Stream/Combiner.pbc        [main] 
     
    188182runtime/parrot/library/Stream/Sub.pbc             [main] 
    189183runtime/parrot/library/Stream/Writer.pbc          [main] 
    190184runtime/parrot/library/TGE.pbc                    [tge] 
     185runtime/parrot/library/config.pbc                 [main] 
     186runtime/parrot/library/config.pir                 [main] 
     187runtime/parrot/library/dumper.pbc                 [main] 
     188runtime/parrot/library/ncurses.pbc                [main] 
     189runtime/parrot/library/parrotlib.pbc              [main] 
     190runtime/parrot/library/pcre.pbc                   [main] 
    191191src/call_list.txt                                 [devel]src 
    192192src/glut_callbacks.c                              [] 
    193193src/jit_emit.h                                    [] 
  • ports/cygwin/README

     
    147147  /usr/lib/parrot/1.0.0/library/Getopt/Obj.pir 
    148148  /usr/lib/parrot/1.0.0/library/HTTP/Daemon.pir 
    149149  /usr/lib/parrot/1.0.0/library/Iter.pir 
    150   /usr/lib/parrot/1.0.0/library/JSON.pir 
     150  /usr/lib/parrot/1.0.0/library/JSONWriter.pir 
    151151  /usr/lib/parrot/1.0.0/library/libpcre.pir 
    152152  /usr/lib/parrot/1.0.0/library/Math/Rand.pbc 
    153153  /usr/lib/parrot/1.0.0/library/Math/Rand.pir 
  • runtime/parrot/library/Config/JSON.pir

     
    3333    text = fh.'readall'() 
    3434 
    3535    # convert the text to an object and return it. 
    36     load_bytecode 'compilers/json/JSON.pbc' 
     36    load_bytecode 'library/JSONReader.pbc' 
    3737 
    3838    .local pmc JSON, config 
    3939    JSON = compreg "JSON" 
     
    7777    close $P1 
    7878 
    7979.end 
    80 .include 'library/JSON.pir' 
     80.include 'library/JSONWriter.pir' 
    8181 
    8282# Local Variables: 
    8383#   mode: pir 
  • runtime/parrot/library/JSON.pir

     
    1 =head1 TITLE 
    2  
    3 JSON.pir - PIR implementation of JSON data interchange format. 
    4  
    5 =head1 SYNOPSIS 
    6  
    7 Use the C<_json> method to generate a JSON representation of a PMC. 
    8  
    9  .include 'library/JSON.pir' 
    10  $S0 = _json( $P0 ) 
    11  
    12 To generate a PMC from a JSON string, see L<compilers/json/JSON.pir>. 
    13  
    14 =cut 
    15  
    16 =head1 FUNCTIONS 
    17  
    18 This library provides the following functions: 
    19  
    20 =over 4 
    21  
    22 =item (string) = _json(pmc, ?pretty ) 
    23  
    24 Convert a PMC to a JSON-serialized string. Note: If you pass in a cyclic 
    25 structure, JSON will eventually throw a maximum recursion depth exception. 
    26  
    27 =over 4 
    28  
    29 =item pmc 
    30  
    31 Required. The PMC to dump. 
    32  
    33 =item pretty 
    34  
    35 Optional. Boolean: If true, then the generated string will be very 
    36 readable for humans. Defaults to false, which will generate the 
    37 most compact string possible. 
    38  
    39 =back 
    40  
    41 =cut 
    42  
    43 .const string _json_prefix = '  ' 
    44  
    45 .sub _json 
    46     .param pmc thing 
    47     .param int pretty     :optional 
    48     .param int has_pretty :opt_flag 
    49  
    50     .local string result 
    51  
    52     if has_pretty goto done_init 
    53     pretty = 0 
    54  
    55 done_init: 
    56     result = _json_any(thing,pretty,0) 
    57     unless pretty goto plain 
    58     result .= "\n" 
    59 plain: 
    60     .return (result) 
    61 .end 
    62  
    63 .sub _json_any 
    64     .param pmc thing 
    65     .param int pretty 
    66     .param int indent 
    67  
    68 done_init: 
    69     if_null thing, json_null 
    70  
    71     $I0 = does thing, "array" 
    72     if $I0 goto json_array 
    73  
    74     $I0 = does thing, "hash" 
    75     if $I0 goto json_hash 
    76  
    77     $I0 = does thing, "string" 
    78     if $I0 goto json_string 
    79  
    80     $I0 = does thing, "boolean" 
    81     if $I0 goto json_boolean 
    82  
    83     $I0 = does thing, "integer" 
    84     if $I0 goto json_integer 
    85  
    86     $I0 = does thing, "float" 
    87     if $I0 goto json_float 
    88  
    89     # Default to a null. We could in the future make this more 
    90     # clever, or conditional. 
    91 json_null: 
    92     .tailcall _json_null(thing,pretty,indent) 
    93 json_string: 
    94     .tailcall _json_string(thing,pretty,indent) 
    95 json_array: 
    96     .tailcall _json_array(thing,pretty,indent) 
    97 json_hash: 
    98     .tailcall _json_hash(thing,pretty,indent) 
    99 json_boolean: 
    100     .tailcall _json_boolean(thing,pretty,indent) 
    101 json_integer: 
    102     .tailcall _json_number(thing,pretty,indent) 
    103 json_float: 
    104     .tailcall _json_number(thing,pretty,indent) 
    105  
    106 .end 
    107  
    108 .sub '_json_null' 
    109   .param pmc thing  # ignored, but needed for the ``API'' 
    110   .param int pretty 
    111   .param int indent 
    112  
    113   unless pretty goto plain 
    114   unless indent goto plain 
    115  
    116   .local string result 
    117   result = repeat _json_prefix, indent 
    118   result .= 'null' 
    119   .return (result) 
    120  
    121 plain: 
    122   .return ('null') 
    123 .end 
    124  
    125 .sub '_json_string' 
    126   .param string thing 
    127   .param int pretty 
    128   .param int indent 
    129  
    130   .local string result 
    131  
    132   thing = escape thing 
    133  
    134   result = '"' . thing 
    135   result = result . '"' 
    136  
    137   unless pretty goto plain 
    138   unless indent goto plain 
    139  
    140   $S0 = repeat _json_prefix, indent 
    141   result = $S0 . result 
    142  
    143 plain: 
    144   .return (result) 
    145 .end 
    146  
    147 .sub '_json_boolean' 
    148   .param pmc thing 
    149   .param int pretty 
    150   .param int indent 
    151  
    152   .local string result 
    153  
    154   result = 'true' 
    155   if thing goto got_value 
    156   result = 'false' 
    157  
    158 got_value: 
    159  
    160   unless pretty goto plain 
    161   unless indent goto plain 
    162  
    163   $S0 = repeat _json_prefix, indent 
    164   result = $S0 . result 
    165  
    166 plain: 
    167   .return (result) 
    168 .end 
    169  
    170 .sub '_json_number' 
    171   .param pmc thing 
    172   .param int pretty 
    173   .param int indent 
    174  
    175   .local string result 
    176  
    177   result = thing 
    178  
    179   unless pretty goto plain 
    180   unless indent goto plain 
    181  
    182   $S0 = repeat _json_prefix, indent 
    183   result = $S0 . result 
    184  
    185 plain: 
    186   .return (result) 
    187 .end 
    188  
    189 .sub '_json_array' 
    190   .param pmc thing 
    191   .param int pretty 
    192   .param int indent 
    193  
    194   .local string result 
    195  
    196   result = '[' 
    197   .local int len 
    198   len = thing 
    199  
    200   unless pretty goto pre_loop 
    201   unless indent goto pre_loop 
    202  
    203   $S0 = repeat _json_prefix, indent 
    204   result = $S0 . result 
    205   if len goto pre_loop 
    206   result .= "\n" 
    207  
    208 pre_loop: 
    209   inc indent 
    210   .local int pos 
    211   pos = 0 
    212   unless pretty goto loop 
    213   if len == 0 goto done_loop 
    214   result .= "\n" 
    215  
    216 loop: 
    217   if pos >= len goto done_loop 
    218   $P1 = thing[pos] 
    219   $S0 = _json_any($P1,pretty,indent) 
    220   result .= $S0 
    221   inc pos 
    222   if pos == len goto loop 
    223   result .= "," 
    224   unless pretty goto loop 
    225   result .= "\n" 
    226   goto loop 
    227  
    228 done_loop: 
    229   dec indent 
    230  
    231   .local string optional_newline,optional_indent 
    232   optional_newline = '' 
    233   optional_indent = '' 
    234  
    235   unless pretty goto done 
    236   if len == 0 goto indent1 
    237   optional_newline = "\n" 
    238  
    239 indent1: 
    240   unless indent goto done 
    241   optional_indent = repeat _json_prefix, indent 
    242  
    243 done: 
    244   result .= optional_newline 
    245   result .= optional_indent 
    246   result .= ']' 
    247  
    248   .return (result) 
    249  
    250 .end 
    251  
    252 .sub '_json_hash' 
    253   .param pmc thing 
    254   .param int pretty 
    255   .param int indent 
    256  
    257   .local int not_empty 
    258   not_empty = thing 
    259  
    260   .local pmc keys 
    261   keys = new 'ResizablePMCArray' 
    262   .local pmc iter 
    263   iter = new 'Iterator', thing 
    264   iter = 0 
    265   .local string key 
    266  
    267 iter_loop: 
    268   unless iter, done_iter 
    269   shift key, iter 
    270   push keys, key 
    271   goto iter_loop 
    272  
    273 done_iter: 
    274   keys.'sort'() 
    275  
    276   .local string result,separator 
    277  
    278   result = '{' 
    279   separator = ':' 
    280  
    281   unless pretty goto pre_loop 
    282   separator = ' : ' 
    283   unless indent goto pre_loop 
    284  
    285   $S0 = repeat _json_prefix, indent 
    286   result = $S0 . result 
    287   if not_empty goto pre_loop 
    288   result .= "\n" 
    289  
    290 pre_loop: 
    291   inc indent 
    292   .local int pos 
    293   pos = 0 
    294   unless pretty goto loop 
    295   unless not_empty goto done_loop 
    296   result .= "\n" 
    297  
    298 loop: 
    299   if pos >= not_empty goto done_loop 
    300   key = keys[pos] 
    301   $S0 = _json_string(key,pretty,indent) 
    302   result .= $S0 
    303   result .= separator 
    304   $P1 = thing[key] 
    305   $S0 = _json_any($P1,pretty,indent) 
    306  
    307   # remove any leading whitespace on the value's string. 
    308   unless pretty goto space_loop_skip 
    309  
    310   .local int space_pos,space_len 
    311   space_pos = 0 
    312   space_len = length $S0 
    313  
    314 space_loop: 
    315   if space_pos >= space_len goto space_loop_done 
    316   $I0 = ord $S0, space_pos 
    317   if $I0 != 32 goto space_loop_done 
    318  
    319   inc space_pos 
    320   goto space_loop 
    321  
    322 space_loop_done: 
    323   $S0 = substr $S0, space_pos 
    324  
    325 space_loop_skip: 
    326  
    327   result .= $S0 
    328   inc pos 
    329   if pos == not_empty goto loop 
    330   result .= "," 
    331   unless pretty goto loop 
    332   result .= "\n" 
    333   goto loop 
    334  
    335 done_loop: 
    336   dec indent 
    337  
    338   .local string optional_newline,optional_indent 
    339   optional_newline = '' 
    340   optional_indent = '' 
    341  
    342   unless pretty goto done 
    343   unless not_empty goto indent1 
    344   optional_newline = "\n" 
    345  
    346 indent1: 
    347   unless indent goto done 
    348   optional_indent = repeat _json_prefix, indent 
    349  
    350 done: 
    351   result .= optional_newline 
    352   result .= optional_indent 
    353   result .= '}' 
    354  
    355   .return (result) 
    356 .end 
    357  
    358 =back 
    359  
    360 =head1 TODO 
    361  
    362 =over 4 
    363  
    364 =item 1 
    365  
    366 Thunk a better way to deal with the maximum recursion depth exception (Or make it official) 
    367  
    368 =back 
    369  
    370 =cut 
    371  
    372 # Local Variables: 
    373 #   mode: pir 
    374 #   fill-column: 100 
    375 # End: 
    376 # vim: expandtab shiftwidth=4 ft=pir: 
  • runtime/parrot/library/JSONWriter.pir

     
    11=head1 TITLE 
    22 
    3 JSON.pir - PIR implementation of JSON data interchange format. 
     3JSONWriter.pir - PIR implementation of JSON data interchange format. 
    44 
    55=head1 SYNOPSIS 
    66 
    77Use the C<_json> method to generate a JSON representation of a PMC. 
    88 
    9  .include 'library/JSON.pir' 
     9 .include 'library/JSONWriter.pir' 
    1010 $S0 = _json( $P0 ) 
    1111 
    12 To generate a PMC from a JSON string, see L<compilers/json/JSON.pir>. 
     12To generate a PMC from a JSON string, see L<compilers/json/JSONReader.pir>. 
    1313 
    1414=cut 
    1515 
  • t/compilers/json/from_parrot.t

    Property changes on: runtime\parrot\library\JSONWriter.pir
    ___________________________________________________________________
    Added: svn:mergeinfo
       Merged /branches/assert_args/runtime/parrot/library/JSON.pir:r34776-34857
       Merged /branches/jit_h_files/runtime/parrot/library/JSON.pir:r34166-35215
       Merged /branches/RELEASE_0_8_2/runtime/parrot/library/JSON.pir:r34004-34020
       Merged /branches/removing_stm/runtime/parrot/library/JSON.pir:r35464-35502
    
     
    3333    $S0 = _json( s, 0 ) 
    3434    say $S0 
    3535.end 
    36 .include 'library/JSON.pir' 
     36.include 'library/JSONWriter.pir' 
    3737CODE 
    3838"" 
    3939OUT 
     
    4848    $S0 = _json( s, 0 ) 
    4949    say $S0 
    5050.end 
    51 .include 'library/JSON.pir' 
     51.include 'library/JSONWriter.pir' 
    5252CODE 
    5353"abcdeABCDE01234$%^&*" 
    5454OUT 
     
    6363    $S0 = _json( s, 0 ) 
    6464    say $S0 
    6565.end 
    66 .include 'library/JSON.pir' 
     66.include 'library/JSONWriter.pir' 
    6767CODE 
    6868"abcde\\ABCDE\"01234\n$%^&*" 
    6969OUT 
     
    8989    $S0 = _json( i, 0 ) 
    9090    say $S0 
    9191.end 
    92 .include 'library/JSON.pir' 
     92.include 'library/JSONWriter.pir' 
    9393CODE 
    94940 
    959535 
     
    116116    $S0 = _json( n ) 
    117117    say $S0 
    118118.end 
    119 .include 'library/JSON.pir' 
     119.include 'library/JSONWriter.pir' 
    120120CODE 
    1211210 
    1221222.5 
     
    143143    $S0 = _json( n, 1 ) 
    144144    print $S0 
    145145.end 
    146 .include 'library/JSON.pir' 
     146.include 'library/JSONWriter.pir' 
    147147CODE 
    148148"abcde\\ABCDE\"01234\n$%^&*" 
    149149-42 
     
    172172    $S0 = _json( array, 0 ) 
    173173    say $S0 
    174174.end 
    175 .include 'library/JSON.pir' 
     175.include 'library/JSONWriter.pir' 
    176176CODE 
    177177[0,1,2,3,4,5,6,7,8,9] 
    178178OUT 
     
    199199    $S0 = _json( array, 1 ) 
    200200    print $S0 
    201201.end 
    202 .include 'library/JSON.pir' 
     202.include 'library/JSONWriter.pir' 
    203203CODE 
    204204[ 
    205205  0, 
     
    233233    $S0 = _json( array, 1 ) 
    234234    print $S0 
    235235.end 
    236 .include 'library/JSON.pir' 
     236.include 'library/JSONWriter.pir' 
    237237CODE 
    238238[ 
    239239  35, 
     
    263263    $S0 = _json( array, 1 ) 
    264264    print $S0 
    265265.end 
    266 .include 'library/JSON.pir' 
     266.include 'library/JSONWriter.pir' 
    267267CODE 
    268268[ 
    269269  0, 
     
    290290    $S0 = _json( hash, 1 ) 
    291291    print $S0 
    292292.end 
    293 .include 'library/JSON.pir' 
     293.include 'library/JSONWriter.pir' 
    294294CODE 
    295295{ 
    296296  "alpha" : 29, 
     
    315315    $S0 = _json( hash, 0 ) 
    316316    say $S0 
    317317.end 
    318 .include 'library/JSON.pir' 
     318.include 'library/JSONWriter.pir' 
    319319CODE 
    320320{"alpha":29,"beta":"B","delta":"DELTA","gamma":3.1} 
    321321OUT 
     
    352352    $S0 = _json( world, 1 ) 
    353353    print $S0 
    354354.end 
    355 .include 'library/JSON.pir' 
     355.include 'library/JSONWriter.pir' 
    356356CODE 
    357357{ 
    358358  "population" : 1234567890, 
     
    403403    $S0 = _json( world, 0 ) 
    404404    say $S0 
    405405.end 
    406 .include 'library/JSON.pir' 
     406.include 'library/JSONWriter.pir' 
    407407CODE 
    408408{"population":1234567890,"some_country":[[{"Perl":"Highway","Python":"Grove","Ruby":"Lane"},{}],[]]} 
    409409OUT 
     
    428428    $S0 = _json( s, 1 ) 
    429429    print $S0 
    430430.end 
    431 .include 'library/JSON.pir' 
     431.include 'library/JSONWriter.pir' 
    432432CODE 
    433433"" 
    434434"" 
     
    456456    $S0 = _json( i, 1 ) 
    457457    print $S0 
    458458.end 
    459 .include 'library/JSON.pir' 
     459.include 'library/JSONWriter.pir' 
    460460CODE 
    4614610 
    4624620 
     
    484484    $S0 = _json( b, 1 ) 
    485485    print $S0 
    486486.end 
    487 .include 'library/JSON.pir' 
     487.include 'library/JSONWriter.pir' 
    488488CODE 
    489489false 
    490490false 
     
    510510    $S0 = _json( n, 1 ) 
    511511    print $S0 
    512512.end 
    513 .include 'library/JSON.pir' 
     513.include 'library/JSONWriter.pir' 
    514514CODE 
    515515null 
    516516null 
  • t/compilers/json/to_parrot.t

     
    689689    return pir_output_is( <<"END_PIR", $dumped, $reason, %args ); 
    690690 
    691691.sub test :main 
    692     load_bytecode 'compilers/json/JSON.pbc' 
     692    load_bytecode 'library/JSONReader.pbc' 
    693693    load_bytecode 'library/dumper.pbc' 
    694694 
    695695    .local pmc JSON, result 
     
    710710    return pir_error_output_like( <<"END_PIR", qr/not a valid JSON value/, $reason, %args ); 
    711711 
    712712.sub test :main 
    713     load_bytecode 'compilers/json/JSON.pbc' 
     713    load_bytecode 'library/JSONReader.pbc' 
    714714 
    715715    .local pmc JSON, result 
    716716    JSON = compreg "JSON" 
  • t/library/mime_base64.t

     
    2424    load_bytecode 'MIME/Base64.pir' 
    2525    load_bytecode 'PGE.pbc' 
    2626    load_bytecode 'PGE/Util.pbc' 
    27     load_bytecode 'compilers/json/JSON.pbc' 
     27    load_bytecode 'library/JSONReader.pbc' 
    2828 
    2929    .local pmc plan, is, ok 
    3030    plan = get_hll_global [ 'Test'; 'More' ], 'plan'