diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/psych.rb | 5 | ||||
-rw-r--r-- | lib/psych/json/ruby_events.rb | 19 | ||||
-rw-r--r-- | lib/psych/json/stream.rb | 33 | ||||
-rw-r--r-- | lib/psych/json/tree_builder.rb | 26 | ||||
-rw-r--r-- | lib/psych/json/yaml_events.rb | 29 | ||||
-rw-r--r-- | lib/psych/stream.rb | 19 | ||||
-rw-r--r-- | lib/psych/streaming.rb | 22 | ||||
-rw-r--r-- | lib/psych/visitors/json_tree.rb | 22 | ||||
-rw-r--r-- | lib/psych/visitors/yaml_tree.rb | 9 |
9 files changed, 101 insertions, 83 deletions
diff --git a/lib/psych.rb b/lib/psych.rb index ba4a683..cb67561 100644 --- a/lib/psych.rb +++ b/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' @@ -9,6 +10,7 @@ require 'psych/set' require 'psych/coder' require 'psych/core_ext' require 'psych/deprecated' +require 'psych/json' ### # = Overview @@ -88,7 +90,7 @@ require 'psych/deprecated' module Psych # The version is Psych you're using - VERSION = '1.0.0' + VERSION = '1.1.0' # The version of libyaml Psych is using LIBYAML_VERSION = Psych.libyaml_version.join '.' @@ -97,7 +99,6 @@ module Psych end autoload :Stream, 'psych/stream' - autoload :JSON, 'psych/json' ### # Load +yaml+ in to a Ruby data structure. If multiple documents are diff --git a/lib/psych/json/ruby_events.rb b/lib/psych/json/ruby_events.rb new file mode 100644 index 0000000..6b73249 --- /dev/null +++ b/lib/psych/json/ruby_events.rb @@ -0,0 +1,19 @@ +module Psych + module JSON + module RubyEvents # :nodoc: + def visit_Time o + formatted = format_time o + @emitter.scalar formatted, nil, nil, false, true, Nodes::Scalar::DOUBLE_QUOTED + end + + def visit_DateTime o + visit_Time o.to_time + end + + def visit_String o + @emitter.scalar o.to_s, nil, nil, false, true, Nodes::Scalar::DOUBLE_QUOTED + end + alias :visit_Symbol :visit_String + end + end +end diff --git a/lib/psych/json/stream.rb b/lib/psych/json/stream.rb index a6da584..be1a0a8 100644 --- a/lib/psych/json/stream.rb +++ b/lib/psych/json/stream.rb @@ -1,32 +1,15 @@ +require 'psych/json/ruby_events' +require 'psych/json/yaml_events' + 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 + class Stream < Psych::Visitors::JSONTree + include Psych::JSON::RubyEvents + include Psych::Streaming - 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 + class Emitter < Psych::Stream::Emitter # :nodoc: + include Psych::JSON::YAMLEvents end - alias :visit_Symbol :visit_String end end end diff --git a/lib/psych/json/tree_builder.rb b/lib/psych/json/tree_builder.rb index 26fcb11..b799c93 100644 --- a/lib/psych/json/tree_builder.rb +++ b/lib/psych/json/tree_builder.rb @@ -1,32 +1,12 @@ +require 'psych/json/yaml_events' + module Psych module JSON ### # Psych::JSON::TreeBuilder is an event based AST builder. Events are sent # 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, !streaming?) - end - - def end_document implicit_end = !streaming? - super(implicit_end) - end - - def start_mapping anchor, tag, implicit, style - super(anchor, nil, 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 + include Psych::JSON::YAMLEvents end end end diff --git a/lib/psych/json/yaml_events.rb b/lib/psych/json/yaml_events.rb new file mode 100644 index 0000000..01d4660 --- /dev/null +++ b/lib/psych/json/yaml_events.rb @@ -0,0 +1,29 @@ +module Psych + module JSON + module YAMLEvents # :nodoc: + def start_document version, tag_directives, implicit + super(version, tag_directives, !streaming?) + end + + def end_document implicit_end = !streaming? + super(implicit_end) + end + + def start_mapping anchor, tag, implicit, style + super(anchor, nil, implicit, Nodes::Mapping::FLOW) + end + + def start_sequence anchor, tag, implicit, style + super(anchor, nil, 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/lib/psych/stream.rb b/lib/psych/stream.rb index 508483e..567c1bb 100644 --- a/lib/psych/stream.rb +++ b/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/lib/psych/streaming.rb b/lib/psych/streaming.rb new file mode 100644 index 0000000..c6fa109 --- /dev/null +++ b/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/lib/psych/visitors/json_tree.rb b/lib/psych/visitors/json_tree.rb index dd06e80..0350dd1 100644 --- a/lib/psych/visitors/json_tree.rb +++ b/lib/psych/visitors/json_tree.rb @@ -1,23 +1,21 @@ +require 'psych/json/ruby_events' + module Psych module Visitors class JSONTree < YAMLTree + include Psych::JSON::RubyEvents + def initialize options = {}, emitter = Psych::JSON::TreeBuilder.new super end - def visit_Time o - formatted = format_time o - @emitter.scalar formatted, nil, nil, false, true, Nodes::Scalar::DOUBLE_QUOTED - end - - def visit_DateTime o - visit_Time o.to_time - end - - def visit_String o - @emitter.scalar o.to_s, nil, nil, false, true, Nodes::Scalar::DOUBLE_QUOTED + def accept target + if target.respond_to?(:encode_with) + dump_coder target + else + send(@dispatch_cache[target.class], target) + end end - alias :visit_Symbol :visit_String end end end diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb index e7d182f..719720a 100644 --- a/lib/psych/visitors/yaml_tree.rb +++ b/lib/psych/visitors/yaml_tree.rb @@ -70,9 +70,12 @@ module Psych def accept target # return any aliases we find - if node = @st[target.object_id] - node.anchor = target.object_id.to_s - return @emitter.alias target.object_id.to_s + if @st.key? target.object_id + oid = target.object_id + node = @st[oid] + anchor = oid.to_s + node.anchor = anchor + return @emitter.alias anchor end if target.respond_to?(:to_yaml) |