summaryrefslogtreecommitdiff
path: root/ext/json/lib/json/pure/generator.rb
diff options
context:
space:
mode:
Diffstat (limited to 'ext/json/lib/json/pure/generator.rb')
-rw-r--r--ext/json/lib/json/pure/generator.rb7
1 files changed, 3 insertions, 4 deletions
diff --git a/ext/json/lib/json/pure/generator.rb b/ext/json/lib/json/pure/generator.rb
index c8bbfd09eeb..91bdc0ff410 100644
--- a/ext/json/lib/json/pure/generator.rb
+++ b/ext/json/lib/json/pure/generator.rb
@@ -41,6 +41,7 @@ module JSON
# UTF16 big endian characters as \u????, and return it.
def utf8_to_json(string) # :nodoc:
string = string.gsub(/["\\\/\x0-\x1f]/) { MAP[$&] }
+ string.force_encoding(Encoding::ASCII_8BIT)
string.gsub!(/(
(?:
[\xc2-\xdf][\x80-\xbf] |
@@ -50,12 +51,10 @@ module JSON
[\x80-\xc1\xf5-\xff] # invalid
)/nx) { |c|
c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
- s = JSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
- s.gsub!(/.{4}/n, '\\\\u\&')
+ c = c.unpack("U*")[0]
+ c > 0xFFFF ? ('\uD%03X\uD%03X' % [0x7C0+c/1024, 0xC00+c%1024]) : ('\u%04X'%c)
}
string
- rescue Iconv::Failure => e
- raise GeneratorError, "Caught #{e.class}: #{e}"
end
module_function :utf8_to_json