Bug #6566

JSON.dump can generate invalid UTF-8 sequence

Added by Shyouhei Urabe almost 3 years ago. Updated about 2 years ago.

Assignee:Yui NARUSE
ruby -v:ruby 2.0.0dev (2012-06-09) [x86_64-linux] Backport:


Look, in the following code JSON.dump outputs a sequence invalid as UTF-8.

# -- encoding: utf-8 --
require 'json'
IO.popen('hexdump -C', 'w') do |fp|
JSON.dump(["\xea"], fp)

RFC4627 says that to encode JSON as a Unicode is a "SHALL". So this is an RFC violation.


bug-6566.diff Magnifier - reject invalid UTF-8 sequence in JSON.generate (1.62 KB) Nobuyoshi Nakada, 06/10/2012 07:19 AM


#1 Updated by Nobuyoshi Nakada almost 3 years ago

A bit simpler, it seems wrong that
returns (({false})).

I think this would be a bug in json generator, but what should happen
in this case? Seems (({convert_UTF8_to_JSON_ASCII()})) wants to reject
invalid sequence.

#2 Updated by Yui NARUSE almost 3 years ago

json is not only for 1.9, so nobu's patch is not acceptable.
I made https://github.com/flori/json/pull/139 .

#3 Updated by Yui NARUSE about 2 years ago

  • Target version changed from 2.0.0 to next minor

Also available in: Atom PDF