rubymotion-community / BubbleWrap

Cocoa wrappers and helpers for RubyMotion (Ruby for iOS and OS X) - Making Cocoa APIs more Ruby like, one API at a time. Fork away and send your pull requests
Other
1.18k stars 208 forks source link

BW::JSON.parse chokes on non-ASCII characters. #325

Closed LandonSchropp closed 10 years ago

LandonSchropp commented 10 years ago

BW::JSON.parse("[ \"hello\" ]") produces ["hello"], but BW::JSON.parse("[ \"hello\x80\" ]") produces the following error:

2013-12-17 17:07:30.891 rui[54571:80b] json.rb:21:in `parse:': Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unable to convert data to string around character 2.) UserInfo=0xae6d810 {NSDebugDescription=Unable to convert data to string around character 2.} (BubbleWrap::JSON::ParserError)
=> #<BubbleWrap::JSON::ParserError: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unable to convert data to string around character 2.) UserInfo=0xae6d810 {NSDebugDescription=Unable to convert data to string around character 2.}>
clayallsopp commented 10 years ago

Hm, I don't think this is true. Your character doesn't need to be escaped in code, since (IIRC) strings in Ruby are UTF8 anyway. Ex: BW::JSON.parse('{"symbol": "€"}') works fine

LandonSchropp commented 10 years ago

I encountered this problem when parsing a response from an external server I have no control over. Since the code is valid in Ruby, I feel like this use case should be supported.

clayallsopp commented 10 years ago

Did a bit more research, and your server is sending invalid JSON. This is not a BubbleWrap problem - it would fail if you used other JSON parsers.

The JSON spec states that characters should be encoded as \u____. From the JSON spec:

JSON unicode