Skip to content

Commit

Permalink
Merge branch 'vipulnsward-json-transform-hash-perf'
Browse files Browse the repository at this point in the history
  • Loading branch information
hsbt committed Dec 1, 2023
2 parents c065433 + e397db4 commit 5ab53a4
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions lib/json/pure/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def configure(opts)
else
raise TypeError, "can't convert #{opts.class} into Hash"
end
for key, value in opts
opts.each do |key, value|
instance_variable_set "@#{key}", value
end
@indent = opts[:indent] if opts.key?(:indent)
Expand Down Expand Up @@ -260,7 +260,7 @@ def configure(opts)
# passed to the configure method.
def to_h
result = {}
for iv in instance_variables
instance_variables.each do |iv|
iv = iv.to_s[1..-1]
result[iv.to_sym] = self[iv]
end
Expand Down Expand Up @@ -327,20 +327,15 @@ def json_shift(state)
end

def json_transform(state)
delim = ','
delim << state.object_nl
result = '{'
result << state.object_nl
delim = ",#{state.object_nl}"
result = "{#{state.object_nl}"
depth = state.depth += 1
first = true
indent = !state.object_nl.empty?
each { |key,value|
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 = "#{result}#{key.to_s.to_json(state)}#{state.space_before}:#{state.space}"
if value.respond_to?(:to_json)
result << value.to_json(state)
else
Expand Down

0 comments on commit 5ab53a4

Please sign in to comment.