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

JSON.parse can't parse special characters #8297

Closed
Blacksmoke16 opened this issue Oct 9, 2019 · 2 comments
Closed

JSON.parse can't parse special characters #8297

Blacksmoke16 opened this issue Oct 9, 2019 · 2 comments
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. status:invalid topic:stdlib:serialization

Comments

@Blacksmoke16
Copy link
Member

Blacksmoke16 commented Oct 9, 2019

JSON.parse errors when parsing strings with special chars, e.x. \t or \n.

https://play.crystal-lang.org/#/r/7qpu

require "json"

puts JSON.parse %({"body": "A bunch of data\tyo"})
Unhandled exception: Unexpected char '	' at 1:26 (JSON::ParseException)
  from /usr/lib/crystal/json/lexer.cr:371:7 in 'raise'
  from /usr/lib/crystal/json/lexer.cr:367:5 in 'unexpected_char'
  from /usr/lib/crystal/json/lexer.cr:366:11 in 'unexpected_char'
  from /usr/lib/crystal/json/lexer/string_based.cr:28:11 in 'consume_string'
  from /usr/lib/crystal/json/lexer.cr:61:37 in 'next_token'
  from /usr/lib/crystal/json/parser.cr:108:11 in 'next_token'
  from /usr/lib/crystal/json/parser.cr:83:11 in 'parse_object'
  from /usr/lib/crystal/json/parser.cr:35:7 in 'parse_value'
  from /usr/lib/crystal/json/parser.cr:13:12 in 'parse'
  from /usr/lib/crystal/json.cr:137:5 in 'parse'
  from /eval:3:6 in '__crystal_main'
  from /usr/lib/crystal/crystal/main.cr:97:5 in 'main_user_code'
  from /usr/lib/crystal/crystal/main.cr:86:7 in 'main'
  from /usr/lib/crystal/crystal/main.cr:106:3 in 'main'
  from __libc_start_main
  from _start
  from ???

Discovered while doing some manual testing of Blacksmoke16/oq#37.

@asterite
Copy link
Member

asterite commented Oct 9, 2019

The exact same program fails in Ruby too: https://carc.in/#/r/7qrf

According to the JSON spec, a string is any codepoint except ", \ or control characters.

@asterite asterite closed this as completed Oct 9, 2019
@yxhuvud
Copy link
Contributor

yxhuvud commented Oct 9, 2019

Do note that it works everywhere (including in Javascript) if you escape the \ with another \.

@Blacksmoke16 Blacksmoke16 added kind:bug A bug in the code. Does not apply to documentation, specs, etc. status:invalid topic:stdlib:serialization labels Jan 12, 2023
@Blacksmoke16 Blacksmoke16 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. status:invalid topic:stdlib:serialization
Projects
None yet
Development

No branches or pull requests

3 participants