ruby-changes:18586
From: tenderlove <ko1@a...>
Date: Thu, 20 Jan 2011 09:29:38 +0900 (JST)
Subject: [ruby-changes:18586] Ruby:r30610 (trunk): * ext/psych/lib/psych/visitors/json_tree.rb: Fix JSON emit for
tenderlove 2011-01-20 09:20:57 +0900 (Thu, 20 Jan 2011) New Revision: 30610 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=30610 Log: * ext/psych/lib/psych/visitors/json_tree.rb: Fix JSON emit for DateTime and Time classes. * test/psych/test_json_tree.rb: test for JSON emit Modified files: trunk/ChangeLog trunk/ext/psych/lib/psych/visitors/json_tree.rb trunk/test/psych/test_json_tree.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 30609) +++ ChangeLog (revision 30610) @@ -1,3 +1,10 @@ +Thu Jan 20 09:19:42 2011 Aaron Patterson <aaron@t...> + + * ext/psych/lib/psych/visitors/json_tree.rb: Fix JSON emit for + DateTime and Time classes. + + * test/psych/test_json_tree.rb: test for JSON emit + Thu Jan 20 08:02:46 2011 Aaron Patterson <aaron@t...> * ext/psych/lib/psych/coder.rb (represent_object): arbitrary objects Index: ext/psych/lib/psych/visitors/json_tree.rb =================================================================== --- ext/psych/lib/psych/visitors/json_tree.rb (revision 30609) +++ ext/psych/lib/psych/visitors/json_tree.rb (revision 30610) @@ -5,6 +5,15 @@ 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 end Index: test/psych/test_json_tree.rb =================================================================== --- test/psych/test_json_tree.rb (revision 30609) +++ test/psych/test_json_tree.rb (revision 30610) @@ -39,5 +39,15 @@ assert_match(/"one"/, json) assert_match(/"two"/, json) end + + def test_time + time = Time.new(2010, 10, 10).utc + assert_equal "{\"a\": \"2010-10-10 07:00:00.000000000Z\"}\n", Psych.to_json({'a' => time }) + end + + def test_datetime + time = Time.new(2010, 10, 10).to_datetime + assert_equal "{\"a\": \"#{time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")}\"}\n", Psych.to_json({'a' => time }) + end end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/