summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDaniel Luz <dev@mernen.com>2010-08-09 04:57:56 -0300
committerDaniel Luz <dev@mernen.com>2010-08-09 05:02:56 -0300
commit9acbc624e599ed8a865c0dedfa4378526c123526 (patch)
tree7ea8b67d1f7666ae27d1987bf3bc89352babd25c /lib
parent914cca595ccaed53da6d94698c6095fed3648f19 (diff)
parente8ef17ece7ddacbe7dde00ec3cb8a96401e1882c (diff)
downloadjson-9acbc624e599ed8a865c0dedfa4378526c123526.tar.gz
Merge branch 'master' (early part) into unified
Diffstat (limited to 'lib')
-rw-r--r--lib/json/pure/generator.rb132
1 files changed, 57 insertions, 75 deletions
diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb
index 12959bf..335c6ae 100644
--- a/lib/json/pure/generator.rb
+++ b/lib/json/pure/generator.rb
@@ -112,7 +112,7 @@ module JSON
when Hash
new(opts)
else
- SAFE_STATE_PROTOTYPE
+ SAFE_STATE_PROTOTYPE.dup
end
end
@@ -167,7 +167,7 @@ module JSON
# generated JSON.
attr_accessor :depth
- def check_max_nesting(depth) # :nodoc:
+ def check_max_nesting # :nodoc:
return if @max_nesting.zero?
current_nesting = depth + 1
current_nesting > @max_nesting and
@@ -252,51 +252,41 @@ module JSON
# _state_ is a JSON::State object, that can also be used to configure the
# produced JSON string output further.
# _depth_ is used to find out nesting depth, to indent accordingly.
- def to_json(state = nil, depth = 0, *)
- if state
- state = State.from_state(state)
- state.check_max_nesting(depth)
- end
- json_transform(state, depth)
+ def to_json(state = nil, *)
+ state = State.from_state(state)
+ state.check_max_nesting
+ json_transform(state)
end
private
- def json_shift(state, depth)
- state and not state.object_nl.empty? or return ''
- state.indent * depth
+ def json_shift(state)
+ state.object_nl.empty? or return ''
+ state.indent * state.depth
end
- def json_transform(state, depth)
+ def json_transform(state)
delim = ','
- if state
- delim << state.object_nl
- result = '{'
- result << state.object_nl
- depth += 1
- first = true
- indent = state && !state.object_nl.empty?
- each { |key,value|
- result << delim unless first
- result << state.indent * depth if indent
- result << key.to_s.to_json(state, depth)
- result << state.space_before
- result << ':'
- result << state.space
- result << value.to_json(state, depth)
- first = false
- }
- depth -= 1
- result << state.object_nl
- result << state.indent * depth if indent if indent
- result << '}'
- else
- result = '{'
- result << map { |key,value|
- key.to_s.to_json << ':' << value.to_json
- }.join(delim)
- result << '}'
- end
+ delim << state.object_nl
+ result = '{'
+ result << state.object_nl
+ depth = state.depth += 1
+ first = true
+ indent = !state.object_nl.empty?
+ each { |key,value|
+ result << delim unless first
+ result << state.indent * depth if indent
+ result << key.to_s.to_json(state)
+ result << state.space_before
+ result << ':'
+ result << state.space
+ result << value.to_json(state)
+ first = false
+ }
+ depth = state.depth -= 1
+ result << state.object_nl
+ result << state.indent * depth if indent if indent
+ result << '}'
result
end
end
@@ -306,39 +296,32 @@ module JSON
# this Array instance.
# _state_ is a JSON::State object, that can also be used to configure the
# produced JSON string output further.
- # _depth_ is used to find out nesting depth, to indent accordingly.
- def to_json(state = nil, depth = 0, *)
- if state
- state = State.from_state(state)
- state.check_max_nesting(depth)
- end
- json_transform(state, depth)
+ def to_json(state = nil, *)
+ state = State.from_state(state)
+ state.check_max_nesting
+ json_transform(state)
end
private
- def json_transform(state, depth)
+ def json_transform(state)
delim = ','
- if state
- delim << state.array_nl
- result = '['
- result << state.array_nl
- depth += 1
- first = true
- indent = state && !state.array_nl.empty?
- each { |value|
- result << delim unless first
- result << state.indent * depth if indent
- result << value.to_json(state, depth)
- first = false
- }
- depth -= 1
- result << state.array_nl
+ delim << state.array_nl
+ result = '['
+ result << state.array_nl
+ depth = state.depth += 1
+ first = true
+ indent = !state.array_nl.empty?
+ each { |value|
+ result << delim unless first
result << state.indent * depth if indent
- result << ']'
- else
- '[' << map { |value| value.to_json }.join(delim) << ']'
- end
+ result << value.to_json(state)
+ first = false
+ }
+ depth = state.depth -= 1
+ result << state.array_nl
+ result << state.indent * depth if indent
+ result << ']'
end
end
@@ -350,15 +333,16 @@ module JSON
module Float
# Returns a JSON string representation for this Float number.
def to_json(state = nil, *)
+ state = State.from_state(state)
case
when infinite?
- if state && state.allow_nan?
+ if state.allow_nan?
to_s
else
raise GeneratorError, "#{self} not allowed in JSON"
end
when nan?
- if state && state.allow_nan?
+ if state.allow_nan?
to_s
else
raise GeneratorError, "#{self} not allowed in JSON"
@@ -374,9 +358,8 @@ module JSON
# This string should be encoded with UTF-8 A call to this method
# returns a JSON string encoded with UTF16 big endian characters as
# \u????.
- def to_json(*args)
- state, = *args
- state ||= State.from_state(state)
+ def to_json(state = nil, *args)
+ state = State.from_state(state)
if encoding == ::Encoding::UTF_8
string = self
else
@@ -392,9 +375,8 @@ module JSON
# This string should be encoded with UTF-8 A call to this method
# returns a JSON string encoded with UTF16 big endian characters as
# \u????.
- def to_json(*args)
- state, = *args
- state ||= State.from_state(state)
+ def to_json(state = nil, *args)
+ state = State.from_state(state)
if state.ascii_only?
'"' << JSON.utf8_to_json_ascii(self) << '"'
else