# json_encode() x-notation

- Thread: https://externals.io/message/102531
- Original archive: https://news-web.php.net/php.internals/102531
- Started by David Rodrigues on 2018-06-29T18:51:15+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #102531 — David Rodrigues — 2018-06-29T18:51:15+00:00

URL: https://externals.io/message/102531#102531

​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

---

## #102532 — Sara Golemon — 2018-06-29T19:37:09+00:00

URL: https://externals.io/message/102531#102532 · In reply to: #102531

> 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

---

## #102589 — Andrea Faulds — 2018-07-04T21:54:53+00:00

URL: https://externals.io/message/102531#102589 · In reply to: #102531

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/

---

## #102590 — Nikita Popov — 2018-07-04T22:01:52+00:00

URL: https://externals.io/message/102531#102590 · In reply to: #102531

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

---

## #102595 — Andrea Faulds — 2018-07-04T23:24:35+00:00

URL: https://externals.io/message/102531#102595 · In reply to: #102589

Hi,

Andrea Faulds wrote:
> Hi David,
>
> David Rodrigues wrote:
> > […]
>
> 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/

---

## #102596 — David Rodrigues — 2018-07-04T23:28:33+00:00

URL: https://externals.io/message/102531#102596 · In reply to: #102590

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:
>
> > […]
>
> 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
