mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/psych/lib/psych/visitors/json_tree.rb: using factory methods for
node creation * ext/psych/lib/psych/visitors/yaml_tree.rb: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27806 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d9615c85a3
commit
37910b13fb
2 changed files with 20 additions and 10 deletions
|
@ -1,16 +1,26 @@
|
|||
module Psych
|
||||
module Visitors
|
||||
class JSONTree < YAMLTree
|
||||
def visit_Symbol o
|
||||
append create_scalar o.to_s
|
||||
end
|
||||
|
||||
def visit_NilClass o
|
||||
scalar = Nodes::Scalar.new(
|
||||
scalar = create_scalar(
|
||||
'null', nil, nil, true, false, Nodes::Scalar::PLAIN)
|
||||
append scalar
|
||||
end
|
||||
|
||||
def visit_Integer o
|
||||
append create_scalar(o.to_s, nil, nil, true, false, Nodes::Scalar::PLAIN)
|
||||
end
|
||||
|
||||
def visit_Float o
|
||||
return super if o.nan? || o.infinite?
|
||||
visit_Integer o
|
||||
end
|
||||
|
||||
def visit_String o
|
||||
append create_scalar o.to_s
|
||||
end
|
||||
alias :visit_Symbol :visit_String
|
||||
|
||||
private
|
||||
def create_document
|
||||
doc = super
|
||||
|
@ -26,7 +36,7 @@ module Psych
|
|||
end
|
||||
|
||||
def create_scalar value, anchor = nil, tag = nil, plain = false, quoted = true, style = Nodes::Scalar::ANY
|
||||
super(value, anchor, tag, false, true, style)
|
||||
super
|
||||
end
|
||||
|
||||
def create_sequence anchor = nil, tag = nil, implicit = true, style = Nodes::Sequence::FLOW
|
||||
|
|
|
@ -151,7 +151,7 @@ module Psych
|
|||
end
|
||||
|
||||
def visit_Integer o
|
||||
append Nodes::Scalar.new o.to_s
|
||||
append create_scalar o.to_s
|
||||
end
|
||||
alias :visit_TrueClass :visit_Integer
|
||||
alias :visit_FalseClass :visit_Integer
|
||||
|
@ -159,11 +159,11 @@ module Psych
|
|||
|
||||
def visit_Float o
|
||||
if o.nan?
|
||||
append Nodes::Scalar.new '.nan'
|
||||
append create_scalar '.nan'
|
||||
elsif o.infinite?
|
||||
append Nodes::Scalar.new(o.infinite? > 0 ? '.inf' : '-.inf')
|
||||
append create_scalar(o.infinite? > 0 ? '.inf' : '-.inf')
|
||||
else
|
||||
append Nodes::Scalar.new o.to_s
|
||||
append create_scalar o.to_s
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue