summaryrefslogtreecommitdiff
path: root/ext/json/lib/json/pure
diff options
context:
space:
mode:
Diffstat (limited to 'ext/json/lib/json/pure')
-rw-r--r--ext/json/lib/json/pure/generator.rb7
-rw-r--r--ext/json/lib/json/pure/parser.rb26
2 files changed, 21 insertions, 12 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
diff --git a/ext/json/lib/json/pure/parser.rb b/ext/json/lib/json/pure/parser.rb
index 9b30f15c07a..a0ab3075fef 100644
--- a/ext/json/lib/json/pure/parser.rb
+++ b/ext/json/lib/json/pure/parser.rb
@@ -126,20 +126,30 @@ module JSON
if u = UNESCAPE_MAP[$&[1]]
u
else # \uXXXX
- bytes = ''
- i = 0
- while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
- bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
- i += 1
+ res = []
+ stack = nil
+ [c.delete!('\\\\u')].pack("H*").unpack("n*").each do |c|
+ case c
+ when 0xD800..0xDBFF
+ raise JSON::ParserError, "partial character in source" if stack
+ stack = c
+ when 0xDC00..0xDFFF
+ raise JSON::ParserError,
+ "partial character in source" unless (0xD800..0xDBFF).include?(stack)
+ res << (stack << 10) - 0x35fdc00 + c
+ stack = nil
+ else
+ raise JSON::ParserError, "partial character in source" if stack
+ res << c
+ end
end
- JSON::UTF16toUTF8.iconv(bytes)
+ raise JSON::ParserError, "partial character in source" if stack
+ res.pack("U*")
end
end
else
UNPARSED
end
- rescue Iconv::Failure => e
- raise GeneratorError, "Caught #{e.class}: #{e}"
end
def parse_value