diff options
author | Florian Frank <flori@ping.de> | 2010-08-06 21:36:27 +0200 |
---|---|---|
committer | Florian Frank <flori@ping.de> | 2010-08-06 21:36:27 +0200 |
commit | 991eab7d9016b0c8b4f630985bb6f19511b74d1c (patch) | |
tree | bcaefef4bc289ff13861b73c739c4e0d2f193ea5 /lib/json/pure/parser.rb | |
parent | 9c19d632fa7071a93eead3aa544dde3e791ad9c4 (diff) | |
download | json-991eab7d9016b0c8b4f630985bb6f19511b74d1c.tar.gz |
some code cleanup
Diffstat (limited to 'lib/json/pure/parser.rb')
-rw-r--r-- | lib/json/pure/parser.rb | 55 |
1 files changed, 28 insertions, 27 deletions
diff --git a/lib/json/pure/parser.rb b/lib/json/pure/parser.rb index 41b8ac7..844931b 100644 --- a/lib/json/pure/parser.rb +++ b/lib/json/pure/parser.rb @@ -69,41 +69,42 @@ module JSON # * *object_class*: Defaults to Hash # * *array_class*: Defaults to Array def initialize(source, opts = {}) - 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 + opts ||= {} + 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 - JSON.iconv('utf-8', 'utf-32be', b) + source.dup.force_encoding(::Encoding::UTF_32BE).encode!(::Encoding::UTF_8) when b.size >= 4 && b[0] == 0 && b[2] == 0 - JSON.iconv('utf-8', 'utf-16be', b) + source.dup.force_encoding(::Encoding::UTF_16BE).encode!(::Encoding::UTF_8) when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0 - JSON.iconv('utf-8', 'utf-32le', b) + source.dup.force_encoding(::Encoding::UTF_32LE).encode!(::Encoding::UTF_8) + when b.size >= 4 && b[1] == 0 && b[3] == 0 - JSON.iconv('utf-8', 'utf-16le', b) + source.dup.force_encoding(::Encoding::UTF_16LE).encode!(::Encoding::UTF_8) else - b + 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 |