diff --git a/lib/bson/types/object_id.rb b/lib/bson/types/object_id.rb index 842697d..50968e3 100644 --- a/lib/bson/types/object_id.rb +++ b/lib/bson/types/object_id.rb @@ -152,6 +152,14 @@ module BSON def to_json(*a) "{\"$oid\": \"#{to_s}\"}" end + + # Create the JSON hash structure convert to MongoDB extended format. Rails 2.3.3 + # introduced as_json to create the needed hash structure to encode objects into JSON. + # + # @return [Hash] the hash representation as MongoDB extended JSON + def as_json(options ={}) + {"$oid" => to_s} + end # Return the UTC time at which this ObjectId was generated. This may # be used in lieu of a created_at timestamp since this information diff --git a/test/bson/object_id_test.rb b/test/bson/object_id_test.rb index 181a8cf..01e076a 100644 --- a/test/bson/object_id_test.rb +++ b/test/bson/object_id_test.rb @@ -127,4 +127,9 @@ class ObjectIdTest < Test::Unit::TestCase id = ObjectId.new assert_equal "{\"$oid\": \"#{id}\"}", id.to_json end + + def test_as_json + id = ObjectId.new + assert_equal({"$oid" => id.to_s}, id.as_json) + end end