In fact, I always thought that JSON would escape all but ASCII displayable characters to Unicode,
Until one time I used JSON. stringify only found that, in fact, is PHP for us to think too thoughtful.
I used to be a phper, so it is very convenient to process JSON as long as the Json_encode can convert the array to JSON data.
As you can see, the default is to escape all ASCII-displayable characters to Unicode.
What good does it do?
Everyone in call Jsonp interface or call JS file, because the file encoding different caused garbled problem, should not be unfamiliar with it.
If your file has non-English characters, it will be garbled if the file encoding is inconsistent when you call it.
Many novice friends should have struggled with this problem.
However, if the characters are escaped to Unicode, they will not be garbled, regardless of the consistent file encoding.
That's why PHP is encoded as Unicode by default, and she's too thoughtful for us to think about it.
Of course, if you want to directly display those characters, is OK, the second parameter plus Json_unescaped_unicode can be.
But this parameter PHP 5.4.0 only started to support.
So JSON. What will stringify escape?
You can see this regular in line No. 351 of Json2.js.
escapable =/[\\\ "\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f \ufeff\ufff0-\uffff]/g;
That is, JSON will only escape this part of the character Unicode, let's simply test it out.
Console.log (Json.stringify ("\x00 \x0a"));
Point to run, you can see that \x00 was escaped to \u0000 and \x0a was designed for \ n
The conversion of these special characters can be seen just below the regular.
But when you test the character \ufeff you will find that Firefox and Chrome are not escaped at all.
Indeed, it seems that only Json2 escaped for us.
Why native JSON. stringify So many characters are not escaped, he did not consider the compatibility problem for us?
In fact, I think, this problem can not be considered, because you do not directly use static pages for other sites to provide interfaces and so on.
Often just their own internal use, even if submitted to the background, a project under the same code, so the internal does not need to consider those compatibility issues.
Like in your hometown, do you want to communicate with them in Mandarin or English?
Communicate directly with dialect to be more fluent.
Of course this is only my personal opinion, do not know how to write the JS engine of the Great God is how to think.
Let's walk through the native JSON escape of these characters \u000-\uffff.
for (var i = 0, str = ' ', arr = []; i < 0xFFFF; i++) { str = json.stringify (String.fromCharCode (i)); Str.indexof ("\ \") >-1 && arr.push (str);} Console.log (Arr.join (","));
My chrome 34 got the result
["\u0000", "\u0001", "\u0002", "\u0003", "\u0004", "\u0005", "\u0006", "\u0007", "\b", "\ T", "\ n", "\u000b", "\f", "\ R", " \u000e "," \u000f "," \u0010 "," \u0011 "," \u0012 "," \u0013 "," \u0014 "," \u0015 "," \u0016 "," \u0017 "," \u0018 "," \u0019 "," \u 001a "," \u001b "," \u001c "," \u001d "," \u001e "," \u001f "," \ "", "\ \"];
Well, that's all for today's share.