diff options
Diffstat (limited to 'lib/json/pure/parser.rb')
-rw-r--r-- | lib/json/pure/parser.rb | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/lib/json/pure/parser.rb b/lib/json/pure/parser.rb index 7e8fe08..7a09f2f 100644 --- a/lib/json/pure/parser.rb +++ b/lib/json/pure/parser.rb @@ -66,7 +66,41 @@ module JSON # * *object_class*: Defaults to Hash # * *array_class*: Defaults to Array def initialize(source, opts = {}) - super + if defined?(::Encoding) + if source.encoding == Encoding::ASCII_8BIT + b = source[0, 4].bytes.to_a + source = case + when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0 + source.dup.force_encoding(Encoding::UTF_32BE).encode!(Encoding::UTF_8) + when b.size >= 4 && b[0] == 0 && b[2] == 0 + source.dup.force_encoding(Encoding::UTF_16BE).encode!(Encoding::UTF_8) + when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0 + source.dup.force_encoding(Encoding::UTF_32LE).encode!(Encoding::UTF_8) + when b.size >= 4 && b[1] == 0 && b[3] == 0 + source.dup.force_encoding(Encoding::UTF_16LE).encode!(Encoding::UTF_8) + else + source.dup + end + else + source = source.encode(Encoding::UTF_8) + end + source.force_encoding(Encoding::ASCII_8BIT) + else + b = source + source = case + when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0 + JSON.iconv('utf-8', 'utf-32be', b) + when b.size >= 4 && b[0] == 0 && b[2] == 0 + JSON.iconv('utf-8', 'utf-16be', b) + when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0 + JSON.iconv('utf-8', 'utf-32le', b) + when b.size >= 4 && b[1] == 0 && b[3] == 0 + JSON.iconv('utf-8', 'utf-16le', b) + else + b + end + end + super source if !opts.key?(:max_nesting) # defaults to 19 @max_nesting = 19 elsif opts[:max_nesting] |