JsonTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Json
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../TestHelper.php';
  26. /**
  27. * @see Zend_Json
  28. */
  29. require_once 'Zend/Json.php';
  30. /**
  31. * @see Zend_Json_Expr
  32. */
  33. require_once 'Zend/Json/Expr.php';
  34. /**
  35. * @see Zend_Json_Encoder
  36. */
  37. require_once 'Zend/Json/Encoder.php';
  38. /**
  39. * @see Zend_Json_Decoder
  40. */
  41. require_once 'Zend/Json/Decoder.php';
  42. /**
  43. * @category Zend
  44. * @package Zend_Json
  45. * @subpackage UnitTests
  46. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  47. * @license http://framework.zend.com/license/new-bsd New BSD License
  48. * @group Zend_Json
  49. */
  50. class Zend_JsonTest extends PHPUnit_Framework_TestCase
  51. {
  52. private $_originalUseBuiltinEncoderDecoderValue;
  53. public function setUp()
  54. {
  55. $this->_originalUseBuiltinEncoderDecoderValue = Zend_Json::$useBuiltinEncoderDecoder;
  56. }
  57. public function tearDown()
  58. {
  59. Zend_Json::$useBuiltinEncoderDecoder = $this->_originalUseBuiltinEncoderDecoderValue;
  60. }
  61. public function testJsonWithPhpJsonExtension()
  62. {
  63. if (!extension_loaded('json')) {
  64. $this->markTestSkipped('JSON extension is not loaded');
  65. }
  66. Zend_Json::$useBuiltinEncoderDecoder = false;
  67. $this->_testJson(array('string', 327, true, null));
  68. }
  69. public function testJsonWithBuiltins()
  70. {
  71. Zend_Json::$useBuiltinEncoderDecoder = true;
  72. $this->_testJson(array('string', 327, true, null));
  73. }
  74. /**
  75. * Test encoding and decoding in a single step
  76. * @param array $values array of values to test against encode/decode
  77. */
  78. protected function _testJson($values)
  79. {
  80. $encoded = Zend_Json::encode($values);
  81. $this->assertEquals($values, Zend_Json::decode($encoded));
  82. }
  83. /**
  84. * test null encoding/decoding
  85. */
  86. public function testNull()
  87. {
  88. $this->_testEncodeDecode(array(null));
  89. }
  90. /**
  91. * test boolean encoding/decoding
  92. */
  93. public function testBoolean()
  94. {
  95. $this->assertTrue(Zend_Json_Decoder::decode(Zend_Json_Encoder::encode(true)));
  96. $this->assertFalse(Zend_Json_Decoder::decode(Zend_Json_Encoder::encode(false)));
  97. }
  98. /**
  99. * test integer encoding/decoding
  100. */
  101. public function testInteger()
  102. {
  103. $this->_testEncodeDecode(array(-2));
  104. $this->_testEncodeDecode(array(-1));
  105. $zero = Zend_Json_Decoder::decode(Zend_Json_Encoder::encode(0));
  106. $this->assertEquals(0, $zero, 'Failed 0 integer test. Encoded: ' . serialize(Zend_Json_Encoder::encode(0)));
  107. }
  108. /**
  109. * test float encoding/decoding
  110. */
  111. public function testFloat()
  112. {
  113. $this->_testEncodeDecode(array(-2.1, 1.2));
  114. }
  115. /**
  116. * test string encoding/decoding
  117. */
  118. public function testString()
  119. {
  120. $this->_testEncodeDecode(array('string'));
  121. $this->assertEquals('', Zend_Json_Decoder::decode(Zend_Json_Encoder::encode('')), 'Empty string encoded: ' . serialize(Zend_Json_Encoder::encode('')));
  122. }
  123. /**
  124. * Test backslash escaping of string
  125. */
  126. public function testString2()
  127. {
  128. $string = 'INFO: Path \\\\test\\123\\abc';
  129. $expected = '"INFO: Path \\\\\\\\test\\\\123\\\\abc"';
  130. $encoded = Zend_Json_Encoder::encode($string);
  131. $this->assertEquals($expected, $encoded, 'Backslash encoding incorrect: expected: ' . serialize($expected) . '; received: ' . serialize($encoded) . "\n");
  132. $this->assertEquals($string, Zend_Json_Decoder::decode($encoded));
  133. }
  134. /**
  135. * Test newline escaping of string
  136. */
  137. public function testString3()
  138. {
  139. $expected = '"INFO: Path\nSome more"';
  140. $string = "INFO: Path\nSome more";
  141. $encoded = Zend_Json_Encoder::encode($string);
  142. $this->assertEquals($expected, $encoded, 'Newline encoding incorrect: expected ' . serialize($expected) . '; received: ' . serialize($encoded) . "\n");
  143. $this->assertEquals($string, Zend_Json_Decoder::decode($encoded));
  144. }
  145. /**
  146. * Test tab/non-tab escaping of string
  147. */
  148. public function testString4()
  149. {
  150. $expected = '"INFO: Path\\t\\\\tSome more"';
  151. $string = "INFO: Path\t\\tSome more";
  152. $encoded = Zend_Json_Encoder::encode($string);
  153. $this->assertEquals($expected, $encoded, 'Tab encoding incorrect: expected ' . serialize($expected) . '; received: ' . serialize($encoded) . "\n");
  154. $this->assertEquals($string, Zend_Json_Decoder::decode($encoded));
  155. }
  156. /**
  157. * Test double-quote escaping of string
  158. */
  159. public function testString5()
  160. {
  161. $expected = '"INFO: Path \"Some more\""';
  162. $string = 'INFO: Path "Some more"';
  163. $encoded = Zend_Json_Encoder::encode($string);
  164. $this->assertEquals($expected, $encoded, 'Quote encoding incorrect: expected ' . serialize($expected) . '; received: ' . serialize($encoded) . "\n");
  165. $this->assertEquals($string, Zend_Json_Decoder::decode($encoded));
  166. }
  167. /**
  168. * test indexed array encoding/decoding
  169. */
  170. public function testArray()
  171. {
  172. $array = array(1, 'one', 2, 'two');
  173. $encoded = Zend_Json_Encoder::encode($array);
  174. $this->assertSame($array, Zend_Json_Decoder::decode($encoded), 'Decoded array does not match: ' . serialize($encoded));
  175. }
  176. /**
  177. * test associative array encoding/decoding
  178. */
  179. public function testAssocArray()
  180. {
  181. $this->_testEncodeDecode(array(array('one' => 1, 'two' => 2)));
  182. }
  183. /**
  184. * test associative array encoding/decoding, with mixed key types
  185. */
  186. public function testAssocArray2()
  187. {
  188. $this->_testEncodeDecode(array(array('one' => 1, 2 => 2)));
  189. }
  190. /**
  191. * test associative array encoding/decoding, with integer keys not starting at 0
  192. */
  193. public function testAssocArray3()
  194. {
  195. $this->_testEncodeDecode(array(array(1 => 'one', 2 => 'two')));
  196. }
  197. /**
  198. * test object encoding/decoding (decoding to array)
  199. */
  200. public function testObject()
  201. {
  202. $value = new stdClass();
  203. $value->one = 1;
  204. $value->two = 2;
  205. $array = array('__className' => 'stdClass', 'one' => 1, 'two' => 2);
  206. $encoded = Zend_Json_Encoder::encode($value);
  207. $this->assertSame($array, Zend_Json_Decoder::decode($encoded));
  208. }
  209. /**
  210. * test object encoding/decoding (decoding to stdClass)
  211. */
  212. public function testObjectAsObject()
  213. {
  214. $value = new stdClass();
  215. $value->one = 1;
  216. $value->two = 2;
  217. $encoded = Zend_Json_Encoder::encode($value);
  218. $decoded = Zend_Json_Decoder::decode($encoded, Zend_Json::TYPE_OBJECT);
  219. $this->assertTrue(is_object($decoded), 'Not decoded as an object');
  220. $this->assertTrue($decoded instanceof StdClass, 'Not a StdClass object');
  221. $this->assertTrue(isset($decoded->one), 'Expected property not set');
  222. $this->assertEquals($value->one, $decoded->one, 'Unexpected value');
  223. }
  224. /**
  225. * Test that arrays of objects decode properly; see issue #144
  226. */
  227. public function testDecodeArrayOfObjects()
  228. {
  229. $value = '[{"id":1},{"foo":2}]';
  230. $expect = array(array('id' => 1), array('foo' => 2));
  231. $this->assertEquals($expect, Zend_Json_Decoder::decode($value));
  232. }
  233. /**
  234. * Test that objects of arrays decode properly; see issue #107
  235. */
  236. public function testDecodeObjectOfArrays()
  237. {
  238. $value = '{"codeDbVar" : {"age" : ["int", 5], "prenom" : ["varchar", 50]}, "234" : [22, "jb"], "346" : [64, "francois"], "21" : [12, "paul"]}';
  239. $expect = array(
  240. 'codeDbVar' => array(
  241. 'age' => array('int', 5),
  242. 'prenom' => array('varchar', 50),
  243. ),
  244. 234 => array(22, 'jb'),
  245. 346 => array(64, 'francois'),
  246. 21 => array(12, 'paul')
  247. );
  248. $this->assertEquals($expect, Zend_Json_Decoder::decode($value));
  249. }
  250. /**
  251. * Test encoding and decoding in a single step
  252. * @param array $values array of values to test against encode/decode
  253. */
  254. protected function _testEncodeDecode($values)
  255. {
  256. foreach ($values as $value) {
  257. $encoded = Zend_Json_Encoder::encode($value);
  258. $this->assertEquals($value, Zend_Json_Decoder::decode($encoded));
  259. }
  260. }
  261. /**
  262. * Test that version numbers such as 4.10 are encoded and decoded properly;
  263. * See ZF-377
  264. */
  265. public function testEncodeReleaseNumber()
  266. {
  267. $value = '4.10';
  268. $this->_testEncodeDecode(array($value));
  269. }
  270. /**
  271. * Tests that spaces/linebreaks prior to a closing right bracket don't throw
  272. * exceptions. See ZF-283.
  273. */
  274. public function testEarlyLineBreak()
  275. {
  276. $expected = array('data' => array(1, 2, 3, 4));
  277. $json = '{"data":[1,2,3,4' . "\n]}";
  278. $this->assertEquals($expected, Zend_Json_Decoder::decode($json));
  279. $json = '{"data":[1,2,3,4 ]}';
  280. $this->assertEquals($expected, Zend_Json_Decoder::decode($json));
  281. }
  282. /**
  283. * Tests for ZF-504
  284. *
  285. * Three confirmed issues reported:
  286. * - encoder improperly encoding empty arrays as structs
  287. * - decoder happily decoding clearly borked JSON
  288. * - decoder decoding octal values improperly (shouldn't decode them at all, as JSON does not support them)
  289. */
  290. public function testZf504()
  291. {
  292. $test = array();
  293. $this->assertSame('[]', Zend_Json_Encoder::encode($test));
  294. try {
  295. $json = '[a"],["a],[][]';
  296. $test = Zend_Json_Decoder::decode($json);
  297. $this->fail("Should not be able to decode '$json'");
  298. $json = '[a"],["a]';
  299. $test = Zend_Json_Decoder::decode($json);
  300. $this->fail("Should not be able to decode '$json'");
  301. } catch (Exception $e) {
  302. // success
  303. }
  304. try {
  305. $expected = 010;
  306. $test = Zend_Json_Decoder::decode('010');
  307. $this->fail('Octal values are not supported in JSON notation');
  308. } catch (Exception $e) {
  309. // sucess
  310. }
  311. }
  312. /**
  313. * Tests for ZF-461
  314. *
  315. * Check to see that cycling detection works properly
  316. */
  317. public function testZf461()
  318. {
  319. $item1 = new Zend_JsonTest_Item() ;
  320. $item2 = new Zend_JsonTest_Item() ;
  321. $everything = array() ;
  322. $everything['allItems'] = array($item1, $item2) ;
  323. $everything['currentItem'] = $item1 ;
  324. try {
  325. $encoded = Zend_Json_Encoder::encode($everything);
  326. } catch (Exception $e) {
  327. $this->fail('Object cycling checks should check for recursion, not duplicate usage of an item');
  328. }
  329. try {
  330. $encoded = Zend_Json_Encoder::encode($everything, true);
  331. $this->fail('Object cycling not allowed when cycleCheck parameter is true');
  332. } catch (Exception $e) {
  333. // success
  334. }
  335. }
  336. /**
  337. * Test for ZF-4053
  338. *
  339. * Check to see that cyclical exceptions are silenced when
  340. * $option['silenceCyclicalExceptions'] = true is used
  341. */
  342. public function testZf4053()
  343. {
  344. $item1 = new Zend_JsonTest_Item() ;
  345. $item2 = new Zend_JsonTest_Item() ;
  346. $everything = array() ;
  347. $everything['allItems'] = array($item1, $item2) ;
  348. $everything['currentItem'] = $item1 ;
  349. $options = array('silenceCyclicalExceptions'=>true);
  350. Zend_Json::$useBuiltinEncoderDecoder = true;
  351. $encoded = Zend_Json::encode($everything, true, $options);
  352. $json = '{"allItems":[{"__className":"Zend_JsonTest_Item"},{"__className":"Zend_JsonTest_Item"}],"currentItem":"* RECURSION (Zend_JsonTest_Item) *"}';
  353. $this->assertEquals($encoded,$json);
  354. }
  355. public function testEncodeObject()
  356. {
  357. $actual = new Zend_JsonTest_Object();
  358. $encoded = Zend_Json_Encoder::encode($actual);
  359. $decoded = Zend_Json_Decoder::decode($encoded, Zend_Json::TYPE_OBJECT);
  360. $this->assertTrue(isset($decoded->__className));
  361. $this->assertEquals('Zend_JsonTest_Object', $decoded->__className);
  362. $this->assertTrue(isset($decoded->foo));
  363. $this->assertEquals('bar', $decoded->foo);
  364. $this->assertTrue(isset($decoded->bar));
  365. $this->assertEquals('baz', $decoded->bar);
  366. $this->assertFalse(isset($decoded->_foo));
  367. }
  368. public function testEncodeClass()
  369. {
  370. $encoded = Zend_Json_Encoder::encodeClass('Zend_JsonTest_Object');
  371. $this->assertContains("Class.create('Zend_JsonTest_Object'", $encoded);
  372. $this->assertContains("ZAjaxEngine.invokeRemoteMethod(this, 'foo'", $encoded);
  373. $this->assertContains("ZAjaxEngine.invokeRemoteMethod(this, 'bar'", $encoded);
  374. $this->assertNotContains("ZAjaxEngine.invokeRemoteMethod(this, 'baz'", $encoded);
  375. $this->assertContains('variables:{foo:"bar",bar:"baz"}', $encoded);
  376. $this->assertContains('constants : {FOO: "bar"}', $encoded);
  377. }
  378. public function testEncodeClasses()
  379. {
  380. $encoded = Zend_Json_Encoder::encodeClasses(array('Zend_JsonTest_Object', 'Zend_JsonTest'));
  381. $this->assertContains("Class.create('Zend_JsonTest_Object'", $encoded);
  382. $this->assertContains("Class.create('Zend_JsonTest'", $encoded);
  383. }
  384. public function testToJsonSerialization()
  385. {
  386. $toJsonObject = new ToJsonClass();
  387. $result = Zend_Json::encode($toJsonObject);
  388. $this->assertEquals('{"firstName":"John","lastName":"Doe","email":"john@doe.com"}', $result);
  389. }
  390. /**
  391. * test encoding array with Zend_Json_Expr
  392. *
  393. * @group ZF-4946
  394. */
  395. public function testEncodingArrayWithExpr()
  396. {
  397. $expr = new Zend_Json_Expr('window.alert("Zend Json Expr")');
  398. $array = array('expr'=>$expr, 'int'=>9, 'string'=>'text');
  399. $result = Zend_Json::encode($array, false, array('enableJsonExprFinder' => true));
  400. $expected = '{"expr":window.alert("Zend Json Expr"),"int":9,"string":"text"}';
  401. $this->assertEquals($expected, $result);
  402. }
  403. /**
  404. * test encoding object with Zend_Json_Expr
  405. *
  406. * @group ZF-4946
  407. */
  408. public function testEncodingObjectWithExprAndInternalEncoder()
  409. {
  410. Zend_Json::$useBuiltinEncoderDecoder = true;
  411. $expr = new Zend_Json_Expr('window.alert("Zend Json Expr")');
  412. $obj = new stdClass();
  413. $obj->expr = $expr;
  414. $obj->int = 9;
  415. $obj->string = 'text';
  416. $result = Zend_Json::encode($obj, false, array('enableJsonExprFinder' => true));
  417. $expected = '{"__className":"stdClass","expr":window.alert("Zend Json Expr"),"int":9,"string":"text"}';
  418. $this->assertEquals($expected, $result);
  419. }
  420. /**
  421. * test encoding object with Zend_Json_Expr
  422. *
  423. * @group ZF-4946
  424. */
  425. public function testEncodingObjectWithExprAndExtJson()
  426. {
  427. if(!function_exists('json_encode')) {
  428. $this->markTestSkipped('Test only works with ext/json enabled!');
  429. }
  430. Zend_Json::$useBuiltinEncoderDecoder = false;
  431. $expr = new Zend_Json_Expr('window.alert("Zend Json Expr")');
  432. $obj = new stdClass();
  433. $obj->expr = $expr;
  434. $obj->int = 9;
  435. $obj->string = 'text';
  436. $result = Zend_Json::encode($obj, false, array('enableJsonExprFinder' => true));
  437. $expected = '{"expr":window.alert("Zend Json Expr"),"int":9,"string":"text"}';
  438. $this->assertEquals($expected, $result);
  439. }
  440. /**
  441. * test encoding object with ToJson and Zend_Json_Expr
  442. *
  443. * @group ZF-4946
  444. */
  445. public function testToJsonWithExpr()
  446. {
  447. Zend_Json::$useBuiltinEncoderDecoder = true;
  448. $obj = new Zend_Json_ToJsonWithExpr();
  449. $result = Zend_Json::encode($obj, false, array('enableJsonExprFinder' => true));
  450. $expected = '{"expr":window.alert("Zend Json Expr"),"int":9,"string":"text"}';
  451. $this->assertEquals($expected, $result);
  452. }
  453. /**
  454. * Regression tests for Zend_Json_Expr and mutliple keys with the same name.
  455. *
  456. * @group ZF-4946
  457. */
  458. public function testEncodingMultipleNestedSwitchingSameNameKeysWithDifferentJsonExprSettings()
  459. {
  460. $data = array(
  461. 0 => array(
  462. "alpha" => new Zend_Json_Expr("function(){}"),
  463. "beta" => "gamma",
  464. ),
  465. 1 => array(
  466. "alpha" => "gamma",
  467. "beta" => new Zend_Json_Expr("function(){}"),
  468. ),
  469. 2 => array(
  470. "alpha" => "gamma",
  471. "beta" => "gamma",
  472. )
  473. );
  474. $result = Zend_Json::encode($data, false, array('enableJsonExprFinder' => true));
  475. $this->assertEquals(
  476. '[{"alpha":function(){},"beta":"gamma"},{"alpha":"gamma","beta":function(){}},{"alpha":"gamma","beta":"gamma"}]',
  477. $result
  478. );
  479. }
  480. /**
  481. * Regression tests for Zend_Json_Expr and mutliple keys with the same name.
  482. *
  483. * @group ZF-4946
  484. */
  485. public function testEncodingMultipleNestedIteratedSameNameKeysWithDifferentJsonExprSettings()
  486. {
  487. $data = array(
  488. 0 => array(
  489. "alpha" => "alpha"
  490. ),
  491. 1 => array(
  492. "alpha" => "beta",
  493. ),
  494. 2 => array(
  495. "alpha" => new Zend_Json_Expr("gamma"),
  496. ),
  497. 3 => array(
  498. "alpha" => "delta",
  499. ),
  500. 4 => array(
  501. "alpha" => new Zend_Json_Expr("epsilon"),
  502. )
  503. );
  504. $result = Zend_Json::encode($data, false, array('enableJsonExprFinder' => true));
  505. $this->assertEquals('[{"alpha":"alpha"},{"alpha":"beta"},{"alpha":gamma},{"alpha":"delta"},{"alpha":epsilon}]', $result);
  506. }
  507. public function testDisabledJsonExprFinder()
  508. {
  509. Zend_Json::$useBuiltinEncoderDecoder = true;
  510. $data = array(
  511. 0 => array(
  512. "alpha" => new Zend_Json_Expr("function(){}"),
  513. "beta" => "gamma",
  514. ),
  515. );
  516. $result = Zend_Json::encode($data);
  517. $this->assertEquals(
  518. '[{"alpha":{"__className":"Zend_Json_Expr"},"beta":"gamma"}]',
  519. $result
  520. );
  521. }
  522. /**
  523. * @group ZF-4054
  524. */
  525. public function testEncodeWithUtf8IsTransformedToPackedSyntax()
  526. {
  527. $data = array("Отмена");
  528. $result = Zend_Json_Encoder::encode($data);
  529. $this->assertEquals('["\u041e\u0442\u043c\u0435\u043d\u0430"]', $result);
  530. }
  531. /**
  532. * @group ZF-4054
  533. *
  534. * This test contains assertions from the Solar Framework by Paul M. Jones
  535. * @link http://solarphp.com
  536. */
  537. public function testEncodeWithUtf8IsTransformedSolarRegression()
  538. {
  539. $expect = '"h\u00c3\u00a9ll\u00c3\u00b6 w\u00c3\u00b8r\u00c5\u201ad"';
  540. $this->assertEquals($expect, Zend_Json_Encoder::encode('héllö wørłd'));
  541. $this->assertEquals('héllö wørłd', Zend_Json_Decoder::decode($expect));
  542. $expect = '"\u0440\u0443\u0441\u0441\u0438\u0448"';
  543. $this->assertEquals($expect, Zend_Json_Encoder::encode("руссиш"));
  544. $this->assertEquals("руссиш", Zend_Json_Decoder::decode($expect));
  545. }
  546. /**
  547. * @group ZF-4054
  548. */
  549. public function testEncodeUnicodeStringSolarRegression()
  550. {
  551. $value = 'héllö wørłd';
  552. $expected = 'h\u00c3\u00a9ll\u00c3\u00b6 w\u00c3\u00b8r\u00c5\u201ad';
  553. $this->assertEquals($expected, Zend_Json_Encoder::encodeUnicodeString($value));
  554. $value = "\xC3\xA4";
  555. $expected = '\u00e4';
  556. $this->assertEquals($expected, Zend_Json_Encoder::encodeUnicodeString($value));
  557. $value = "\xE1\x82\xA0\xE1\x82\xA8";
  558. $expected = '\u10a0\u10a8';
  559. $this->assertEquals($expected, Zend_Json_Encoder::encodeUnicodeString($value));
  560. }
  561. /**
  562. * @group ZF-4054
  563. */
  564. public function testDecodeUnicodeStringSolarRegression()
  565. {
  566. $expected = 'héllö wørłd';
  567. $value = 'h\u00c3\u00a9ll\u00c3\u00b6 w\u00c3\u00b8r\u00c5\u201ad';
  568. $this->assertEquals($expected, Zend_Json_Decoder::decodeUnicodeString($value));
  569. $expected = "\xC3\xA4";
  570. $value = '\u00e4';
  571. $this->assertEquals($expected, Zend_Json_Decoder::decodeUnicodeString($value));
  572. $value = '\u10a0';
  573. $expected = "\xE1\x82\xA0";
  574. $this->assertEquals($expected, Zend_Json_Decoder::decodeUnicodeString($value));
  575. }
  576. /**
  577. * @group ZF-4054
  578. *
  579. * This test contains assertions from the Solar Framework by Paul M. Jones
  580. * @link http://solarphp.com
  581. */
  582. public function testEncodeWithUtf8IsTransformedSolarRegressionEqualsJsonExt()
  583. {
  584. if(function_exists('json_encode') == false) {
  585. $this->markTestSkipped('Test can only be run, when ext/json is installed.');
  586. }
  587. $this->assertEquals(
  588. json_encode('héllö wørłd'),
  589. Zend_Json_Encoder::encode('héllö wørłd')
  590. );
  591. $this->assertEquals(
  592. json_encode("руссиш"),
  593. Zend_Json_Encoder::encode("руссиш")
  594. );
  595. }
  596. /**
  597. * @group ZF-4946
  598. */
  599. public function testUtf8JsonExprFinder()
  600. {
  601. $data = array("Отмена" => new Zend_Json_Expr("foo"));
  602. Zend_Json::$useBuiltinEncoderDecoder = true;
  603. $result = Zend_Json::encode($data, false, array('enableJsonExprFinder' => true));
  604. $this->assertEquals('{"\u041e\u0442\u043c\u0435\u043d\u0430":foo}', $result);
  605. Zend_Json::$useBuiltinEncoderDecoder = false;
  606. $result = Zend_Json::encode($data, false, array('enableJsonExprFinder' => true));
  607. $this->assertEquals('{"\u041e\u0442\u043c\u0435\u043d\u0430":foo}', $result);
  608. }
  609. /**
  610. * @group ZF-4437
  611. */
  612. public function testKommaDecimalIsConvertedToCorrectJsonWithDot()
  613. {
  614. $localeInfo = localeconv();
  615. if($localeInfo['decimal_point'] != ",") {
  616. $this->markTestSkipped("This test only works for platforms where , is the decimal point separator.");
  617. }
  618. Zend_Json::$useBuiltinEncoderDecoder = true;
  619. $this->assertEquals("[1.20, 1.68]", Zend_Json_Encoder::encode(array(
  620. (float)"1,20", (float)"1,68"
  621. )));
  622. }
  623. public function testEncodeObjectImplementingIterator()
  624. {
  625. $this->markTestIncomplete('Test is not yet finished.');
  626. }
  627. /**
  628. * @group ZF-8663
  629. */
  630. public function testNativeJsonEncoderWillProperlyEncodeSolidusInStringValues()
  631. {
  632. $source = "</foo><foo>bar</foo>";
  633. $target = '"<\\/foo><foo>bar<\\/foo>"';
  634. // first test ext/json
  635. Zend_Json::$useBuiltinEncoderDecoder = false;
  636. $this->assertEquals($target, Zend_Json::encode($source));
  637. }
  638. /**
  639. * @group ZF-8663
  640. */
  641. public function testBuiltinJsonEncoderWillProperlyEncodeSolidusInStringValues()
  642. {
  643. $source = "</foo><foo>bar</foo>";
  644. $target = '"<\\/foo><foo>bar<\\/foo>"';
  645. // first test ext/json
  646. Zend_Json::$useBuiltinEncoderDecoder = true;
  647. $this->assertEquals($target, Zend_Json::encode($source));
  648. }
  649. /**
  650. * @group ZF-8918
  651. * @expectedException Zend_Json_Exception
  652. */
  653. public function testDecodingInvalidJsonShouldRaiseAnException()
  654. {
  655. Zend_Json::decode(' some string ');
  656. }
  657. /**
  658. * @group ZF-9416
  659. * Encoding an iterator using the internal encoder should handle undefined keys
  660. */
  661. public function testIteratorWithoutDefinedKey()
  662. {
  663. $inputValue = new ArrayIterator(array('foo'));
  664. $encoded = Zend_Json_Encoder::encode($inputValue);
  665. $expectedDecoding = '{"__className":"ArrayIterator","0":"foo"}';
  666. $this->assertEquals($encoded, $expectedDecoding);
  667. }
  668. }
  669. /**
  670. * Zend_JsonTest_Item: test item for use with testZf461()
  671. */
  672. class Zend_JsonTest_Item
  673. {
  674. }
  675. /**
  676. * Zend_JsonTest_Object: test class for encoding classes
  677. */
  678. class Zend_JsonTest_Object
  679. {
  680. const FOO = 'bar';
  681. public $foo = 'bar';
  682. public $bar = 'baz';
  683. protected $_foo = 'fooled you';
  684. public function foo($bar, $baz)
  685. {
  686. }
  687. public function bar($baz)
  688. {
  689. }
  690. protected function baz()
  691. {
  692. }
  693. }
  694. class ToJsonClass
  695. {
  696. private $_firstName = 'John';
  697. private $_lastName = 'Doe';
  698. private $_email = 'john@doe.com';
  699. public function toJson()
  700. {
  701. $data = array(
  702. 'firstName' => $this->_firstName,
  703. 'lastName' => $this->_lastName,
  704. 'email' => $this->_email
  705. );
  706. return Zend_Json::encode($data);
  707. }
  708. }
  709. /**
  710. * ISSUE ZF-4946
  711. *
  712. */
  713. class Zend_Json_ToJsonWithExpr
  714. {
  715. private $_string = 'text';
  716. private $_int = 9;
  717. private $_expr = 'window.alert("Zend Json Expr")';
  718. public function toJson()
  719. {
  720. $data = array(
  721. 'expr' => new Zend_Json_Expr($this->_expr),
  722. 'int' => $this->_int,
  723. 'string' => $this->_string
  724. );
  725. return Zend_Json::encode($data, false, array('enableJsonExprFinder' => true));
  726. }
  727. }