public static function jsonSerialize($value) { $serializer = new JsonSerializer(); $serializer->defineSerialization('Exception', [get_class(), 'serializeException'], [get_class(), 'unserializeException']); //if (is_object($value) && is_a(get_class($value), '\Exception', true)) { if (is_object($value) && static::isClassException(get_class($value))) { $serializer->defineSerialization(get_class($value), [get_class(), 'serializeException'], [get_class(), 'unserializeException']); } $serializedValue = $serializer->serialize($value); return $serializedValue; }
$serial = json_decode($data, true); ok(!isset($serial['lines'][0]['cache']), 'static properties should always be omitted'); eq($serial['lines'][0]['data'], 456, 'private properties should be included by default'); $prop = new ReflectionProperty($output->lines[0], 'data'); $prop->setAccessible(true); $json->skipPrivateProperties(); $data = $json->serialize($input); $output = $json->unserialize($data); $serial = json_decode($data, true); ok(!isset($serial['lines'][0]['data']), 'private properties should be omitted after skipPrivateProperties()'); eq($prop->getValue($output->lines[0]), 123, 'private properties should initialize to their default value'); }); test('Can serialize/unserialize standard objects', function () { $serializer = new JsonSerializer(); $input = (object) array('foo' => 'bar'); $json = $serializer->serialize($input); $data = json_decode($json, true); eq($data[JsonSerializer::TYPE], JsonSerializer::STD_CLASS, 'stdClass object gets tagged'); ok(isset($data['foo']), 'undefined property is preserved'); eq($data['foo'], 'bar', 'undefined property value is preserved'); $output = $serializer->unserialize($json); ok(isset($output->foo), 'object property is restored'); eq($output->foo, $input->foo, 'property value is restored'); }); test('Can unserialize legacy array/hash values', function () { $array = array('foo', 'bar', 'baz'); $input = array('array' => $array, 'hash' => array(JsonSerializer::TYPE => JsonSerializer::HASH, 'foo' => 1, 'bar' => 2, 'baz' => 3)); $serializer = new JsonSerializer(); $output = $serializer->unserialize(json_encode($input)); eq($output['array'], $array, 'correctly unserializes a straight array'); ok(!isset($output['hash'][JsonSerializer::TYPE]), 'legacy hash tag detected and removed');