Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102595 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 33530 invoked from network); 4 Jul 2018 23:24:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Jul 2018 23:24:41 -0000 X-Host-Fingerprint: 85.230.241.5 c-05f1e655.79-4-64736c10.bbcust.telenor.se Received: from [85.230.241.5] ([85.230.241.5:13830] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id BF/DA-55607-7375D3B5 for ; Wed, 04 Jul 2018 19:24:39 -0400 Message-ID: To: internals@lists.php.net References: <38.D8.55607.0324D3B5@pb1.pair.com> Date: Thu, 5 Jul 2018 01:24:35 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:49.0) Gecko/20100101 Firefox/49.0 SeaMonkey/2.46 MIME-Version: 1.0 In-Reply-To: <38.D8.55607.0324D3B5@pb1.pair.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Posted-By: 85.230.241.5 Subject: Re: json_encode() x-notation From: ajf@ajf.me (Andrea Faulds) 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/