Is there any benefit of saving UTF-8 characters unescaped in a json file if one only access them through PHP?
Here is what I tested:
fwrite(fopen('fileA.json','w'), json_encode('аккредитовать'));
then the content of fileA.json
is given by
"\u0413\u043b\u0430\u0432\u043d\u0430\u044f"
However, when I store it with
fwrite(fopen('fileB.json','w'), json_encode('аккредитовать', JSON_UNESCAPED_UNICODE));
the content of fileB.json
is given by
"аккредитовать"
To my surprise each of the following calls
echo json_decode(file_get_contents('fileA.json'));
echo json_decode(file_get_contents('fileB.json'));
echo json_decode(file_get_contents('fileA.json')), false, 512, JSON_UNESCAPED_UNICODE);
echo json_decode(file_get_contents('fileB.json')), false, 512, JSON_UNESCAPED_UNICODE);
gives the same output:
'аккредитовать'
So as a result I would conclude that I only need to save UTF-8 chars in a json file if I want to open and read the json file directly with an editor. If I only plan to show/save the content of the json file with php then I don't need save the content unescaped and I can use
fwrite(fopen('fileA.json','w'), json_encode('аккредитовать'));
echo json_decode(file_get_contents('fileA.json'));`
Is that correct, or did I miss anything important?