Browse Source

Merge pull request #10281 from LAV45/fix_json_encode

Unexpected behavior for \SimpleXMLElement and Json::encode() #10278
tags/2.0.7
Dmitry Naumenko 9 years ago
parent
commit
95edff3b1d
  1. 1
      framework/CHANGELOG.md
  2. 2
      framework/helpers/BaseJson.php
  3. 19
      tests/framework/helpers/JsonTest.php

1
framework/CHANGELOG.md

@ -36,6 +36,7 @@ Yii Framework 2 Change Log
- Bug #10029: Fixed MaskedInput not working with PJAX (martrix78, samdark)
- Bug #10101: Fixed assignments saving on role removing in `\yii\rbac\PhpManager` (rezident1307)
- Bug #10142: Fixed `yii\validators\EmailValidator` to check the length of email properly (silverfire)
- Bug #10278: Fixed `yii\helpers\BaseJson` support \SimpleXMLElement data (SilverFire, LAV45)
- Bug: Fixed generation of canonical URLs for `ViewAction` pages (samdark)
- Bug: Fixed `mb_*` functions calls to use `UTF-8` or `Yii::$app->charset` (silverfire)
- Enh #3506: Added `\yii\validators\IpValidator` to perform validation of IP addresses and subnets (SilverFire, samdark)

2
framework/helpers/BaseJson.php

@ -142,6 +142,8 @@ class BaseJson
$data = $data->jsonSerialize();
} elseif ($data instanceof Arrayable) {
$data = $data->toArray();
} elseif ($data instanceof \SimpleXMLElement) {
$data = (array) $data;
} else {
$result = [];
foreach ($data as $name => $value) {

19
tests/framework/helpers/JsonTest.php

@ -7,6 +7,7 @@ use yii\helpers\BaseJson;
use yii\helpers\Json;
use yiiunit\TestCase;
use yii\web\JsExpression;
use yiiunit\framework\web\Post;
/**
* @group helpers
@ -101,6 +102,24 @@ class JsonTest extends TestCase
// JsonSerializable
$data = new JsonModel();
$this->assertSame('{"json":"serializable"}', Json::htmlEncode($data));
// https://github.com/yiisoft/yii2/issues/10278
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<file>
<apiKey>ieu2iqw4o</apiKey>
<methodProperties>
<FindByString>Kiev</FindByString>
</methodProperties>
</file>';
$document = simplexml_load_string($xml);
$this->assertSame('{"apiKey":"ieu2iqw4o","methodProperties":{"FindByString":"Kiev"}}', Json::encode($document));
$postsStack = new \SplStack();
$postsStack->push(new Post(915, 'record1'));
$postsStack->push(new Post(456, 'record2'));
$this->assertSame('{"1":{"id":456,"title":"record2"},"0":{"id":915,"title":"record1"}}', Json::encode($postsStack));
}
public function testDecode()

Loading…
Cancel
Save