Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There seems to be a fundamental contradiction between the specification of escape sequences between JSON and raw strings. #3186

Closed
aeschylus opened this issue Sep 29, 2023 · 6 comments

Comments

@aeschylus
Copy link

aeschylus commented Sep 29, 2023

I did some analysis with Bourne.parse and JSON.parse. The issue is unrelated to request size and is only related to JSON string formatting. Consider the following examples:

try {
  JSON.parse(`{"foo":"bar\""}`)
} catch (e) {
  console.error(e)
}
// SyntaxError: Expected ',' or '}' after property value in JSON at position 12

try {
  JSON.parse(String.raw`{"foo":"bar\""}`)
} catch (e) {
  console.error(e)
}
// No error

hapijs/hapi#4461 (comment)

@ljharb
Copy link
Member

ljharb commented Sep 30, 2023

This doesn’t seem related to Intl; I’ll transfer it to 262.

Why do you expect the output of String.raw to be JSON-parseable in a way that’s similar to the original?

@ljharb ljharb transferred this issue from tc39/ecma402 Sep 30, 2023
@ljharb
Copy link
Member

ljharb commented Sep 30, 2023

Actually, closing, since this is just the content of another person’s issue comment.

@ljharb ljharb closed this as not planned Won't fix, can't repro, duplicate, stale Sep 30, 2023
@aeschylus
Copy link
Author

aeschylus commented Sep 30, 2023

I would expect the JSON as JSON to be parsable. The other person is my teammate, and I posted it here because it's not an issue with Hapi, it's an issue with ECMAScript. What would be the best place to reopen the issue?

Am I wrong to expect the escaped quote to be escaped, and therefore for the JSON to be parsable?

@aeschylus
Copy link
Author

aeschylus commented Sep 30, 2023

The consequence of this is that I can't receive some input to an API endpoint and parse it without causing an error. This is a fundamental need. In this case the client is trying to represent inches (as in: 'This board is 2" x 4"'). I don't have control over the format of this input, and currently we are having to write the stream directly to the file system and then translate it back in, instead of merely parsing it in the API endpoint.

@bakkot
Copy link
Contributor

bakkot commented Sep 30, 2023

Am I wrong to expect the escaped quote to be escaped, and therefore for the JSON to be parsable?

Yes.

This isn't an issue with ECMAScript, and this isn't a support forum. I recommend trying StackOverflow.

@bathos
Copy link
Contributor

bathos commented Oct 1, 2023

In case you haven’t figured this out yet: the string '{"foo":"bar\""}' is an ES string with an ES escape sequence whose string value is a double quote. The full string value is {"foo":"bar""}, which is not valid JSON source text. JSON is another language with its own escape sequences, so the corrected ES string is '{"foo":"bar\\""}', i.e. you need to escape (ES) the escape (JSON).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants