package.xml0000664000175000017500000002532612654067664011327 0ustar janjan Horde_Serialize pear.horde.org Data Encapulation API An interface to various methods of encapsulating data. Chuck Hagenbuch chuck chuck@horde.org yes Jan Schneider jan jan@horde.org yes Michael Slusarz slusarz slusarz@horde.org yes 2016-02-02 2.0.5 1.0.0 stable stable LGPL-2.1 * [jan] Mark PHP 7 as supported. 5.3.0 8.0.0alpha1 8.0.0alpha1 1.7.0 Horde_Exception pear.horde.org 2.0.0 3.0.0alpha1 3.0.0alpha1 Horde_Util pear.horde.org 2.0.0 3.0.0alpha1 3.0.0alpha1 Horde_Imap_Client pear.horde.org 2.0.0 3.0.0alpha1 3.0.0alpha1 Horde_Mime pear.horde.org 2.0.0 3.0.0alpha1 3.0.0alpha1 Horde_Test pear.horde.org 2.1.0 3.0.0alpha1 3.0.0alpha1 lzf pecl.php.net 1.5.2 lzf bz2 json wddx zlib 0.0.1 0.0.1 alpha alpha 2004-01-01 LGPL-2.1 Initial packaging. 2006-05-08 0.0.2 0.0.2 alpha alpha LGPL-2.1 Converted to package.xml 2.0 for pear.horde.org 1.0.0alpha1 1.0.0 alpha alpha 2011-03-08 LGPL-2.1 * First alpha release for Horde 4. * Throw exceptions rather than returning PEAR_Errors. * Add support for limited error handling for json encoding errors (requires PHP 5.3+). 1.0.0beta1 1.0.0 beta beta 2011-03-16 LGPL-2.1 * First beta release for Horde 4. 1.0.0RC1 1.0.0 beta beta 2011-03-22 LGPL-2.1 * First release candidate for Horde 4. 1.0.0RC2 1.0.0 beta beta 2011-03-29 LGPL-2.1 * Second release candidate for Horde 4. * [jan] Drop SQLXML serialization method (Request #9705). 1.0.0 1.0.0 stable stable 2011-04-06 LGPL-2.1 * First stable release for Horde 4. 1.0.1 1.0.0 stable stable 2011-05-03 LGPL-2.1 * [jan] Fix dependency. 1.0.2 1.0.0 stable stable 2011-11-08 LGPL-2.1 * [jan] Fix unit tests. 2.0.0alpha1 1.0.0 alpha stable 2012-07-05 LGPL-2.1 * First alpha release for Horde 5. 2.0.0beta1 1.0.0 beta stable 2012-07-19 LGPL-2.1 * First beta release for Horde 5. 2.0.0 1.0.0 stable stable 2012-10-30 LGPL-2.1 * First stable release for Horde 5. 2.0.1 1.0.0 stable stable 2012-11-19 LGPL-2.1 * [mms] Use new Horde_Test layout. 2.0.2 1.0.0 stable stable 2013-03-05 LGPL-2.1 * [jan] Improve dependency detection. 2.0.3 1.0.0 stable stable 2015-01-09 LGPL-2.1 * [jan] Improve PSR-2 compatibility. * [jan] Add Composer definition. 2.0.4 1.0.0 stable stable 2015-07-31 LGPL-2.1 * [jan] Fix Composer file. 2.0.5 1.0.0 stable stable 2016-02-02 LGPL-2.1 * [jan] Mark PHP 7 as supported. Horde_Serialize-2.0.5/doc/Horde/Serialize/examples/json.php0000664000175000017500000000075112654067664021752 0ustar janjan * @category Horde * @package Serialize */ class Horde_Serialize_Exception extends Horde_Exception_Wrapped { } Horde_Serialize-2.0.5/lib/Horde/Serialize.php0000664000175000017500000002556012654067664017171 0ustar janjan * @author Michael Slusarz * @package Serialize * @category Horde */ class Horde_Serialize { /* Type constants */ const UNKNOWN = -1; const NONE = 0; const WDDX = 1; const BZIP = 2; const IMAP8 = 3; const IMAPUTF7 = 4; const IMAPUTF8 = 5; const BASIC = 6; const GZ_DEFLATE = 7; const GZ_COMPRESS = 8; const GZ_ENCODE = 9; const BASE64 = 10; const RAW = 12; const URL = 13; const UTF7 = 14; const UTF7_BASIC = 15; const JSON = 16; const LZF = 17; /** * Serialize a value. * * See the list of constants at the top of the file for the serializing * techniques that can be used. * * @param mixed $data The data to be serialized. * @param mixed $mode The mode of serialization. Can be either a single * mode or array of modes. If array, will be * serialized in the order provided. * @param mixed $params Any additional parameters the serialization method * requires. * * @return string The serialized data. * @throws Horde_Serialize_Exception */ public static function serialize($data, $mode = array(self::BASIC), $params = null) { if (!is_array($mode)) { $mode = array($mode); } /* Parse through the list of serializing modes. */ foreach ($mode as $val) { /* Check to make sure the mode is supported. */ if (!self::hasCapability($val)) { throw new Horde_Serialize_Exception('Unsupported serialization type'); } $data = self::_serialize($data, $val, $params); } return $data; } /** * Unserialize a value. * * See the list of constants at the top of the file for the serializing * techniques that can be used. * * @param mixed $data The data to be unserialized. * @param mixed $mode The mode of unserialization. Can be either a * single mode or array of modes. If array, will be * unserialized in the order provided. * @param mixed $params Any additional parameters the unserialization * method requires. * * @return string The unserialized data. * @throws Horde_Serialize_Exception */ public static function unserialize($data, $mode = self::BASIC, $params = null) { if (!is_array($mode)) { $mode = array($mode); } /* Parse through the list of unserializing modes. */ foreach ($mode as $val) { /* Check to make sure the mode is supported. */ if (!self::hasCapability($val)) { throw new Horde_Serialize_Exception('Unsupported unserialization type'); } $data = self::_unserialize($data, $val, $params); } return $data; } /** * Check whether or not a serialization method is supported. * * @param integer $mode The serialization method. * * @return boolean True if supported, false if not. */ public static function hasCapability($mode) { switch ($mode) { case self::BZIP: return Horde_Util::extensionExists('bz2'); case self::WDDX: return Horde_Util::extensionExists('wddx'); case self::IMAPUTF7: return class_exists('Horde_Imap_Client'); case self::IMAPUTF8: return class_exists('Horde_Mime'); case self::GZ_DEFLATE: case self::GZ_COMPRESS: case self::GZ_ENCODE: return Horde_Util::extensionExists('zlib'); case self::LZF: return Horde_Util::extensionExists('lzf'); case self::NONE: case self::BASIC: case self::BASE64: case self::IMAP8: case self::RAW: case self::URL: case self::UTF7: case self::UTF7_BASIC: case self::JSON: return true; default: return false; } } /** * Serialize data. * * @param mixed $data The data to be serialized. * @param mixed $mode The mode of serialization. Can be * either a single mode or array of modes. * If array, will be serialized in the * order provided. * @param mixed $params Any additional parameters the serialization method * requires. * * @return string A serialized string. * @throws Horde_Serialize_Exception */ protected static function _serialize($data, $mode, $params = null) { switch ($mode) { case self::NONE: break; // $params['level'] = Level of compression (default: 3) // $params['workfactor'] = How does compression phase behave when given // worst case, highly repetitive, input data // (default: 30) case self::BZIP: $data = bzcompress($data, isset($params['level']) ? $params['level'] : 3, isset($params['workfactor']) ? $params['workfactor'] : 30); if (is_integer($data)) { $data = false; } break; case self::WDDX: $data = wddx_serialize_value($data); break; case self::IMAP8: $data = quoted_printable_encode($data); break; case self::IMAPUTF7: $data = Horde_Imap_Client_Utf7imap::Utf8ToUtf7Imap(Horde_String::convertCharset($data, 'ISO-8859-1', 'UTF-8')); break; case self::IMAPUTF8: $data = Horde_Mime::decode($data); break; // $params['level'] = Level of compression (default: 3) case self::GZ_DEFLATE: $data = gzdeflate($data, isset($params['level']) ? $params['level'] : 3); break; case self::BASIC: $data = serialize($data); break; // $params['level'] = Level of compression (default: 3) case self::GZ_COMPRESS: $data = gzcompress($data, isset($params['level']) ? $params['level'] : 3); break; case self::BASE64: $data = base64_encode($data); break; // $params['level'] = Level of compression (default: 3) case self::GZ_ENCODE: $data = gzencode($data, isset($params['level']) ? $params['level'] : 3); break; case self::RAW: $data = rawurlencode($data); break; case self::URL: $data = urlencode($data); break; // $params = Source character set case self::UTF7: $data = Horde_String::convertCharset($data, $params, 'UTF-7'); break; // $params = Source character set case self::UTF7_BASIC: $data = self::serialize($data, array(self::UTF7, self::BASIC), $params); break; case self::JSON: $tmp = json_encode($data); /* Basic error handling attempts. * TODO: JSON_ERROR_UTF8 = 5; available as of PHP 5.3.3 */ if (json_last_error() === 5) { $data = json_encode(Horde_String::convertCharset($data, $params, 'UTF-8', true)); } else { $data = $tmp; } break; case self::LZF: $data = lzf_compress($data); break; } if ($data === false) { throw new Horde_Serialize_Exception('Serialization failed.'); } return $data; } /** * Unserialize data. * * @param mixed $data The data to be unserialized. * @param mixed $mode The mode of unserialization. Can be either a * single mode or array of modes. If array, will be * unserialized in the order provided. * @param mixed $params Any additional parameters the unserialization * method requires. * * @return mixed Unserialized data. * @throws Horde_Serialize_Exception */ protected static function _unserialize(&$data, $mode, $params = null) { switch ($mode) { case self::NONE: break; case self::RAW: $data = rawurldecode($data); break; case self::URL: $data = urldecode($data); break; case self::WDDX: $data = wddx_deserialize($data); break; case self::BZIP: // $params['small'] = Use bzip2 'small memory' mode? $data = bzdecompress($data, isset($params['small']) ? $params['small'] : false); break; case self::IMAP8: $data = quoted_printable_decode($data); break; case self::IMAPUTF7: $data = Horde_String::convertCharset(Horde_Imap_Client_Utf7imap::Utf7ImapToUtf8($data), 'UTF-8', 'ISO-8859-1'); break; case self::IMAPUTF8: $data = Horde_Mime::encode($data); break; case self::BASIC: $data2 = @unserialize($data); // Unserialize can return false both on error and if $data is the // false value. if (($data2 === false) && ($data == serialize(false))) { return $data2; } $data = $data2; break; case self::GZ_DEFLATE: $data = gzinflate($data); break; case self::BASE64: $data = base64_decode($data); break; case self::GZ_COMPRESS: $data = gzuncompress($data); break; // $params = Output character set case self::UTF7: $data = Horde_String::convertCharset($data, 'utf-7', $params); break; // $params = Output character set case self::UTF7_BASIC: $data = self::unserialize($data, array(self::BASIC, self::UTF7), $params); break; case self::JSON: $out = json_decode($data); if (!is_null($out) || (strcasecmp($data, 'null') === 0)) { return $out; } break; case self::LZF: $data = @lzf_decompress($data); break; } if ($data === false) { throw new Horde_Serialize_Exception('Unserialization failed.'); } return $data; } } Horde_Serialize-2.0.5/test/Horde/Serialize/fixtures/badutf8.txt0000664000175000017500000000012212654067664022623 0ustar janjanNote: To play video messages sent to email, QuickTime® 6.5 or higher is required. Horde_Serialize-2.0.5/test/Horde/Serialize/AllTests.php0000664000175000017500000000013212654067664021121 0ustar janjanrun(); Horde_Serialize-2.0.5/test/Horde/Serialize/bootstrap.php0000664000175000017500000000014312654067664021405 0ustar janjan * @category Horde * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Serialize * @subpackage UnitTests */ class Horde_Serialize_JsonTest extends PHPUnit_Framework_TestCase { // JSON associative arrays tests. public function testJsonAssociativeArray() { // array case - strict: associative array with nested associative // arrays $arr = array( 'car1'=> array( 'color'=> 'tan', 'model' => 'sedan' ), 'car2' => array( 'color' => 'red', 'model' => 'sports' ) ); $this->assertEquals( '{"car1":{"color":"tan","model":"sedan"},"car2":{"color":"red","model":"sports"}}', Horde_Serialize::serialize($arr, Horde_Serialize::JSON) ); // array case - strict: associative array with nested associative // arrays, and some numeric keys thrown in // Should degrade to a numeric array. $arn = array( 0 => array( 0 => 'tan\\', 'model\\' => 'sedan' ), 1 => array( 0 => 'red', 'model' => 'sports' ) ); $arn_ja = '[{"0":"tan\\\\","model\\\\":"sedan"},{"0":"red","model":"sports"}]'; $this->assertEquals( $arn_ja, Horde_Serialize::serialize($arn, Horde_Serialize::JSON) ); $this->assertInternalType( 'array', Horde_Serialize::unserialize($arn_ja, Horde_Serialize::JSON) ); // sparse numeric assoc array: associative array numeric keys which // are not fully populated in a range of 0 to length-1 // Test a sparsely populated numerically indexed associative array. $arrs = array( 1 => 'one', 2 => 'two', 5 => 'five' ); $this->assertEquals( '{"1":"one","2":"two","5":"five"}', Horde_Serialize::serialize($arrs, Horde_Serialize::JSON) ); } // JSON empties tests. public function testJsonEmpties() { $obj0_j = '{}'; $obj1_j = '{ }'; $this->assertInternalType( 'object', Horde_Serialize::unserialize($obj0_j, Horde_Serialize::JSON) ); $this->assertEquals( '0', count(get_object_vars(Horde_Serialize::unserialize($obj0_j, Horde_Serialize::JSON))) ); $this->assertInternalType( 'object', Horde_Serialize::unserialize($obj1_j, Horde_Serialize::JSON) ); $this->assertEquals( '0', count(get_object_vars(Horde_Serialize::unserialize($obj1_j, Horde_Serialize::JSON))) ); } // JSON encode/decode tests (invalid UTF-8 input). public function testJsonInvalidUTF8Input() { $old_error_reporting = error_reporting(E_ALL & ~E_WARNING); $this->assertEquals( '"Note: To play video messages sent to email, QuickTime\u00ae 6.5 or higher is required.\n"', Horde_Serialize::serialize(file_get_contents(__DIR__ . '/fixtures/badutf8.txt'), Horde_Serialize::JSON, 'iso-8859-1') ); error_reporting($old_error_reporting); } // JSON encode/decode tests. public function testJsonEncodeAndDecode() { $obj = new stdClass(); $obj->a_string = '"he":llo}:{world'; $obj->an_array = array(1, 2, 3); $obj->obj = new stdClass(); $obj->obj->a_number = 123; $obj_j = '{"a_string":"\"he\":llo}:{world","an_array":[1,2,3],"obj":{"a_number":123}}'; $arr = array(null, true, array(1, 2, 3), "hello\"],[world!"); $arr_j = '[null,true,[1,2,3],"hello\"],[world!"]'; $str1 = 'hello world'; $str1_j = '"hello world"'; $str1_j_ = "'hello world'"; $str2 = "hello\t\"world\""; $str2_j = '"hello\\t\\"world\\""'; $str3 = "\\\r\n\t\"/"; $str3_j = '"\\\\\\r\\n\\t\\"\\/"'; $str4 = 'héllö wørÅ‚d'; $str4_j = '"h\u00e9ll\u00f6 w\u00f8r\u0142d"'; $str4_j_ = '"héllö wørÅ‚d"'; // type case: null $this->assertEquals( 'null', Horde_Serialize::serialize(null, Horde_Serialize::JSON) ); // type case: boolean true $this->assertEquals( 'true', Horde_Serialize::serialize(true, Horde_Serialize::JSON) ); // type case: boolean false $this->assertEquals( 'false', Horde_Serialize::serialize(false, Horde_Serialize::JSON) ); // numeric case: 1 $this->assertEquals( 1, Horde_Serialize::serialize(1, Horde_Serialize::JSON) ); // numeric case: -1 $this->assertEquals( -1, Horde_Serialize::serialize(-1, Horde_Serialize::JSON) ); // numeric case: 1.0 $this->assertEquals( 1, Horde_Serialize::serialize(1.0, Horde_Serialize::JSON) ); // numeric case: 1.1 $this->assertEquals( 1.1, Horde_Serialize::serialize(1.1, Horde_Serialize::JSON) ); // string case: hello world $this->assertEquals( $str1_j, Horde_Serialize::serialize($str1, Horde_Serialize::JSON) ); // string case: hello world, with tab, double-quotes $this->assertEquals( $str2_j, Horde_Serialize::serialize($str2, Horde_Serialize::JSON) ); // string case: backslash, return, newline, tab, double-quote $this->assertEquals( $str3_j, Horde_Serialize::serialize($str3, Horde_Serialize::JSON) ); // string case: hello world, with unicode $this->assertEquals( $str4_j, Horde_Serialize::serialize($str4, Horde_Serialize::JSON) ); // array case: array with elements and nested arrays $this->assertEquals( '[null,true,[1,2,3],"hello\"],[world!"]', Horde_Serialize::serialize($arr, Horde_Serialize::JSON) ); // object case: object with properties, nested object and arrays $this->assertEquals( '{"a_string":"\"he\":llo}:{world","an_array":[1,2,3],"obj":{"a_number":123}}', Horde_Serialize::serialize($obj, Horde_Serialize::JSON) ); // type case: null $this->assertNull( Horde_Serialize::unserialize('null', Horde_Serialize::JSON) ); // type case: boolean true $this->assertTrue( Horde_Serialize::unserialize('true', Horde_Serialize::JSON) ); // type case: boolean false $this->assertFalse( Horde_Serialize::unserialize('false', Horde_Serialize::JSON) ); // numeric case: 1 $this->assertEquals( 1, Horde_Serialize::unserialize('1', Horde_Serialize::JSON) ); // numeric case: -1 $this->assertEquals( -1, Horde_Serialize::unserialize('-1', Horde_Serialize::JSON) ); // numeric case: 1.0 $this->assertEquals( 1.0, Horde_Serialize::unserialize('1.0', Horde_Serialize::JSON) ); $this->assertInternalType( 'float', Horde_Serialize::unserialize('1.0', Horde_Serialize::JSON) ); // numeric case: 1.1 $this->assertEquals( 1.1, Horde_Serialize::unserialize('1.1', Horde_Serialize::JSON) ); $this->assertInternalType( 'float', Horde_Serialize::unserialize('1.1', Horde_Serialize::JSON) ); // string case: hello world $this->assertEquals( $str1, Horde_Serialize::unserialize($str1_j, Horde_Serialize::JSON) ); $this->assertEquals( "'" . $str1 . "'", Horde_Serialize::unserialize($str1_j_, Horde_Serialize::JSON) ); // string case: hello world, with tab, double-quotes $this->assertEquals( $str2, Horde_Serialize::unserialize($str2_j, Horde_Serialize::JSON) ); // string case: backslash, return, newline, tab, double-quote $this->assertEquals( $str3, Horde_Serialize::unserialize($str3_j, Horde_Serialize::JSON) ); // string case: hello world, with unicode $this->assertEquals( $str4, Horde_Serialize::unserialize($str4_j, Horde_Serialize::JSON) ); $this->assertEquals( $str4, Horde_Serialize::unserialize($str4_j_, Horde_Serialize::JSON) ); // array case: array with elements and nested arrays $this->assertEquals( $arr, Horde_Serialize::unserialize($arr_j, Horde_Serialize::JSON) ); // object case: object with properties, nested object and arrays $this->assertEquals( $obj, Horde_Serialize::unserialize($obj_j, Horde_Serialize::JSON) ); // type case: null $this->assertNull( Horde_Serialize::unserialize(Horde_Serialize::serialize(null, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // type case: boolean true $this->assertTrue( Horde_Serialize::unserialize(Horde_Serialize::serialize(true, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // type case: boolean false $this->assertFalse( Horde_Serialize::unserialize(Horde_Serialize::serialize(false, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // numeric case: 1 $this->assertEquals( 1, Horde_Serialize::unserialize(Horde_Serialize::serialize(1, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // numeric case: -1 $this->assertEquals( -1, Horde_Serialize::unserialize(Horde_Serialize::serialize(-1, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // numeric case: 1.0 $this->assertEquals( 1, Horde_Serialize::unserialize(Horde_Serialize::serialize(1.0, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // numeric case: 1.1 $this->assertEquals( 1.1, Horde_Serialize::unserialize(Horde_Serialize::serialize(1.1, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // string case: hello world $this->assertEquals( $str1, Horde_Serialize::unserialize(Horde_Serialize::serialize($str1, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // string case: hello world, with tab, double-quotes $this->assertEquals( $str2, Horde_Serialize::unserialize(Horde_Serialize::serialize($str2, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // string case: backslash, return, newline, tab, double-quote $this->assertEquals( $str3, Horde_Serialize::unserialize(Horde_Serialize::serialize($str3, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // string case: hello world, with unicode $this->assertEquals( $str4, Horde_Serialize::unserialize(Horde_Serialize::serialize($str4, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // array case: array with elements and nested arrays $this->assertEquals( $arr, Horde_Serialize::unserialize(Horde_Serialize::serialize($arr, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // object case: object with properties, nested object and arrays $this->assertEquals( $obj, Horde_Serialize::unserialize(Horde_Serialize::serialize($obj, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // type case: null $this->assertEquals( 'null', Horde_Serialize::serialize(Horde_Serialize::unserialize('null', Horde_Serialize::JSON), Horde_Serialize::JSON) ); // type case: boolean true $this->assertEquals( 'true', Horde_Serialize::serialize(Horde_Serialize::unserialize('true', Horde_Serialize::JSON), Horde_Serialize::JSON) ); // type case: boolean false $this->assertEquals( 'false', Horde_Serialize::serialize(Horde_Serialize::unserialize('false', Horde_Serialize::JSON), Horde_Serialize::JSON) ); // numeric case: 1 $this->assertEquals( '1', Horde_Serialize::serialize(Horde_Serialize::unserialize('1', Horde_Serialize::JSON), Horde_Serialize::JSON) ); // numeric case: -1 $this->assertEquals( '-1', Horde_Serialize::serialize(Horde_Serialize::unserialize('-1', Horde_Serialize::JSON), Horde_Serialize::JSON) ); // numeric case: 1.0 $this->assertEquals( '1.0', Horde_Serialize::serialize(Horde_Serialize::unserialize('1.0', Horde_Serialize::JSON), Horde_Serialize::JSON) ); // numeric case: 1.1 $this->assertEquals( '1.1', Horde_Serialize::serialize(Horde_Serialize::unserialize('1.1', Horde_Serialize::JSON), Horde_Serialize::JSON) ); // string case: hello world $this->assertEquals( $str1_j, Horde_Serialize::serialize(Horde_Serialize::unserialize($str1_j, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // string case: hello world, with tab, double-quotes $this->assertEquals( $str2_j, Horde_Serialize::serialize(Horde_Serialize::unserialize($str2_j, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // string case: backslash, return, newline, tab, double-quote $this->assertEquals( $str3_j, Horde_Serialize::serialize(Horde_Serialize::unserialize($str3_j, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // string case: hello world, with unicode $this->assertEquals( $str4_j, Horde_Serialize::serialize(Horde_Serialize::unserialize($str4_j, Horde_Serialize::JSON), Horde_Serialize::JSON) ); $this->assertEquals( $str4_j, Horde_Serialize::serialize(Horde_Serialize::unserialize($str4_j_, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // array case: array with elements and nested arrays $this->assertEquals( $arr_j, Horde_Serialize::serialize(Horde_Serialize::unserialize($arr_j, Horde_Serialize::JSON), Horde_Serialize::JSON) ); // object case: object with properties, nested object and arrays $this->assertEquals( $obj_j, Horde_Serialize::serialize(Horde_Serialize::unserialize($obj_j, Horde_Serialize::JSON), Horde_Serialize::JSON) ); } // JSON nested arrays tests. public function testJsonNestedArrays() { $str1 = '[{"this":"that"}]'; $str1_ob = new stdClass; $str1_ob->this = 'that'; $str2 = '{"this":["that"]}'; $str2_ob = new stdClass; $str2_ob->this = array('that'); $str3 = '{"params":[{"foo":["1"],"bar":"1"}]}'; $str3_ob = new stdClass; $str3_ob2 = new stdClass; $str3_ob2->foo = array(1); $str3_ob2->bar = 1; $str3_ob->params = array($str3_ob2); $str4 = '[{"foo": "bar", "baz": "winkle"}]'; $str4_ob2 = new stdClass; $str4_ob2->foo = 'bar'; $str4_ob2->baz = 'winkle'; $str4_ob = array($str4_ob2); $str5 = '{"params":[{"options": {"old": [ ], "new": [{"elements": {"old": [], "new": [{"elementName": "aa", "isDefault": false, "elementRank": "0", "priceAdjust": "0", "partNumber": ""}]}}], "optionName": "aa", "isRequired": false, "optionDesc": ""}}]}'; $str5_ob = new stdClass; $str5_ob->params = array(new stdClass); $str5_ob->params[0]->options = new stdClass; $str5_ob->params[0]->options->old = array(); $str5_ob->params[0]->options->new = array(new stdClass); $str5_ob->params[0]->options->new[0]->elements = new stdClass; $str5_ob->params[0]->options->new[0]->elements->old = array(); $str5_ob->params[0]->options->new[0]->elements->new = array(new stdClass); $str5_ob->params[0]->options->new[0]->elements->new[0]->elementName = 'aa'; $str5_ob->params[0]->options->new[0]->elements->new[0]->isDefault = false; $str5_ob->params[0]->options->new[0]->elements->new[0]->elementRank = 0; $str5_ob->params[0]->options->new[0]->elements->new[0]->priceAdjust = 0; $str5_ob->params[0]->options->new[0]->elements->new[0]->partNumber = ''; $str5_ob->params[0]->options->optionName = 'aa'; $str5_ob->params[0]->options->isRequired = false; $str5_ob->params[0]->options->optionDesc = ''; // simple compactly-nested array $this->assertEquals( array($str1_ob), Horde_Serialize::unserialize($str1, Horde_Serialize::JSON) ); // simple compactly-nested array $this->assertEquals( $str2_ob, Horde_Serialize::unserialize($str2, Horde_Serialize::JSON) ); // complex compactly nested array $this->assertEquals( $str3_ob, Horde_Serialize::unserialize($str3, Horde_Serialize::JSON) ); // complex compactly nested array $this->assertEquals( $str4_ob, Horde_Serialize::unserialize($str4, Horde_Serialize::JSON) ); // super complex compactly nested array $this->assertEquals( $str5_ob, Horde_Serialize::unserialize($str5, Horde_Serialize::JSON) ); } // JSON objects tests. public function testJsonObjects() { $obj_j = '{"a_string":"\"he\":llo}:{world","an_array":[1,2,3],"obj":{"a_number":123}}'; $obj1 = new stdClass; $obj1->car1 = new stdClass; $obj1->car1->color = 'tan'; $obj1->car1->model = 'sedan'; $obj1->car2 = new stdClass; $obj1->car2->color = 'red'; $obj1->car2->model = 'sports'; $obj1_j = '{"car1":{"color":"tan","model":"sedan"},"car2":{"color":"red","model":"sports"}}'; $this->assertInternalType( 'object', Horde_Serialize::unserialize($obj_j, Horde_Serialize::JSON) ); // object - strict: Object with nested objects $this->assertEquals( $obj1_j, Horde_Serialize::serialize($obj1, Horde_Serialize::JSON) ); // object case $this->assertEquals( $obj_j, Horde_Serialize::serialize(Horde_Serialize::unserialize($obj_j, Horde_Serialize::JSON), Horde_Serialize::JSON) ); } // JSON spaces tests. public function testJsonSpaces() { $obj = new stdClass; $obj->a_string = "\"he\":llo}:{world"; $obj->an_array = array(1, 2, 3); $obj->obj = new stdClass; $obj->obj->a_number = 123; $obj_js = '{"a_string": "\"he\":llo}:{world", "an_array":[1, 2, 3], "obj": {"a_number":123}}'; // checking whether notation with spaces works $this->assertEquals( $obj, Horde_Serialize::unserialize($obj_js, Horde_Serialize::JSON) ); } // JSON unquoted keys tests. public function testJsonUnquotedKeys() { $ob1 = new stdClass; $ob1->{'0'} = 'tan'; $ob1->model = 'sedan'; $ob2 = new stdClass; $ob2->{'0'} = 'red'; $ob2->model = 'sports'; $arn = array($ob1, $ob2); $arn_ja = '[{"0":"tan","model":"sedan"},{"0":"red","model":"sports"}]'; $arrs = new stdClass; $arrs->{'1'} = 'one'; $arrs->{'2'} = 'two'; $arrs->{'5'} = 'fi"ve'; $arrs_jo = '{"1":"one","2":"two","5":"fi\"ve"}'; // array case - strict: associative array with unquoted keys, nested // associative arrays, and some numeric keys thrown in // ...unless the input array has some numeric indices, in which case // the behavior is to degrade to a regular array $this->assertEquals( $arn_ja, Horde_Serialize::serialize($arn, Horde_Serialize::JSON) ); // sparse numeric assoc array: associative array with unquoted keys, // single-quoted values, numeric keys which are not fully populated in // a range of 0 to length-1 // Test a sparsely populated numerically indexed associative array $this->assertEquals( $arrs_jo, Horde_Serialize::serialize($arrs, Horde_Serialize::JSON) ); } } Horde_Serialize-2.0.5/test/Horde/Serialize/phpunit.xml0000664000175000017500000000005612654067664021073 0ustar janjan