From 8ab4e77a8194d90d4731a425172444b1c8b817f9 Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Sat, 5 Dec 2009 10:54:36 -0500 Subject: [PATCH] Added to_json for Mongo::ObjectID. --- lib/mongo/types/objectid.rb | 4 ++++ test/test_objectid.rb | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/mongo/types/objectid.rb b/lib/mongo/types/objectid.rb index 8bb9c67..6afd016 100644 --- a/lib/mongo/types/objectid.rb +++ b/lib/mongo/types/objectid.rb @@ -100,6 +100,10 @@ module Mongo end def inspect; to_s; end + + def to_json(*args) + %Q("#{to_s}") + end # Get a string representation of this ObjectID using the legacy byte # ordering. This method may eventually be removed. If you are not sure diff --git a/test/test_objectid.rb b/test/test_objectid.rb index 0d37a73..107930b 100644 --- a/test/test_objectid.rb +++ b/test/test_objectid.rb @@ -1,4 +1,4 @@ -require 'test/test_helper' +require 'test_helper' class ObjectIDTest < Test::Unit::TestCase @@ -139,4 +139,9 @@ class ObjectIDTest < Test::Unit::TestCase assert_in_delta time.to_i, id.generation_time.to_i, 2 end + + def test_json + id = ObjectID.new + assert_equal %Q("#{id}"), id.to_json + end end