summaryrefslogtreecommitdiff
path: root/lib/json
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2011-07-27 02:14:54 +0200
committerFlorian Frank <flori@ping.de>2011-07-27 02:40:10 +0200
commitbdb2b910a5ebd92471de13d7e90d54408f6b093e (patch)
treeae717c3fa4721733def0fe5b450fd350380814ce /lib/json
parent9320a2a18a4d09f8e8a83129db793057968928b1 (diff)
downloadjson-bdb2b910a5ebd92471de13d7e90d54408f6b093e.tar.gz
started quirks mode for generator
Diffstat (limited to 'lib/json')
-rw-r--r--lib/json/common.rb18
-rw-r--r--lib/json/pure/generator.rb17
2 files changed, 30 insertions, 5 deletions
diff --git a/lib/json/common.rb b/lib/json/common.rb
index 1a5f048..1330acc 100644
--- a/lib/json/common.rb
+++ b/lib/json/common.rb
@@ -198,7 +198,11 @@ module JSON
# amount of sanity checks, and the pretty_generate method for some
# defaults for a pretty output.
def generate(obj, opts = nil)
- state = SAFE_STATE_PROTOTYPE.dup
+ if State === opts
+ state, opts = opts, nil
+ else
+ state = SAFE_STATE_PROTOTYPE.dup
+ end
if opts
if opts.respond_to? :to_hash
opts = opts.to_hash
@@ -225,7 +229,11 @@ module JSON
# *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, opts = nil)
- state = FAST_STATE_PROTOTYPE.dup
+ if State === opts
+ state, opts = opts, nil
+ else
+ state = FAST_STATE_PROTOTYPE.dup
+ end
if opts
if opts.respond_to? :to_hash
opts = opts.to_hash
@@ -252,7 +260,11 @@ module JSON
# The _opts_ argument can be used to configure the generator, see the
# generate method for a more detailed explanation.
def pretty_generate(obj, opts = nil)
- state = PRETTY_STATE_PROTOTYPE.dup
+ if State === opts
+ state, opts = opts, nil
+ else
+ state = PRETTY_STATE_PROTOTYPE.dup
+ end
if opts
if opts.respond_to? :to_hash
opts = opts.to_hash
diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb
index 3687e30..59df74d 100644
--- a/lib/json/pure/generator.rb
+++ b/lib/json/pure/generator.rb
@@ -141,6 +141,7 @@ module JSON
@array_nl = ''
@allow_nan = false
@ascii_only = false
+ @quirks_mode = false
configure opts
end
@@ -165,6 +166,10 @@ module JSON
# the generated JSON, max_nesting = 0 if no maximum is checked.
attr_accessor :max_nesting
+ # If this attribute is set to true, quirks mode is enabled, otherwise
+ # it's disabled.
+ attr_accessor :quirks_mode
+
# This integer returns the current depth data structure nesting in the
# generated JSON.
attr_accessor :depth
@@ -188,10 +193,17 @@ module JSON
@allow_nan
end
+ # Returns true, if only ASCII characters should be generated. Otherwise
+ # returns false.
def ascii_only?
@ascii_only
end
+ # Returns true, if quirks mode is enabled. Otherwise returns false.
+ def quirks_mode?
+ @quirks_mode
+ end
+
# Configure this State instance with the Hash _opts_, and return
# itself.
def configure(opts)
@@ -203,6 +215,7 @@ module JSON
@allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
@depth = opts[:depth] || 0
+ @quirks_mode = opts[:quirks_mode] if opts.key?(:quirks_mode)
if !opts.key?(:max_nesting) # defaults to 19
@max_nesting = 19
elsif opts[:max_nesting]
@@ -218,7 +231,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 depth]
+ for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only quirks_mode depth]
result[iv.intern] = instance_variable_get("@#{iv}")
end
result
@@ -229,7 +242,7 @@ module JSON
# GeneratorError exception.
def generate(obj)
result = obj.to_json(self)
- if result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m
+ if !@quirks_mode && result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m
raise GeneratorError, "only generation of JSON objects or arrays allowed"
end
result