Hello. I saw that JS supports the x-notation (\x40) and u-notation
(\u0040), but PHP only supports u-notation. There some reason for that?
JSON.parse('"\x40"'); // => @
JSON.parse('"\u0040"'); // => @
While PHP:
json_decode('"\u0040"'); // => @
json_decode('"\x40"'); // => null (Syntax error)
The json.org really don't seems doc the x-notation version, but some pages
does: http://www.javascriptkit.com/jsref/escapesequence.shtml
The same is valid to json_encode('@'), it will stringify to '\u0040'
instead of the most simplified version '\x40'.
I don't know if there are some reason for that, but I think that, at least,
is reasonable to json_decode()
supports that.
--
David Rodrigues
Hello. I saw that JS supports the x-notation (\x40) and u-notation
(\u0040), but PHP only supports u-notation. There some reason for that?
The TL;DR version AIUI, is that JSON strings are Unicode strings, so
any byte sequence in a JSON string must be valid UTF-8. With \x
encoding, one could easily create byte sequences which are not valid
UTF-8. Best practices tend to use an intermediate, binary-safe
encoding (such as base64) when needing to transfer octet streams.
-Sara
Hi David,
David Rodrigues wrote:
Hello. I saw that JS supports the x-notation (\x40) and u-notation
(\u0040), but PHP only supports u-notation. There some reason for that?JSON.parse('"\x40"'); // => @
JSON.parse('"\u0040"'); // => @While PHP:
json_decode('"\u0040"'); // => @
json_decode('"\x40"'); // => null (Syntax error)The json.org really don't seems doc the x-notation version, but some pages
does: http://www.javascriptkit.com/jsref/escapesequence.shtmlThe same is valid to json_encode('@'), it will stringify to '\u0040'
instead of the most simplified version '\x40'.I don't know if there are some reason for that, but I think that, at least,
is reasonable tojson_decode()
supports that.
Douglas Crockford tried to keep JSON as minimal as possible, so that's
probably why JSON doesn't support \x. That it's supported by JS doesn't
matter, JSON isn't JS. In JS it is just a synonym for \u so there's not
really anything you're missing out on anyway.
Thanks.
--
Andrea Faulds
https://ajf.me/
Hi,
Andrea Faulds wrote:
Hi David,
David Rodrigues wrote:
Hello. I saw that JS supports the x-notation (\x40) and u-notation
(\u0040), but PHP only supports u-notation. There some reason for that?JSON.parse('"\x40"'); // => @
JSON.parse('"\u0040"'); // => @While PHP:
json_decode('"\u0040"'); // => @
json_decode('"\x40"'); // => null (Syntax error)The json.org really don't seems doc the x-notation version, but some
pages
does: http://www.javascriptkit.com/jsref/escapesequence.shtmlThe same is valid to json_encode('@'), it will stringify to '\u0040'
instead of the most simplified version '\x40'.I don't know if there are some reason for that, but I think that, at
least,
is reasonable tojson_decode()
supports that.Douglas Crockford tried to keep JSON as minimal as possible, so that's
probably why JSON doesn't support \x. That it's supported by JS doesn't
matter, JSON isn't JS. In JS it is just a synonym for \u so there's not
really anything you're missing out on anyway.Thanks.
A follow-up, I was confused by your example appearing to show JSON.parse
supporting this, because it definitely isn't valid JSON. Well, the thing
is that '' and "" behave identically in JS, unlike in PHP where '' lacks
most escape sequences. You missed a slash (and so did I at first!):
JSON.parse('"\x40"')
< SyntaxError: JSON Parse error: Invalid escape character x
--
Andrea Faulds
https://ajf.me/
On Fri, Jun 29, 2018 at 8:51 PM, David Rodrigues david.proweb@gmail.com
wrote:
Hello. I saw that JS supports the x-notation (\x40) and u-notation
(\u0040), but PHP only supports u-notation. There some reason for that?JSON.parse('"\x40"'); // => @
JSON.parse('"\u0040"'); // => @
No it doesn't. JSON.parse() will correctly reject \x escapes because they
aren't valid JSON. You are using a normal JS string (which has nothing to
do with JSON and supports \x), which will unescape \x before JSON.parse()
ever sees it. If you write JSON.parse('"\x40"') or similar, you will get
an error.
Nikita
Em qua, 4 de jul de 2018 19:01, Nikita Popov nikita.ppv@gmail.com
escreveu:
On Fri, Jun 29, 2018 at 8:51 PM, David Rodrigues david.proweb@gmail.com
wrote:Hello. I saw that JS supports the x-notation (\x40) and u-notation
(\u0040), but PHP only supports u-notation. There some reason for that?JSON.parse('"\x40"'); // => @
JSON.parse('"\u0040"'); // => @No it doesn't. JSON.parse() will correctly reject \x escapes because they
aren't valid JSON. You are using a normal JS string (which has nothing to
do with JSON and supports \x), which will unescape \x before JSON.parse()
ever sees it. If you write JSON.parse('"\x40"') or similar, you will get
an error.
Oh! You are right. I just forgot to test this case. :D Topic is closed if
no one have no more to say, so.
Nikita