TomR TomR - 3 months ago 33
JSON Question

REST Yii2 - how to display non-UTF8 characters coming from database in json?

I am building Yii2 REST application that uses WIN1257 (not UTF8) encoded Firebird 2.1 database. I can get json result if data does not containt non-latin letters but when varchar fields contain non-latin language specifice characters then I get errors message:

malformed UTF-8 characters, possibly incorrectly encoded., "yii\base\InvalidParamException"


Full stack trace is:

{
"name": "Exception",
"message": "Malformed UTF-8 characters, possibly incorrectly encoded.",
"code": 5,
"type": "yii\\base\\InvalidParamException",
"file": "C:\\xampp\\htdocs\\myproject\\vendor\\yiisoft\\yii2\\helpers\\BaseJson.php",
"line": 123,
"stack-trace": [
"#0 C:\\xampp\\htdocs\\myproject\\vendor\\yiisoft\\yii2\\helpers\\BaseJson.php(61): yii\\helpers\\BaseJson::handleJsonError(5)",
"#1 C:\\xampp\\htdocs\\myproject\\vendor\\yiisoft\\yii2\\web\\JsonResponseFormatter.php(89): yii\\helpers\\BaseJson::encode(Array, 320)",
"#2 C:\\xampp\\htdocs\\myproject\\vendor\\yiisoft\\yii2\\web\\JsonResponseFormatter.php(73): yii\\web\\JsonResponseFormatter->formatJson(Object(yii\\web\\Response))",
"#3 C:\\xampp\\htdocs\\myproject\\vendor\\yiisoft\\yii2\\web\\Response.php(948): yii\\web\\JsonResponseFormatter->format(Object(yii\\web\\Response))",
"#4 C:\\xampp\\htdocs\\myproject\\vendor\\yiisoft\\yii2\\web\\Response.php(312): yii\\web\\Response->prepare()",
"#5 C:\\xampp\\htdocs\\myproject\\vendor\\yiisoft\\yii2\\base\\Application.php(381): yii\\web\\Response->send()",
"#6 C:\\xampp\\htdocs\\myproject\\web\\index.php(12): yii\\base\\Application->run()",
"#7 {main}"
]
}


Is it possible to derive some class that converts those characters into UTF8 characters before presenting them to json formatter? The database migration to UTF8 is currently out of reach, web application should adapt to the requirements in this case.

Answer

Can you just convert your string from WIN1257 to UTF-8?

$str = mb_convert_encoding($str, "CP1257", "UTF-8");

You can also try libiconv:

$str = iconv("CP1257", "UTF-8", $str);