From 044d6010fd22ede84b5afd93bce7c4f0ff97adbd Mon Sep 17 00:00:00 2001 From: tenderlove Date: Sat, 22 May 2010 22:12:39 +0000 Subject: * ext/psych/lib/psych/json/stream.rb: adding a JSON streaming API * ext/psych/lib/psych/stream.rb: ditto * ext/psych/lib/psych.rb: using autoload * ext/psych/lib/psych/json.rb: ditto * ext/psych/lib/psych/json/tree_builder.rb: refactor * ext/psych/lib/psych/visitors/json_tree.rb: refactor git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27961 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/psych/lib/psych/json.rb | 6 ++++++ ext/psych/lib/psych/json/stream.rb | 32 +++++++++++++++++++++++++++++++ ext/psych/lib/psych/json/tree_builder.rb | 12 ++++++++++-- ext/psych/lib/psych/stream.rb | 2 +- ext/psych/lib/psych/visitors/json_tree.rb | 15 --------------- 5 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 ext/psych/lib/psych/json.rb create mode 100644 ext/psych/lib/psych/json/stream.rb (limited to 'ext/psych/lib/psych') diff --git a/ext/psych/lib/psych/json.rb b/ext/psych/lib/psych/json.rb new file mode 100644 index 0000000000..412ab2708b --- /dev/null +++ b/ext/psych/lib/psych/json.rb @@ -0,0 +1,6 @@ +module Psych + module JSON + autoload :TreeBuilder, 'psych/json/tree_builder' + autoload :Stream, 'psych/json/stream' + end +end diff --git a/ext/psych/lib/psych/json/stream.rb b/ext/psych/lib/psych/json/stream.rb new file mode 100644 index 0000000000..a6da584cbf --- /dev/null +++ b/ext/psych/lib/psych/json/stream.rb @@ -0,0 +1,32 @@ +module Psych + module JSON + class Stream < Psych::Stream + class Emitter < Psych::Stream::Emitter # :nodoc: + def start_document version, tag_directives, implicit + super(version, tag_directives, !streaming?) + end + + def start_mapping anchor, tag, implicit, style + super(anchor, tag, implicit, Nodes::Mapping::FLOW) + end + + def start_sequence anchor, tag, implicit, style + super(anchor, tag, implicit, Nodes::Sequence::FLOW) + end + + def scalar value, anchor, tag, plain, quoted, style + if "tag:yaml.org,2002:null" == tag + super('null', nil, nil, true, false, Nodes::Scalar::PLAIN) + else + super + end + end + end + + def visit_String o + @emitter.scalar o.to_s, nil, nil, false, true, Nodes::Scalar::ANY + end + alias :visit_Symbol :visit_String + end + end +end diff --git a/ext/psych/lib/psych/json/tree_builder.rb b/ext/psych/lib/psych/json/tree_builder.rb index d0a76177bf..720ede7e1e 100644 --- a/ext/psych/lib/psych/json/tree_builder.rb +++ b/ext/psych/lib/psych/json/tree_builder.rb @@ -5,11 +5,11 @@ module Psych # to an instance of Psych::JSON::TreeBuilder and a JSON AST is constructed. class TreeBuilder < Psych::TreeBuilder def start_document version, tag_directives, implicit - super(version, tag_directives, true) + super(version, tag_directives, !streaming?) end def end_document implicit_end = !streaming? - super(true) + super(implicit_end) end def start_mapping anchor, tag, implicit, style @@ -19,6 +19,14 @@ module Psych def start_sequence anchor, tag, implicit, style super(anchor, tag, implicit, Nodes::Sequence::FLOW) end + + def scalar value, anchor, tag, plain, quoted, style + if "tag:yaml.org,2002:null" == tag + super('null', nil, nil, true, false, Nodes::Scalar::PLAIN) + else + super + end + end end end end diff --git a/ext/psych/lib/psych/stream.rb b/ext/psych/lib/psych/stream.rb index ac231aafdc..508483e573 100644 --- a/ext/psych/lib/psych/stream.rb +++ b/ext/psych/lib/psych/stream.rb @@ -35,7 +35,7 @@ module Psych # Create a new streaming emitter. Emitter will print to +io+. See # Psych::Stream for an example. def initialize io - super({}, Emitter.new(io)) + super({}, self.class.const_get(:Emitter).new(io)) end ### diff --git a/ext/psych/lib/psych/visitors/json_tree.rb b/ext/psych/lib/psych/visitors/json_tree.rb index 0ec1678a39..3502cdb63c 100644 --- a/ext/psych/lib/psych/visitors/json_tree.rb +++ b/ext/psych/lib/psych/visitors/json_tree.rb @@ -5,25 +5,10 @@ module Psych super end - def visit_NilClass o - @emitter.scalar 'null', nil, nil, true, false, Nodes::Scalar::PLAIN - end - - def visit_Integer o - @emitter.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 @emitter.scalar o.to_s, nil, nil, false, true, Nodes::Scalar::ANY end alias :visit_Symbol :visit_String - - private end end end -- cgit v1.2.3