Skip to content

[#498] Handle utf-8 bom for json parsing#520

Merged
jnunemaker merged 1 commit intojnunemaker:masterfrom
rsullivan00:rs/bom_handling
Apr 21, 2017
Merged

[#498] Handle utf-8 bom for json parsing#520
jnunemaker merged 1 commit intojnunemaker:masterfrom
rsullivan00:rs/bom_handling

Conversation

@rsullivan00
Copy link
Contributor

@rsullivan00 rsullivan00 commented Apr 17, 2017

Closes #498

Currently, HTTParty errors when you try to parse JSON that has a utf-8 BOM. Since .NET applications automatically prepend the BOM characters, this severely limits the usefulness of HTTParty when talking to Windows-based JSON services.

This PR resolves that issue by stripping the BOM characters before parsing the JSON response.


def json
JSON.parse(body, :quirks_mode => true, :allow_nan => true)
JSON.parse(body.gsub(/^#{UTF8_BOM}/, ''), :quirks_mode => true, :allow_nan => true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is the right spot for this logic, or if I should be checking encoding somehow before trying to strip the BOM.

I'm open to suggestions, if there is a better way to do this.

@jnunemaker
Copy link
Owner

I really don't know much about this, but it seems like an ok fix to me. Thanks!

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

Successfully merging this pull request may close these issues.

BOM characters \xEF\xBB\xBF

2 participants