summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog7
-rw-r--r--ext/psych/lib/psych/visitors/json_tree.rb9
-rw-r--r--test/psych/test_json_tree.rb10
3 files changed, 26 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index bdea51f839..63d024581a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Thu Jan 20 09:19:42 2011 Aaron Patterson <aaron@tenderlovemaking.com>
+
+ * 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@tenderlovemaking.com>
* ext/psych/lib/psych/coder.rb (represent_object): arbitrary objects
diff --git a/ext/psych/lib/psych/visitors/json_tree.rb b/ext/psych/lib/psych/visitors/json_tree.rb
index f2dbfb33d7..dd06e80607 100644
--- a/ext/psych/lib/psych/visitors/json_tree.rb
+++ b/ext/psych/lib/psych/visitors/json_tree.rb
@@ -5,6 +5,15 @@ module Psych
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
diff --git a/test/psych/test_json_tree.rb b/test/psych/test_json_tree.rb
index 96e17be961..5fcb0d6a37 100644
--- a/test/psych/test_json_tree.rb
+++ b/test/psych/test_json_tree.rb
@@ -39,5 +39,15 @@ module Psych
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