--TEST-- JsonProcessor test #10: Serialize --FILE-- <?php use php\format\JsonProcessor; use php\lib\items; $json = new JsonProcessor(); $json->onSerialize('object', function ($val) { return items::toArray((array) $val); }); // ignoring int handler $obj = new stdClass(); $obj->x = 10; $obj->y = 20; $obj->obj = new stdClass(); echo $json->format($obj); ?> --EXPECTF-- [10,20,{}]
--TEST-- JsonProcessor test #5: Deserialize --FILE-- <?php use php\format\JsonProcessor; $json = new JsonProcessor(); var_dump($json->parse('[1,1.1, true, false, null, "foobar"]')); ?> --EXPECT-- array(6) { [0]=> int(1) [1]=> float(1.1) [2]=> bool(true) [3]=> bool(false) [4]=> NULL [5]=> string(6) "foobar" }
--TEST-- JsonProcessor test #9: Serialize --FILE-- <?php use php\format\JsonProcessor; $json = new JsonProcessor(); $json->onSerialize('int', function ($val) { return "int:" . $val; }); $json->onSerialize('array', function ($val) { return $val; }); // ignoring int handler echo $json->format([1, 2]); ?> --EXPECTF-- [1,2]
--TEST-- JsonProcessor test #2 --FILE-- <?php use php\format\JsonProcessor; $json = new JsonProcessor(); var_dump($json->format([1, 1.1, true, false, null, 'foobar'])); $json = new JsonProcessor(JsonProcessor::SERIALIZE_PRETTY_PRINT); echo $json->format([1, 1.1, true, false, null, 'foobar']); ?> --EXPECT-- string(32) "[1,1.1,true,false,null,"foobar"]" [ 1, 1.1, true, false, null, "foobar" ]
--TEST-- JsonProcessor test #11: Serialize --FILE-- <?php use php\format\JsonProcessor; use php\lib\items; class Foo { public $x = 10, $y = 20; } class Bar extends Foo { public $z = 30; } $json = new JsonProcessor(); $json->onClassSerialize('Foo', function (Foo $val) { return "X:{$val->x}, Y:{$val->y}"; }); echo $json->format(new Foo()), "\n"; echo $json->format(new Bar()), "\n"; $json->onClassSerialize('Bar', function (Foo $val) { return "X:{$val->x}, Y:{$val->y}, Z:{$val->z}"; }); echo $json->format(new Bar()), "\n"; $json->onClassSerialize('Foo', null); echo $json->format(new Foo()), "\n"; echo $json->format(new Bar()), "\n"; ?> --EXPECTF-- "X:10, Y:20" "X:10, Y:20"
--TEST-- JsonProcessor test #8: Serialize --FILE-- <?php use php\format\JsonProcessor; $json = new JsonProcessor(); $json->onSerialize('int', function ($val) { return "int:" . $val; }); echo $json->format([1, 2]), "\n"; $json->onSerialize('int', null); echo $json->format([1, 2]), "\n"; ?> --EXPECTF-- ["int:1","int:2"] [1,2]
--TEST-- JsonProcessor test #6: Deserialize --FILE-- <?php use php\format\JsonProcessor; $json = new JsonProcessor(); var_dump($json->parse('{"x": 100, "y": 500}')); ?> --EXPECTF-- object(stdClass)#%d (2) { ["x"]=> int(100) ["y"]=> int(500) }
--TEST-- JsonProcessor test #4: Deserialize --FILE-- <?php use php\format\JsonProcessor; $json = new JsonProcessor(); var_dump($json->parse('1')); var_dump($json->parse('1.1')); var_dump($json->parse('true')); var_dump($json->parse('false')); var_dump($json->parse('null')); var_dump($json->parse('"foobar"')); ?> --EXPECT-- int(1) float(1.1) bool(true) bool(false) NULL string(6) "foobar"
--TEST-- JsonProcessor test #1 --FILE-- <?php use php\format\JsonProcessor; $json = new JsonProcessor(); var_dump($json->format(1)); var_dump($json->format(1.1)); var_dump($json->format(true)); var_dump($json->format(false)); var_dump($json->format(null)); var_dump($json->format('foobar')); ?> --EXPECT-- string(1) "1" string(3) "1.1" string(4) "true" string(5) "false" string(4) "null" string(8) ""foobar""
--TEST-- JsonProcessor test #7: Serialize --FILE-- <?php use php\format\JsonProcessor; $json = new JsonProcessor(); $json->onSerialize('int', function ($val) { return "int:" . $val; }); $json->onSerialize('float', function ($val) { return "float:" . $val; }); $json->onSerialize('bool', function ($val) { return "bool:" . (int) $val; }); $json->onSerialize('null', function ($val) { return "empty:" . $val; }); $json->onSerialize('string', function ($val) { return "str:'" . $val . "'"; }); var_dump($json->format(1)); var_dump($json->format(1.1)); var_dump($json->format(true)); var_dump($json->format(false)); var_dump($json->format(null)); var_dump($json->format('foobar')); ?> --EXPECTF-- string(7) ""int:1"" string(11) ""float:1.1""
--TEST-- JsonProcessor test #3: Objects --FILE-- <?php use php\format\JsonProcessor; $json = new JsonProcessor(); $object = new stdClass(); $object->x = 20; $object->y = 30; echo $json->format($object); ?> --EXPECT-- {"x":20,"y":30}