2
0

JsonTest.php 24 KB

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