3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* simplejson - a tiny JSON parser for older PHP versions ------------ The main purpose of this is to allow the parsing of JSON encoded strings into PHP native structures, or PHP objects encoding into JSON strings. Primary target for this are mature systems running versions of PHP older than 5.2, which provides this functionality. The functions are confirmed to work on PHP as old as 4.1.2. The functions do not care about character encoding and will do nothing to magically fix character set issues. They'll work with the data as-provided and won't, for example, (un)escape \u0000 or \x00 characters. WARNING: Be aware that the string input is being "evaluated" and run by this function with all the implications that includes! ------------ Copyright (C) 2006 Borgar Thorsteinsson [borgar.undraland.com] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------ */ /** * Parses a JSON string into a PHP variable. * @param string $json The JSON string to be parsed. * @param bool $assoc Optional flag to force all objects into associative arrays. * @return mixed Parsed structure as object or array, or null on parser failure. */ function fromJSON ( $json, $assoc = false ) { /* by default we don't tolerate ' as string delimiters if you need this, then simply change the comments on the following lines: */ // $matchString = '/(".*?(?<!\\\\)"|\'.*?(?<!\\\\)\')/'; $matchString = '/".*?(?<!\\\\)"/'; // safety / validity test $t = preg_replace( $matchString, '', $json ); $t = preg_replace( '/[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/', '', $t ); if ($t != '') { return null; } // build to/from hashes for all strings in the structure $s2m = array(); $m2s = array(); preg_match_all( $matchString, $json, $m ); foreach ($m[0] as $s) { $hash = '"' . md5( $s ) . '"'; $s2m[$s] = $hash; $m2s[$hash] = str_replace( '$', '\$', $s ); // prevent $ magic } // hide the strings $json = strtr( $json, $s2m ); // convert JS notation to PHP notation $a = ($assoc) ? '' : '(object) '; $json = strtr( $json, array( ':' => '=>', '[' => 'array(', '{' => "{$a}array(", ']' => ')', '}' => ')' ) ); // remove leading zeros to prevent incorrect type casting $json = preg_replace( '~([\s\(,>])(-?)0~', '$1$2', $json ); // return the strings $json = strtr( $json, $m2s ); /* "eval" string and return results. As there is no try statement in PHP4, the trick here is to suppress any parser errors while a function is built and then run the function if it got made. */ $f = @create_function( '', "return {$json};" ); $r = ($f) ? $f() : null; // free mem (shouldn't really be needed, but it's polite) unset( $s2m ); unset( $m2s ); unset( $f ); return $r; } /** * Encodes a PHP variable into a JSON string. * @param mixed $value A PHP variable to be encoded. */ function toJSON ( $value ) { if ($value === null) { return 'null'; }; // gettype fails on null? $out = ''; $esc = "\"\\/\n\r\t" . chr( 8 ) . chr( 12 ); // escaped chars $l = '.'; // decimal point switch ( gettype( $value ) ) { case 'boolean': $out .= $value ? 'true' : 'false'; break; case 'float': case 'double': // PHP uses the decimal point of the current locale but JSON expects %x2E $l = localeconv(); $l = $l['decimal_point']; // fallthrough... case 'integer': $out .= str_replace( $l, '.', $value ); // what, no getlocale? break; case 'array': // if array only has numeric keys, and is sequential... ? for ($i = 0; ($i < count( $value ) && isset( $value[$i]) ); $i++); if ($i === count($value)) { // it's a "true" array... or close enough $out .= '[' . implode(',', array_map('toJSON', $value)) . ']'; break; } // fallthrough to object for associative arrays... case 'object': $arr = is_object($value) ? get_object_vars($value) : $value; $b = array(); foreach ($arr as $k => $v) { $b[] = '"' . addcslashes($k, $esc) . '":' . toJSON($v); } $out .= '{' . implode( ',', $b ) . '}'; break; default: // anything else is treated as a string return '"' . addcslashes($value, $esc) . '"'; break; } return $out; } ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jiWi2
function name:  (null)
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  171     0  E > > RETURN                                                   1

Function fromjson:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 18
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 44
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 44
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 53
Branch analysis from position: 51
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 89, Position 2 = 93
Branch analysis from position: 89
1 jumps found. (Code = 42) Position 1 = 94
Branch analysis from position: 94
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 93
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 89, Position 2 = 93
Branch analysis from position: 89
Branch analysis from position: 93
Branch analysis from position: 44
filename:       /in/jiWi2
function name:  fromJSON
number of ops:  100
compiled vars:  !0 = $json, !1 = $assoc, !2 = $matchString, !3 = $t, !4 = $s2m, !5 = $m2s, !6 = $m, !7 = $s, !8 = $hash, !9 = $a, !10 = $f, !11 = $r
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
   64     2        ASSIGN                                                   !2, '%2F%22.%2A%3F%28%3F%3C%21%5C%5C%29%22%2F'
   67     3        INIT_FCALL                                               'preg_replace'
          4        SEND_VAR                                                 !2
          5        SEND_VAL                                                 ''
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $13     
          8        ASSIGN                                                   !3, $13
   68     9        INIT_FCALL                                               'preg_replace'
         10        SEND_VAL                                                 '%2F%5B%2C%3A%7B%7D%5C%5B%5C%5D0-9.%5C-%2BEaeflnr-u+%5Cn%5Cr%5Ct%5D%2F'
         11        SEND_VAL                                                 ''
         12        SEND_VAR                                                 !3
         13        DO_ICALL                                         $15     
         14        ASSIGN                                                   !3, $15
   69    15        IS_NOT_EQUAL                                             !3, ''
         16      > JMPZ                                                     ~17, ->18
         17    > > RETURN                                                   null
   72    18    >   ASSIGN                                                   !4, <array>
   73    19        ASSIGN                                                   !5, <array>
   74    20        INIT_FCALL                                               'preg_match_all'
         21        SEND_VAR                                                 !2
         22        SEND_VAR                                                 !0
         23        SEND_REF                                                 !6
         24        DO_ICALL                                                 
   75    25        FETCH_DIM_R                                      ~21     !6, 0
         26      > FE_RESET_R                                       $22     ~21, ->44
         27    > > FE_FETCH_R                                               $22, !7, ->44
   76    28    >   INIT_FCALL                                               'md5'
         29        SEND_VAR                                                 !7
         30        DO_ICALL                                         $23     
         31        CONCAT                                           ~24     '%22', $23
         32        CONCAT                                           ~25     ~24, '%22'
         33        ASSIGN                                                   !8, ~25
   77    34        ASSIGN_DIM                                               !4, !7
         35        OP_DATA                                                  !8
   78    36        INIT_FCALL                                               'str_replace'
         37        SEND_VAL                                                 '%24'
         38        SEND_VAL                                                 '%5C%24'
         39        SEND_VAR                                                 !7
         40        DO_ICALL                                         $29     
         41        ASSIGN_DIM                                               !5, !8
         42        OP_DATA                                                  $29
   75    43      > JMP                                                      ->27
         44    >   FE_FREE                                                  $22
   82    45        INIT_FCALL                                               'strtr'
         46        SEND_VAR                                                 !0
         47        SEND_VAR                                                 !4
         48        DO_ICALL                                         $30     
         49        ASSIGN                                                   !0, $30
   85    50      > JMPZ                                                     !1, ->53
         51    >   QM_ASSIGN                                        ~32     ''
         52      > JMP                                                      ->54
         53    >   QM_ASSIGN                                        ~32     '%28object%29+'
         54    >   ASSIGN                                                   !9, ~32
   86    55        INIT_FCALL                                               'strtr'
         56        SEND_VAR                                                 !0
   88    57        INIT_ARRAY                                       ~34     '%3D%3E', '%3A'
   89    58        ADD_ARRAY_ELEMENT                                ~34     'array%28', '%5B'
   90    59        NOP                                                      
         60        FAST_CONCAT                                      ~35     !9, 'array%28'
         61        ADD_ARRAY_ELEMENT                                ~34     ~35, '%7B'
   91    62        ADD_ARRAY_ELEMENT                                ~34     '%29', '%5D'
   92    63        ADD_ARRAY_ELEMENT                                ~34     '%29', '%7D'
         64        SEND_VAL                                                 ~34
         65        DO_ICALL                                         $36     
   86    66        ASSIGN                                                   !0, $36
   97    67        INIT_FCALL                                               'preg_replace'
         68        SEND_VAL                                                 '%7E%28%5B%5Cs%5C%28%2C%3E%5D%29%28-%3F%290%7E'
         69        SEND_VAL                                                 '%241%242'
         70        SEND_VAR                                                 !0
         71        DO_ICALL                                         $38     
         72        ASSIGN                                                   !0, $38
  100    73        INIT_FCALL                                               'strtr'
         74        SEND_VAR                                                 !0
         75        SEND_VAR                                                 !5
         76        DO_ICALL                                         $40     
         77        ASSIGN                                                   !0, $40
  106    78        BEGIN_SILENCE                                    ~42     
         79        INIT_FCALL_BY_NAME                                       'create_function'
         80        SEND_VAL_EX                                              ''
         81        ROPE_INIT                                     3  ~44     'return+'
         82        ROPE_ADD                                      1  ~44     ~44, !0
         83        ROPE_END                                      2  ~43     ~44, '%3B'
         84        SEND_VAL_EX                                              ~43
         85        DO_FCALL                                      0  $46     
         86        END_SILENCE                                              ~42
         87        ASSIGN                                                   !10, $46
  107    88      > JMPZ                                                     !10, ->93
         89    >   INIT_DYNAMIC_CALL                                        !10
         90        DO_FCALL                                      0  $48     
         91        QM_ASSIGN                                        ~49     $48
         92      > JMP                                                      ->94
         93    >   QM_ASSIGN                                        ~49     null
         94    >   ASSIGN                                                   !11, ~49
  110    95        UNSET_CV                                                 !4
         96        UNSET_CV                                                 !5
         97        UNSET_CV                                                 !10
  112    98      > RETURN                                                   !11
  113    99*     > RETURN                                                   null

End of function fromjson

Function tojson:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
8 jumps found. (Code = 188) Position 1 = 22, Position 2 = 28, Position 3 = 28, Position 4 = 33, Position 5 = 40, Position 6 = 64, Position 7 = 99, Position 8 = 9
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 25
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 108
Branch analysis from position: 108
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 108
Branch analysis from position: 108
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 108
Branch analysis from position: 108
Branch analysis from position: 28
Branch analysis from position: 33
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
2 jumps found. (Code = 46) Position 1 = 46, Position 2 = 48
Branch analysis from position: 46
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 42
Branch analysis from position: 49
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 64
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 108
Branch analysis from position: 108
Branch analysis from position: 64
2 jumps found. (Code = 43) Position 1 = 66, Position 2 = 71
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 72
Branch analysis from position: 72
2 jumps found. (Code = 77) Position 1 = 75, Position 2 = 90
Branch analysis from position: 75
2 jumps found. (Code = 78) Position 1 = 76, Position 2 = 90
Branch analysis from position: 76
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
Branch analysis from position: 90
1 jumps found. (Code = 42) Position 1 = 108
Branch analysis from position: 108
Branch analysis from position: 90
Branch analysis from position: 71
2 jumps found. (Code = 77) Position 1 = 75, Position 2 = 90
Branch analysis from position: 75
Branch analysis from position: 90
Branch analysis from position: 42
2 jumps found. (Code = 46) Position 1 = 46, Position 2 = 48
Branch analysis from position: 46
Branch analysis from position: 48
Branch analysis from position: 48
Branch analysis from position: 64
Branch analysis from position: 99
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 22
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 28
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 28
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 33
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 40
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 64
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 99
Branch analysis from position: 99
Branch analysis from position: 64
Branch analysis from position: 40
Branch analysis from position: 33
Branch analysis from position: 28
Branch analysis from position: 28
Branch analysis from position: 22
filename:       /in/jiWi2
function name:  toJSON
number of ops:  111
compiled vars:  !0 = $value, !1 = $out, !2 = $esc, !3 = $l, !4 = $i, !5 = $arr, !6 = $b, !7 = $v, !8 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  119     0  E >   RECV                                             !0      
  121     1        TYPE_CHECK                                    2          !0
          2      > JMPZ                                                     ~9, ->4
          3    > > RETURN                                                   'null'
  123     4    >   ASSIGN                                                   !1, ''
  124     5        ASSIGN                                                   !2, '%22%5C%2F%0A%0D%09%08%0C'
  125     6        ASSIGN                                                   !3, '.'
  127     7        GET_TYPE                                         ~13     !0
          8      > SWITCH_STRING                                            ~13, [ 'boolean':->22, 'float':->28, 'double':->28, 'integer':->33, 'array':->40, 'object':->64, ], ->99
  129     9    >   CASE                                                     ~13, 'boolean'
         10      > JMPNZ                                                    ~14, ->22
  133    11    >   CASE                                                     ~13, 'float'
         12      > JMPNZ                                                    ~14, ->28
  134    13    >   CASE                                                     ~13, 'double'
         14      > JMPNZ                                                    ~14, ->28
  140    15    >   CASE                                                     ~13, 'integer'
         16      > JMPNZ                                                    ~14, ->33
  144    17    >   CASE                                                     ~13, 'array'
         18      > JMPNZ                                                    ~14, ->40
  154    19    >   CASE                                                     ~13, 'object'
         20      > JMPNZ                                                    ~14, ->64
         21    > > JMP                                                      ->99
  130    22    > > JMPZ                                                     !0, ->25
         23    >   QM_ASSIGN                                        ~15     'true'
         24      > JMP                                                      ->26
         25    >   QM_ASSIGN                                        ~15     'false'
         26    >   ASSIGN_OP                                     8          !1, ~15
  131    27      > JMP                                                      ->108
  136    28    >   INIT_FCALL                                               'localeconv'
         29        DO_ICALL                                         $17     
         30        ASSIGN                                                   !3, $17
  137    31        FETCH_DIM_R                                      ~19     !3, 'decimal_point'
         32        ASSIGN                                                   !3, ~19
  141    33    >   INIT_FCALL                                               'str_replace'
         34        SEND_VAR                                                 !3
         35        SEND_VAL                                                 '.'
         36        SEND_VAR                                                 !0
         37        DO_ICALL                                         $21     
         38        ASSIGN_OP                                     8          !1, $21
  142    39      > JMP                                                      ->108
  146    40    >   ASSIGN                                                   !4, 0
         41      > JMP                                                      ->43
         42    >   PRE_INC                                                  !4
         43    >   COUNT                                            ~25     !0
         44        IS_SMALLER                                       ~26     !4, ~25
         45      > JMPZ_EX                                          ~26     ~26, ->48
         46    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~27     !0, !4
         47        BOOL                                             ~26     ~27
         48    > > JMPNZ                                                    ~26, ->42
  147    49    >   COUNT                                            ~28     !0
         50        IS_IDENTICAL                                             !4, ~28
         51      > JMPZ                                                     ~29, ->64
  149    52    >   INIT_FCALL                                               'implode'
         53        SEND_VAL                                                 '%2C'
         54        INIT_FCALL                                               'array_map'
         55        SEND_VAL                                                 'toJSON'
         56        SEND_VAR                                                 !0
         57        DO_ICALL                                         $30     
         58        SEND_VAR                                                 $30
         59        DO_ICALL                                         $31     
         60        CONCAT                                           ~32     '%5B', $31
         61        CONCAT                                           ~33     ~32, '%5D'
         62        ASSIGN_OP                                     8          !1, ~33
  150    63      > JMP                                                      ->108
  155    64    >   TYPE_CHECK                                  256          !0
         65      > JMPZ                                                     ~35, ->71
         66    >   INIT_FCALL                                               'get_object_vars'
         67        SEND_VAR                                                 !0
         68        DO_ICALL                                         $36     
         69        QM_ASSIGN                                        ~37     $36
         70      > JMP                                                      ->72
         71    >   QM_ASSIGN                                        ~37     !0
         72    >   ASSIGN                                                   !5, ~37
  156    73        ASSIGN                                                   !6, <array>
  157    74      > FE_RESET_R                                       $40     !5, ->90
         75    > > FE_FETCH_R                                       ~41     $40, !7, ->90
         76    >   ASSIGN                                                   !8, ~41
  158    77        INIT_FCALL                                               'addcslashes'
         78        SEND_VAR                                                 !8
         79        SEND_VAR                                                 !2
         80        DO_ICALL                                         $44     
         81        CONCAT                                           ~45     '%22', $44
         82        CONCAT                                           ~46     ~45, '%22%3A'
         83        INIT_FCALL_BY_NAME                                       'toJSON'
         84        SEND_VAR_EX                                              !7
         85        DO_FCALL                                      0  $47     
         86        CONCAT                                           ~48     ~46, $47
         87        ASSIGN_DIM                                               !6
         88        OP_DATA                                                  ~48
  157    89      > JMP                                                      ->75
         90    >   FE_FREE                                                  $40
  160    91        INIT_FCALL                                               'implode'
         92        SEND_VAL                                                 '%2C'
         93        SEND_VAR                                                 !6
         94        DO_ICALL                                         $49     
         95        CONCAT                                           ~50     '%7B', $49
         96        CONCAT                                           ~51     ~50, '%7D'
         97        ASSIGN_OP                                     8          !1, ~51
  161    98      > JMP                                                      ->108
  164    99    >   INIT_FCALL                                               'addcslashes'
        100        SEND_VAR                                                 !0
        101        SEND_VAR                                                 !2
        102        DO_ICALL                                         $53     
        103        CONCAT                                           ~54     '%22', $53
        104        CONCAT                                           ~55     ~54, '%22'
        105        FREE                                                     ~13
        106      > RETURN                                                   ~55
  165   107*       JMP                                                      ->108
        108    >   FREE                                                     ~13
  167   109      > RETURN                                                   !1
  169   110*     > RETURN                                                   null

End of function tojson

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
166.61 ms | 1416 KiB | 33 Q