[前][次][番号順一覧][スレッド一覧]

ruby-changes:16013

From: tenderlove <ko1@a...>
Date: Sun, 23 May 2010 07:12:55 +0900 (JST)
Subject: [ruby-changes:16013] Ruby:r27961 (trunk): * ext/psych/lib/psych/json/stream.rb: adding a JSON streaming API

tenderlove	2010-05-23 07:12:39 +0900 (Sun, 23 May 2010)

  New Revision: 27961

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=27961

  Log:
    * 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

  Added directories:
    trunk/test/psych/json/
  Added files:
    trunk/ext/psych/lib/psych/json/stream.rb
    trunk/ext/psych/lib/psych/json.rb
    trunk/test/psych/json/test_stream.rb
  Modified files:
    trunk/ChangeLog
    trunk/ext/psych/lib/psych/json/tree_builder.rb
    trunk/ext/psych/lib/psych/stream.rb
    trunk/ext/psych/lib/psych/visitors/json_tree.rb
    trunk/ext/psych/lib/psych.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 27960)
+++ ChangeLog	(revision 27961)
@@ -1,3 +1,18 @@
+Sun May 23 07:08:34 2010  Aaron Patterson <aaron@t...>
+
+	* 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
+	
+
 Sat May 22 03:53:05 2010  Satoshi Shiba  <shiba@r...>
 
 	* cont.c (fiber_setcontext): Use swapcontext() instead longjmp().
Index: ext/psych/lib/psych/visitors/json_tree.rb
===================================================================
--- ext/psych/lib/psych/visitors/json_tree.rb	(revision 27960)
+++ ext/psych/lib/psych/visitors/json_tree.rb	(revision 27961)
@@ -5,25 +5,10 @@
         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
Index: ext/psych/lib/psych/stream.rb
===================================================================
--- ext/psych/lib/psych/stream.rb	(revision 27960)
+++ ext/psych/lib/psych/stream.rb	(revision 27961)
@@ -35,7 +35,7 @@
     # 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
 
     ###
Index: ext/psych/lib/psych/json/tree_builder.rb
===================================================================
--- ext/psych/lib/psych/json/tree_builder.rb	(revision 27960)
+++ ext/psych/lib/psych/json/tree_builder.rb	(revision 27961)
@@ -5,11 +5,11 @@
     # 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 @@
       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
Index: ext/psych/lib/psych/json/stream.rb
===================================================================
--- ext/psych/lib/psych/json/stream.rb	(revision 0)
+++ ext/psych/lib/psych/json/stream.rb	(revision 27961)
@@ -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
Index: ext/psych/lib/psych/json.rb
===================================================================
--- ext/psych/lib/psych/json.rb	(revision 0)
+++ ext/psych/lib/psych/json.rb	(revision 27961)
@@ -0,0 +1,6 @@
+module Psych
+  module JSON
+    autoload :TreeBuilder, 'psych/json/tree_builder'
+    autoload :Stream, 'psych/json/stream'
+  end
+end
Index: ext/psych/lib/psych.rb
===================================================================
--- ext/psych/lib/psych.rb	(revision 27960)
+++ ext/psych/lib/psych.rb	(revision 27961)
@@ -3,7 +3,6 @@
 require 'psych/visitors'
 require 'psych/handler'
 require 'psych/tree_builder'
-require 'psych/json/tree_builder'
 require 'psych/parser'
 require 'psych/omap'
 require 'psych/set'
@@ -98,6 +97,7 @@
   end
 
   autoload :Stream, 'psych/stream'
+  autoload :JSON, 'psych/json'
 
   ###
   # Load +yaml+ in to a Ruby data structure.  If multiple documents are
Index: test/psych/json/test_stream.rb
===================================================================
--- test/psych/json/test_stream.rb	(revision 0)
+++ test/psych/json/test_stream.rb	(revision 27961)
@@ -0,0 +1,75 @@
+require_relative '../helper'
+
+module Psych
+  module JSON
+    class TestStream < TestCase
+      def setup
+        @io     = StringIO.new
+        @stream = Psych::JSON::Stream.new(@io)
+        @stream.start
+      end
+
+      def test_explicit_documents
+        @io     = StringIO.new
+        @stream = Psych::JSON::Stream.new(@io)
+        @stream.start
+
+        @stream.push({ 'foo' => 'bar' })
+
+        assert !@stream.finished?, 'stream not finished'
+        @stream.finish
+        assert @stream.finished?, 'stream finished'
+
+        assert_match(/^---/, @io.string)
+        assert_match(/\.\.\.$/, @io.string)
+      end
+
+      def test_null
+        @stream.push(nil)
+        assert_match(/^--- null/, @io.string)
+      end
+
+      def test_string
+        @stream.push "foo"
+        assert_match(/(['"])foo\1/, @io.string)
+      end
+
+      def test_symbol
+        @stream.push :foo
+        assert_match(/(['"])foo\1/, @io.string)
+      end
+
+      def test_int
+        @stream.push 10
+        assert_match(/^--- 10/, @io.string)
+      end
+
+      def test_float
+        @stream.push 1.2
+        assert_match(/^--- 1.2/, @io.string)
+      end
+
+      def test_hash
+        hash = { 'one' => 'two' }
+        @stream.push hash
+
+        json = @io.string
+        assert_match(/}$/, json)
+        assert_match(/^--- \{/, json)
+        assert_match(/['"]one['"]/, json)
+        assert_match(/['"]two['"]/, json)
+      end
+
+      def test_list_to_json
+        list = %w{ one two }
+        @stream.push list
+
+        json = @io.string
+        assert_match(/]$/, json)
+        assert_match(/^--- \[/, json)
+        assert_match(/['"]one['"]/, json)
+        assert_match(/['"]two['"]/, json)
+      end
+    end
+  end
+end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]