setJsonContent unicode issue

the code like below:

<?php
$app = new Phalcon\Mvc\Micro(new Phalcon\DI\FactoryDefault());
$r=$app->response;
$r->setJsonContent(array('中文'));
echo $r->getContent();

will produte the output like

["\u4e2d\u6587"]

In my client (writen by C++) the result parsed by JSONNode is can't readable. but the code below is fine

<?php
$app = new Phalcon\Mvc\Micro(new Phalcon\DI\FactoryDefault());
$r=$app->response;
$r->setContent(json_encode(array('中文'),JSON_UNESCAPE_UNICODE));
echo $r->getContent();

output:

["中文"]

Is that a bug?



6.3k
edited Nov '14

It is not a bug, it is a feature. It works like this in most implementations.

Although rare, there are some implementations that don't escape unicode characters... Anyway, it will work escaped, don't worry :)

edit: if your client in C++ can't parse that json, then it is buggy as most implementations of JSON escape the Unicode (altough it is not a rule)

Hi! Use this ;)

$r->setJsonContent(array('中文'),JSON_UNESCAPE_UNICODE);