json
json copied to clipboard
Extrange error parsing a hash
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
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
Many Thanks!