From 37910b13fbc1d6831b5623368472497629b36ae9 Mon Sep 17 00:00:00 2001 From: tenderlove Date: Sun, 16 May 2010 02:33:05 +0000 Subject: [PATCH] * 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 --- ext/psych/lib/psych/visitors/json_tree.rb | 22 ++++++++++++++++------ ext/psych/lib/psych/visitors/yaml_tree.rb | 8 ++++---- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/ext/psych/lib/psych/visitors/json_tree.rb b/ext/psych/lib/psych/visitors/json_tree.rb index 0440dc778d..774b043ba4 100644 --- a/ext/psych/lib/psych/visitors/json_tree.rb +++ b/ext/psych/lib/psych/visitors/json_tree.rb @@ -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 diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb index 4c8ba295a3..608da6d1a0 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -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