summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2010-08-06 21:36:27 +0200
committerFlorian Frank <flori@ping.de>2010-08-06 21:36:27 +0200
commit991eab7d9016b0c8b4f630985bb6f19511b74d1c (patch)
treebcaefef4bc289ff13861b73c739c4e0d2f193ea5 /lib
parent9c19d632fa7071a93eead3aa544dde3e791ad9c4 (diff)
downloadjson-991eab7d9016b0c8b4f630985bb6f19511b74d1c.tar.gz
some code cleanup
Diffstat (limited to 'lib')
-rw-r--r--lib/json/common.rb10
-rw-r--r--lib/json/pure/parser.rb55
2 files changed, 33 insertions, 32 deletions
diff --git a/lib/json/common.rb b/lib/json/common.rb
index 244634b..3f5b322 100644
--- a/lib/json/common.rb
+++ b/lib/json/common.rb
@@ -11,9 +11,9 @@ module JSON
# generate and parse for their documentation.
def [](object, opts = {})
if object.respond_to? :to_str
- JSON.parse(object.to_str, opts => {})
+ JSON.parse(object.to_str, opts)
else
- JSON.generate(object, opts => {})
+ JSON.generate(object, opts)
end
end
@@ -377,11 +377,11 @@ module ::Kernel
#
# The _opts_ argument is passed through to generate/parse respectively, see
# generate and parse for their documentation.
- def JSON(object, opts = {})
+ def JSON(object, *args)
if object.respond_to? :to_str
- JSON.parse(object.to_str, opts)
+ JSON.parse(object.to_str, args.first)
else
- JSON.generate(object, opts)
+ JSON.generate(object, args.first)
end
end
end
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