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

YAML load errors when parsing JSON that initially contained a \u0000 character sequence #22

Closed
gus opened this issue Mar 1, 2011 · 2 comments

Comments

@gus
Copy link
Contributor

gus commented Mar 1, 2011

When incoming JSON to be parsed contains the character sequence \u0000, YAML blows up. \u0000 converts to \x00 with unescape just fine, but YAML chokes. Basically, this is because end-of-string is considered to be wherever \u0000 was.

The same issue does not arise if \x00 was in the initial string to be JSON.parsed.

Fix/pull-request forthcoming.

@ianyamey
Copy link

ianyamey commented Aug 2, 2011

Here is a quick monkey patch that you can use to get around this problem. By no means is this presented as a permanent fix.

All it does is deletes any \u0000 control characters from the string before it gets converted to yaml.

Not entirely elegant but it does the job...

##
## This is a monkey patch that fix a bug with how Crack/YAML parses the unicode control character \u0000.
## To get around the Invalid JSON string error, we simply remove these from the string
##
module Crack
  class JSON
    def self.parse(json)
      YAML.load(unescape(convert_json_to_yaml(json.gsub(/\\[u|U]0000/,""))))
    rescue ArgumentError => e
      raise ParseError, "Invalid JSON string"
    end
  end
end

@ericgj
Copy link
Contributor

ericgj commented Aug 2, 2011

You can see my -- as yet unmerged -- pull request that fixes this here. If you're using Crack through HTTParty, you can get around this by using a different JSON parser besides Crack. See discussion here.

@kiskoza kiskoza closed this as completed Jan 29, 2024
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