json icon indicating copy to clipboard operation
json copied to clipboard

Extrange error parsing a hash

Open jgordor opened this issue 17 years ago • 2 comments

require 'json'

test1 = { "userdata"=>"PK\001\004\024\023\01230\010\b\000{{5;~\37" } p test1.to_json test2 = { "userdata"=>"PK\001\004\024\023\01230\010\b\000{{5;~\377" } p test2.to_json

"{"userdata":"PK\u0001\u0004\u0014\u0013\n30\b\b\u0000{{5;~\u001f"}" test.rb:6:in `to_json': partial character in source, but hit end (JSON::GeneratorError) from test.rb:6

What can be happen with this data?

Thanks in advance

jgordor avatar Sep 21 '09 20:09 jgordor

The value string isn't valid: In UTF-8 byte sequences aren't allow to start with a 0xff byte.

If you want to create JSON for the string from your example you could use String#to_json_raw_object as a workaround:

test3 = { "userdata"=>"PK\001\004\024\023\01230\010\b\000{{5;~\377".to_json_raw_object }
JSON(JSON(test3)) == test2 # => true

flori avatar Sep 22 '09 09:09 flori

Many Thanks!

jgordor avatar Sep 22 '09 09:09 jgordor