Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] json_encode() x-notation

Posted by David Rodrigues 
David Rodrigues
[PHP-DEV] json_encode() x-notation
June 29, 2018 09:00PM
​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
Sara Golemon
Re: [PHP-DEV] json_encode() x-notation
June 29, 2018 09:40PM
On Fri, Jun 29, 2018 at 1:51 PM, David Rodrigues <[email protected]> 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?
>
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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Andrea Faulds
[PHP-DEV] Re: json_encode() x-notation
July 05, 2018 12:00AM
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.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.
>

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/

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Nikita Popov
Re: [PHP-DEV] json_encode() x-notation
July 05, 2018 12:10AM
On Fri, Jun 29, 2018 at 8:51 PM, David Rodrigues <[email protected]>
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
Andrea Faulds
[PHP-DEV] Re: json_encode() x-notation
July 05, 2018 01:30AM
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.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.
>>
>
> 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/

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
David Rodrigues
Re: [PHP-DEV] json_encode() x-notation
July 05, 2018 01:40AM
Em qua, 4 de jul de 2018 19:01, Nikita Popov <[email protected]>
escreveu:

> On Fri, Jun 29, 2018 at 8:51 PM, David Rodrigues <[email protected]>
> 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
>
Sorry, only registered users may post in this forum.

Click here to login