<?php class jsont_class { $value) { $nk = $this->ecran_js2php($key); $r[$nk] = $value; } foreach ($self as $key => $value) { $nk = $this->ecran_js2php($key); $s[$nk] = $value; } $rules = $r; $self = $s; ////////////////////////////////// foreach ($rules as $rule => $___) { if (substr($rule, 0, 4) != "self") { $rules['self.' . $rule] = $rules[$rule]; } } } private function check_is_func($str_f) { return (substr($str_f, 0, 8) == "function"); } private function run_func($str_f, $expr) { if ($this->check_is_func($str_f)) { //return " FBODY: [" . $str_f . "] ARGS: [" . ($expr != null) ? $expr:$obj . "] "; //ORIGINAL: $param = eval($expr); $param = null; $this->expand_chain($expr, $param); $body = strstr($str_f, "{"); $func = create_function('', $body); return $func($param); } return $this->debug ? ("ERR: run_func(). NOT FUNCTION: FBODY: [" . $str_f . "] ARGS: [" . $expr . "] "):""; } private function expand($a, $e) { return (substr($e = preg_replace('/^\$/', $e, $a, 1), 0, 4) != "self") ? 'self.' . $e : $e; } private function pa_h1($p) { return $this->processArg($p[1], $this->pa_h1e); } private function pa_h2($p) { //IN ORIGINAL HERE CALCULATING (!)STRING FUNC/FUNC_ARG WICH RUN AFTER THAT BY eval //ORIGINAL: return $this->rules['self.' . $p[1] . '](' . $this->expand($p[2], $this->pa_h2e . ')' ); //BUT IN PHP VERSION WE RUN FUNCTION HERE return $this->run_func($this->rules['self.' . $p[1]], $this->expand($p[2], $this->pa_h2e) ); } private function trf($expr, $s) { $this->pa_h1e = $expr; return preg_replace_callback('/{([A-Za-z0-9_\$\.\[\]\'@\(\)]+)}/', array($this, 'pa_h1'), $s); } private function apply($expr) { //ORIGINAL: $x = preg_replace('/\[[0-9]+\]/', "[*]", $expr); $x = preg_replace('/.[0-9]+/', ".*", $expr); $res = null; if (isset($this->rules[$x])) { $rx = $this->rules[$x]; if(gettype($rx) == "string") { if ($this->check_is_func($rx)) { //ORIGINAL: $res = $this->trf($expr, (string)$rx(eval(expr)))); $res = $this->trf($expr, (string)$this->run_func($rx, $expr)); } else { $res = $this->trf($expr, $rx); } } } else { $res = $this->jst_eval($expr); } return $res; } private function processArg($arg, $parentExpr) { $res = ""; $this->output = true; if ($arg[0] == "@") { $this->pa_h2e = $parentExpr; //ORIGINAL: $res = eval(preg_replace_callback('/@([A-za-z0-9_]+)\(([A-Za-z0-9_\$\.\[\]\']+)\)/', array($this, 'pa_h2'), $arg, 1)); $res = preg_replace_callback('/@([A-za-z0-9_]+)\(([A-Za-z0-9_\$\.\[\]\']+)\)/', array($this, 'pa_h2'), $arg, 1); } else if ($arg != "$") { $res = $this->apply($this->expand($arg, $parentExpr)); } else { $res = $this->jst_eval($parentExpr); } $this->output = false; return $res; } private function expand_chain($chain_, &$ret_val) { $ok = false; $ret_val = null; //$chain_ = str_replace(" ", "", $chain_); $chain = explode(".", $chain_); if($sc = sizeof($chain)) { $rt = null; for($i = 0; $i<$sc; $i++) { $key = $chain[$i]; if($key == "self") { $rt = $this->self; $ok = true; continue; } if(!array_key_exists($key, $rt)) { $rt = null; $ok = false; break; } $rt = $rt[$key]; } $ret_val = $rt; } if($this->debug && !$ok) { print("ERROR: expand_chain() CAN'T EXPAND THE PARAM: [" . $chain_ . "] CHECK YOU JSONT SCRIPT SYNTAX" ); }; return $ok; } private function jst_eval($expr) { $res = ""; if($this->expand_chain($expr, $v)) { if(is_array($v)) { foreach($v as $key => $value) { $res .= $this->apply($expr . '.' . $key ); } } else { if ($this->output) { $res .= $v; } } } return $res; } function jsonT($self_, $rules_) { $this->rules = $rules_; $this->self = $self_; $this->init($this->rules, $this->self); return $this->apply("self"); } } ////////////////////////////////////////// // SAMPLE HERE ////////////////////////////////////////// function j2o($j2o_, &$arr, &$json_str) { if($j2o_) $arr = json_decode($json_str, TRUE); else $json_str = json_encode($arr); } function show($s) { return preg_replace('/\n/', "<br/>", preg_replace('/>/', ">", preg_replace('/</', "<", preg_replace('/&/', "&", $s . "\n")))) . "<hr/>"; } main(); function main() { $t = array(); $o = array(); $t[1] = '{ "self": "<table>{pnt}</table>", "pnt": "<tr><td>{pnt.x}</td><td>{pnt.y}</td></tr>" }'; $o[1] = '{ "pnt": { "x":2, "y":3 }}'; $t[2] = '{ "self": "<table><tr>{$}</tr></table>", "self[*]": "<td>{$}</td>" }'; $o[2] = '[ 1, 2 ]'; $t[3] = '{ "self": "<table>\n{$}\n</table>", "self[*]": "<tr>{$}</tr>\n", "self[*][*]": "<td>{$}</td>" }'; $o[3] = '[[1,2],[3,4]]'; $t[4] = '{ "self": "<div>\n{p}\n</div>", "p": "<table><tr>{$}</tr></table>\n", "p[*]": "<td>{$.x}</td><td>{$.y}</td>" }'; $o[4] = '{"a":"hello", "p":[{"x":1,"y":2},{"x":3,"y":4}]}'; $t[5] = '{ "self": "<a href=\"{uri}\" title=\"{title}\">{$.title}</a>" }'; $o[5] = '{ "uri":"http://somewhere.org", "title":"somewhere homepage" }'; $t[6] = '{ "menu": "<menu>\n <header>{menu.header}</header>\n{menu.items}</menu>", "menu.items[*]": "function(){$x = func_get_args(); return $x[0] ? \" <item action=\" . $x[0][\"id\"] . \" id=\" . $x[0][\"id\"] . \">\" . (array_key_exists(\"label\", $x[0]) ? $x[0][\"label\"]:$x[0][\"id\"]) . \"</li>\n\" : \" <separator/>\n\";}" }'; $o[6] = '{"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOut", "label": "Zoom Out"}, {"id": "OriginalView", "label": "Original View"}, null, {"id": "Quality"}, {"id": "Pause"}, {"id": "Mute"}, null, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View SVG"}, {"id": "ViewSource", "label": "View Source"}, {"id": "SaveAs", "label": "Save As"}, null, {"id": "Help"}, {"id": "About", "label": "About Adobe CVG Viewer..."} ]}}'; $t[7] = '{ "self": "<p> {a} {b} {c} {d} </p>" }'; $o[7] = '{ "a":false, "b":null, "d":true }'; $t[8] = '{ "self": "<p> {a} </p>", "a": "function() {$x = func_get_args(); return $x[0] ? \"black\" : \"white\";}" }'; $o[8] = '{ "a": true }'; $t[9] = '{ "A": "<div>{A.a1} and {A.a2}</div>" }'; $o[9] = '{ "A" : { "a1": "a1val", "a2": "a2val" }, "B" : { "b1": "b1val", "b2": "b2val" } }'; $o[10] = '{ "line": { "p1": {"x":2, "y":3}, "p2": {"x":4, "y":5} }}'; $t[10] = '{ "self": "<svg>{line}</svg>", "line": "<line x1=\"{$.p1.x}\" y1=\"{$.p1.y}\" x2=\"{$.p2.x}\" y2=\"{$.p2.y}\" />" }'; $o[11] = '["red", "green", "blue"]'; $t[11] = '{"self": "<ul>\n{$}</ul>\n", "self[*]": " <li>{$}</li>\n"}'; $o[12] = '{ "color": "blue", "closed": true, "points": [[10,10],[20,10],[20,20],[10,20]] }'; $t[12] = '{ "self": "<svg><{closed} stroke=\"{color}\" points=\"{points}\" /></svg>", "closed": "function(){$x = func_get_args(); return $x[0] ? \"polygon\" : \"polyline\";}", "points[*][*]": "{$} " }'; $o[13] = '{"menu": { "id": "file", "value": "File:", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }}'; $t[13] = '{ "menu": "<menu id=\"{$.id}\" value=\"{$.value}\">\n <popup>\n{$.popup.menuitem} </popup>\n</menu>", "menu.popup.menuitem[*]": " <menuitem value=\"{$.value}\" onclick=\"{$.onclick}\" />\n" }'; $o[14] = '[{"u":"http://www.ericclapton.com/hello", "d":"Eric Clapton", "t":["guitarist","blues","rock"]}, {"u":"http://www.bbking.com/", "d":"BB King : Official Site", "t":["guitarist","blues"]}, {"u":"http://www.stevieraysbluesbar.com/", "d":"Louisville\ House of Blues", "t":["guitarist","blues","rock"]}]'; $t[14] = '{ "self": "<ul>\n{$}</ul>", "self[*]": " <li>\n <img style=\"position:absolute;display:none;\" width=\"16\" height=\"16\"\n onload=\"showImage(this)\" src=\"{@icon($.u)}\"/>\n <a style=\"margin-left:20px;\" href=\"{$.u}\">{$.d}</a>\n </li>\n", "icon": "function(){$x = func_get_args(); $pieces = explode(\"/\", $x[0]); return join(\"/\", array_splice($pieces, 0, 3)) . \"/favicon.ico\";}"}'; $t[15] = '{ "self": "<p> {$.a} </p>", "a": "function(){$x = func_get_args(); return $x[0] ? \"black\" : \"white\"; }" }'; $o[15] = '{ "a": true }'; $t[16] = '{ "self": "<p> {a}, {b}, {bx} </p>", "b": "hello", "bx": "" }'; $o[16] = '{ "a": false, "b": null }'; $t[17] = '{ "self": "function(){ $x = func_get_args(); return count($x[0]); }" }'; $o[17] = '[ 1, 2, 1 ]'; $jstc = new jsont_class(); $jst = array(); $jso = array(); $N1 = 1; // 1 $N2 = 17; // 17 for($i=$N1; $i<$N2+1; $i++ ) { j2o(true, $jso[$i], $o[$i]); j2o(true, $jst[$i], $t[$i]); print(show($jstc->jsonT($jso[$i], $jst[$i]))); } } ?>