From 4ed82ae5cb15646272947a33de7714bd852f80a3 Mon Sep 17 00:00:00 2001 From: tenderlove Date: Mon, 21 Feb 2011 18:09:38 +0000 Subject: * ext/psych/lib/psych/streaming.rb: refactor streaming methods to a module. * ext/psych/lib/psych/stream.rb: extracted streaming specific methods to a module. * ext/psych/lib/psych/json/stream.rb: JSON stream inherits from JSONTree and includes streaming methods. * ext/psych/lib/psych/visitors/json_tree.rb: JSON does not support object references, so remove object reference testing when building JSON trees. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30936 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/psych/lib/psych.rb | 1 + ext/psych/lib/psych/json/stream.rb | 3 ++- ext/psych/lib/psych/stream.rb | 19 +------------------ ext/psych/lib/psych/streaming.rb | 22 ++++++++++++++++++++++ ext/psych/lib/psych/visitors/json_tree.rb | 8 ++++++++ 5 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 ext/psych/lib/psych/streaming.rb (limited to 'ext/psych/lib') diff --git a/ext/psych/lib/psych.rb b/ext/psych/lib/psych.rb index 565b811deb..0cf4448599 100644 --- a/ext/psych/lib/psych.rb +++ b/ext/psych/lib/psych.rb @@ -1,5 +1,6 @@ require 'psych.so' require 'psych/nodes' +require 'psych/streaming' require 'psych/visitors' require 'psych/handler' require 'psych/tree_builder' diff --git a/ext/psych/lib/psych/json/stream.rb b/ext/psych/lib/psych/json/stream.rb index e568719dc8..be1a0a8a82 100644 --- a/ext/psych/lib/psych/json/stream.rb +++ b/ext/psych/lib/psych/json/stream.rb @@ -3,8 +3,9 @@ require 'psych/json/yaml_events' module Psych module JSON - class Stream < Psych::Stream + class Stream < Psych::Visitors::JSONTree include Psych::JSON::RubyEvents + include Psych::Streaming class Emitter < Psych::Stream::Emitter # :nodoc: include Psych::JSON::YAMLEvents diff --git a/ext/psych/lib/psych/stream.rb b/ext/psych/lib/psych/stream.rb index 508483e573..567c1bb790 100644 --- a/ext/psych/lib/psych/stream.rb +++ b/ext/psych/lib/psych/stream.rb @@ -31,23 +31,6 @@ module Psych end end - ### - # Create a new streaming emitter. Emitter will print to +io+. See - # Psych::Stream for an example. - def initialize io - super({}, self.class.const_get(:Emitter).new(io)) - end - - ### - # Start streaming using +encoding+ - def start encoding = Nodes::Stream::UTF8 - super.tap { yield self if block_given? } - ensure - finish if block_given? - end - - private - def register target, obj - end + include Psych::Streaming end end diff --git a/ext/psych/lib/psych/streaming.rb b/ext/psych/lib/psych/streaming.rb new file mode 100644 index 0000000000..c6fa109d5a --- /dev/null +++ b/ext/psych/lib/psych/streaming.rb @@ -0,0 +1,22 @@ +module Psych + module Streaming + ### + # Create a new streaming emitter. Emitter will print to +io+. See + # Psych::Stream for an example. + def initialize io + super({}, self.class.const_get(:Emitter).new(io)) + end + + ### + # Start streaming using +encoding+ + def start encoding = Nodes::Stream::UTF8 + super.tap { yield self if block_given? } + ensure + finish if block_given? + end + + private + def register target, obj + end + end +end diff --git a/ext/psych/lib/psych/visitors/json_tree.rb b/ext/psych/lib/psych/visitors/json_tree.rb index 292afae623..0350dd1faa 100644 --- a/ext/psych/lib/psych/visitors/json_tree.rb +++ b/ext/psych/lib/psych/visitors/json_tree.rb @@ -8,6 +8,14 @@ module Psych def initialize options = {}, emitter = Psych::JSON::TreeBuilder.new super end + + def accept target + if target.respond_to?(:encode_with) + dump_coder target + else + send(@dispatch_cache[target.class], target) + end + end end end end -- cgit v1.2.3