summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/json/add/ostruct.rb31
-rw-r--r--lib/json/add/time.rb4
-rw-r--r--lib/json/common.rb45
-rw-r--r--lib/json/ext.rb6
-rw-r--r--lib/json/pure.rb6
-rw-r--r--lib/json/pure/generator.rb29
-rw-r--r--lib/json/pure/parser.rb9
-rw-r--r--lib/json/version.rb2
8 files changed, 113 insertions, 19 deletions
diff --git a/lib/json/add/ostruct.rb b/lib/json/add/ostruct.rb
new file mode 100644
index 0000000..da81e10
--- /dev/null
+++ b/lib/json/add/ostruct.rb
@@ -0,0 +1,31 @@
+unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
+ require 'json'
+end
+require 'ostruct'
+
+# OpenStruct serialization/deserialization
+class OpenStruct
+
+ # Deserializes JSON string by constructing new Struct object with values
+ # <tt>v</tt> serialized by <tt>to_json</tt>.
+ def self.json_create(object)
+ new(object['t'] || object[:t])
+ end
+
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
+ klass = self.class.name
+ klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
+ {
+ JSON.create_id => klass,
+ 't' => table,
+ }
+ end
+
+ # Stores class name (OpenStruct) with this struct's values <tt>v</tt> as a
+ # JSON string.
+ def to_json(*args)
+ as_json.to_json(*args)
+ end
+end
diff --git a/lib/json/add/time.rb b/lib/json/add/time.rb
index abc807a..9755707 100644
--- a/lib/json/add/time.rb
+++ b/lib/json/add/time.rb
@@ -10,8 +10,8 @@ class Time
if usec = object.delete('u') # used to be tv_usec -> tv_nsec
object['n'] = usec * 1000
end
- if respond_to?(:tv_nsec)
- at(*object.values_at('s', 'n'))
+ if instance_methods.include?(:tv_nsec)
+ at(object['s'], Rational(object['n'], 1000))
else
at(object['s'], object['n'] / 1000)
end
diff --git a/lib/json/common.rb b/lib/json/common.rb
index 43e249c..cf7a8b9 100644
--- a/lib/json/common.rb
+++ b/lib/json/common.rb
@@ -284,22 +284,40 @@ module JSON
module_function :pretty_unparse
# :startdoc:
+ class << self
+ # The global default options for the JSON.load method:
+ # :max_nesting: false
+ # :allow_nan: true
+ # :quirks_mode: true
+ attr_accessor :load_default_options
+ end
+ self.load_default_options = {
+ :max_nesting => false,
+ :allow_nan => true,
+ :quirks_mode => true,
+ }
+
# Load a ruby data structure from a JSON _source_ and return it. A source can
# either be a string-like object, an IO-like object, or an object responding
# to the read method. If _proc_ was given, it will be called with any nested
- # Ruby object as an argument recursively in depth first order.
+ # Ruby object as an argument recursively in depth first order. The default
+ # options for the parser can be changed via the load_default_options method.
#
# This method is part of the implementation of the load/dump interface of
# Marshal and YAML.
def load(source, proc = nil)
+ opts = load_default_options
if source.respond_to? :to_str
source = source.to_str
elsif source.respond_to? :to_io
source = source.to_io.read
- else
+ elsif source.respond_to?(:read)
source = source.read
end
- result = parse(source, :max_nesting => false, :allow_nan => true)
+ if opts[:quirks_mode] && (source.nil? || source.empty?)
+ source = 'null'
+ end
+ result = parse(source, opts)
recurse_proc(result, &proc) if proc
result
end
@@ -321,6 +339,19 @@ module JSON
alias restore load
module_function :restore
+ class << self
+ # The global default options for the JSON.dump method:
+ # :max_nesting: false
+ # :allow_nan: true
+ # :quirks_mode: true
+ attr_accessor :dump_default_options
+ end
+ self.dump_default_options = {
+ :max_nesting => false,
+ :allow_nan => true,
+ :quirks_mode => true,
+ }
+
# Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
# the result.
#
@@ -331,6 +362,9 @@ module JSON
# exception is raised. This argument is similar (but not exactly the
# same!) to the _limit_ argument in Marshal.dump.
#
+ # The default options for the generator can be changed via the
+ # dump_default_options method.
+ #
# This method is part of the implementation of the load/dump interface of
# Marshal and YAML.
def dump(obj, anIO = nil, limit = nil)
@@ -341,8 +375,9 @@ module JSON
anIO = nil
end
end
- limit ||= 0
- result = generate(obj, :allow_nan => true, :max_nesting => limit)
+ opts = JSON.dump_default_options
+ limit and opts.update(:max_nesting => limit)
+ result = generate(obj, opts)
if anIO
anIO.write result
anIO
diff --git a/lib/json/ext.rb b/lib/json/ext.rb
index 7264a85..c5f8131 100644
--- a/lib/json/ext.rb
+++ b/lib/json/ext.rb
@@ -1,3 +1,9 @@
+if ENV['SIMPLECOV_COVERAGE'].to_i == 1
+ require 'simplecov'
+ SimpleCov.start do
+ add_filter "/tests/"
+ end
+end
require 'json/common'
module JSON
diff --git a/lib/json/pure.rb b/lib/json/pure.rb
index dbac93c..b68668b 100644
--- a/lib/json/pure.rb
+++ b/lib/json/pure.rb
@@ -1,3 +1,9 @@
+if ENV['SIMPLECOV_COVERAGE'].to_i == 1
+ require 'simplecov'
+ SimpleCov.start do
+ add_filter "/tests/"
+ end
+end
require 'json/common'
require 'json/pure/parser'
require 'json/pure/generator'
diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb
index 7c9b2ad..9141ae5 100644
--- a/lib/json/pure/generator.rb
+++ b/lib/json/pure/generator.rb
@@ -136,14 +136,15 @@ module JSON
# * *quirks_mode*: Enables quirks_mode for parser, that is for example
# generating single JSON values instead of documents is possible.
def initialize(opts = {})
- @indent = ''
- @space = ''
- @space_before = ''
- @object_nl = ''
- @array_nl = ''
- @allow_nan = false
- @ascii_only = false
- @quirks_mode = false
+ @indent = ''
+ @space = ''
+ @space_before = ''
+ @object_nl = ''
+ @array_nl = ''
+ @allow_nan = false
+ @ascii_only = false
+ @quirks_mode = false
+ @buffer_initial_length = 1024
configure opts
end
@@ -172,6 +173,16 @@ module JSON
# it's disabled.
attr_accessor :quirks_mode
+ # :stopdoc:
+ attr_reader :buffer_initial_length
+
+ def buffer_initial_length=(length)
+ if length > 0
+ @buffer_initial_length = length
+ end
+ end
+ # :startdoc:
+
# This integer returns the current depth data structure nesting in the
# generated JSON.
attr_accessor :depth
@@ -233,7 +244,7 @@ module JSON
# passed to the configure method.
def to_h
result = {}
- for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only quirks_mode depth]
+ for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only quirks_mode buffer_initial_length depth]
result[iv.intern] = instance_variable_get("@#{iv}")
end
result
diff --git a/lib/json/pure/parser.rb b/lib/json/pure/parser.rb
index e24aac1..84eb67f 100644
--- a/lib/json/pure/parser.rb
+++ b/lib/json/pure/parser.rb
@@ -73,7 +73,7 @@ module JSON
def initialize(source, opts = {})
opts ||= {}
unless @quirks_mode = opts[:quirks_mode]
- source = determine_encoding source
+ source = convert_encoding source
end
super source
if !opts.key?(:max_nesting) # defaults to 19
@@ -145,7 +145,12 @@ module JSON
private
- def determine_encoding(source)
+ def convert_encoding(source)
+ if source.respond_to?(:to_str)
+ source = source.to_str
+ else
+ raise TypeError, "#{source.inspect} is not like a string"
+ end
if defined?(::Encoding)
if source.encoding == ::Encoding::ASCII_8BIT
b = source[0, 4].bytes.to_a
diff --git a/lib/json/version.rb b/lib/json/version.rb
index 5983945..9272c53 100644
--- a/lib/json/version.rb
+++ b/lib/json/version.rb
@@ -1,6 +1,6 @@
module JSON
# JSON version
- VERSION = '1.6.1'
+ VERSION = '1.6.3'
VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
VERSION_MINOR = VERSION_ARRAY[1] # :nodoc: