json_encode in PHP: data growth


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
3 views
PHP has built-in tools to convert the data to json. Consider this example:

echo json_encode(iconv('cp1251', 'utf-8', 'test'));

It will return "\\ u0442\\ u0435\\ u0441\\ u0442" (spaces so as not to tempt the parser) is 26 bytes, not 10 as I would like.

How so?
by | 3 views

4 Answers

0 like 0 dislike
Ummm. What's the problem, gentlemen? Sure of the encoding — forward)
\r
php -r 'var_dump(json_encode("test"));'
string(26) ""\\u0442\\u0435\\u0441\\u0442""
\r
php -r 'var_dump(json_encode("test", JSON_UNESCAPED_UNICODE));'
string(10) ""test""
by
0 like 0 dislike
It was in a q& a about it. What to do? To accept and enable gzip.
by
0 like 0 dislike
To use self-made json::encode — there is code in 20 lines.
And even to feed him cp1251 without an envelope.
Such a decision only one negative — working at hundreds of times the built-in solution.
by
0 like 0 dislike
This way of encoding non-Latin characters is, so to speak, the only reliable way to ensure an accurate representation of such symbols regardless of web-page coding. But it is a shame that json_encode() has no option to disable encoding if the encoding of the page and the JSON data is obviously the same.
by

Related questions

0 like 0 dislike
6 answers
asked Mar 22, 2019 by camelos
0 like 0 dislike
3 answers
0 like 0 dislike
3 answers
asked May 22, 2019 by bezopasnosdelka
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
asked May 21, 2019 by Artamov
110,608 questions
257,186 answers
0 comments
25,944 users