EncodeJavaScript uses numbers that can not be parsed in JSON

EncodeJavaScript uses numbers that can not be parsed in JSON


I've tried to use built-in EncodeJavaScript function to encode values for JSON string, but the result could not be parsed. The reason is that it encodes double-quote (") as \x22 and slash (\) as \x3. I would expect these to be encoded as \" and \\ respectively. And the function does encode line-breaks as \r\n, which is good. I have checked with an online parser, and it failed on the first "\x" it encountered, so it looks like the whole "\xN" construction is invalid (I'm not sure if met such construction before so don't know about it).

Is there a reason to use these numerics?
Should I maybe post an Idea to change this?
Hi Igor,

Tecnically the EscapeJavascript function has the correct behaviour, since it is made to escape strings in Javascript and the \x is a valid escape caracter in javascript.
Here is the spec: http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4

But it is not valid in JSON strings where it only accepts the \u escape ( http://www.ietf.org/rfc/rfc4627.txt)

So you have 3 options here:
  • Use a forge component to convert your structure/entity to json for you (ardoJSON) instead of building it yourself.
  • Make yourself a method that excapes what you need (following the spec "quotation mark, reverse solidus, and the control characters (U+0000    through U+001F)").
  • Make a regular expression replace after the EncodeJavascript of  "(?<!\\)\\x" with "\u00"  (notice the negative lookbehind on the regular expression so it doesn't catch a \\x)
João Rosado