diff options
author | Florian Frank <flori@ping.de> | 2009-10-19 14:16:03 +0200 |
---|---|---|
committer | Florian Frank <flori@ping.de> | 2009-10-19 14:20:47 +0200 |
commit | 54fad60636b416899fdc25c50957c4f6538a38c5 (patch) | |
tree | 29c42742a1d7551f2427d5d10514c51a456e86f3 /lib | |
parent | c6b15875bb0b099282a848f85bd9a9e9a4e038a2 (diff) | |
parent | 03b157516fa22ac135496a3831963f8305f7a0bb (diff) | |
download | json-54fad60636b416899fdc25c50957c4f6538a38c5.tar.gz |
implemented utf sniffing, transcoding in parser
improved documentation
added to changes
bumped version
Diffstat (limited to 'lib')
-rw-r--r-- | lib/json/common.rb | 46 | ||||
-rw-r--r-- | lib/json/pure/parser.rb | 36 | ||||
-rw-r--r-- | lib/json/version.rb | 2 |
3 files changed, 67 insertions, 17 deletions
diff --git a/lib/json/common.rb b/lib/json/common.rb index 467c7b2..39f6336 100644 --- a/lib/json/common.rb +++ b/lib/json/common.rb @@ -1,4 +1,5 @@ require 'json/version' +require 'iconv' module JSON class << self @@ -105,7 +106,7 @@ module JSON module_function - # Parse the JSON string _source_ into a Ruby data structure and return it. + # Parse the JSON document _source_ into a Ruby data structure and return it. # # _opts_ can have the following # keys: @@ -122,9 +123,9 @@ module JSON JSON.parser.new(source, opts).parse end - # Parse the JSON string _source_ into a Ruby data structure and return it. + # Parse the JSON document _source_ into a Ruby data structure and return it. # The bang version of the parse method, defaults to the more dangerous values - # for the _opts_ hash, so be sure only to parse trusted _source_ strings. + # for the _opts_ hash, so be sure only to parse trusted _source_ documents. # # _opts_ can have the following keys: # * *max_nesting*: The maximum depth of nesting allowed in the parsed data @@ -145,9 +146,8 @@ module JSON JSON.parser.new(source, opts).parse end - # Unparse the Ruby data structure _obj_ into a single line JSON string and - # return it. _state_ is - # * a JSON::State object, + # Generate a JSON document from the Ruby data structure _obj_ and return + # it. _state_ is * a JSON::State object, # * or a Hash like object (responding to to_hash), # * an object convertible into a hash by a to_h method, # that is used as or to configure a State object. @@ -180,7 +180,11 @@ module JSON else state = State.new end - obj.to_json(state) + result = obj.to_json(state) + if result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m + raise GeneratorError, "only generation of JSON objects or arrays allowed" + end + result end # :stopdoc: @@ -190,14 +194,17 @@ module JSON module_function :unparse # :startdoc: - # Unparse the Ruby data structure _obj_ into a single line JSON string and - # return it. This method disables the checks for circles in Ruby objects, and - # also generates NaN, Infinity, and, -Infinity float values. + # Generate a JSON document from the Ruby data structure _obj_ and return it. + # This method disables the checks for circles in Ruby objects. # # *WARNING*: Be careful not to pass any Ruby data structures with circles as # _obj_ argument, because this will cause JSON to go into an infinite loop. def fast_generate(obj) - obj.to_json(nil) + result = obj.to_json(nil) + if result !~ /\A(?:\[.*\]|\{.*\})\Z/ + raise GeneratorError, "only generation of JSON objects or arrays allowed" + end + result end # :stopdoc: @@ -206,8 +213,9 @@ module JSON module_function :fast_unparse # :startdoc: - # Unparse the Ruby data structure _obj_ into a JSON string and return it. The - # returned string is a prettier form of the string returned by #unparse. + # Generate a JSON document from the Ruby data structure _obj_ and return it. + # The returned document is a prettier form of the document returned by + # #unparse. # # The _opts_ argument can be used to configure the generator, see the # generate method for a more detailed explanation. @@ -229,7 +237,11 @@ module JSON end state.configure(opts) end - obj.to_json(state) + result = obj.to_json(state) + if result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m + raise GeneratorError, "only generation of JSON objects or arrays allowed" + end + result end # :stopdoc: @@ -270,7 +282,6 @@ module JSON proc.call result end end - module_function :recurse_proc alias restore load module_function :restore @@ -306,6 +317,11 @@ module JSON rescue JSON::NestingError raise ArgumentError, "exceed depth limit" end + + # Shortuct for iconv. + def self.iconv(to, from, string) + Iconv.iconv(to, from, string).first + end end module ::Kernel 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] diff --git a/lib/json/version.rb b/lib/json/version.rb index 788ca2e..ff48b9b 100644 --- a/lib/json/version.rb +++ b/lib/json/version.rb @@ -1,6 +1,6 @@ module JSON # JSON version - VERSION = '1.1.9' + VERSION = '1.2.0' VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc: VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc: VERSION_MINOR = VERSION_ARRAY[1] # :nodoc: |