implementing as_json for object id to return structure of mongoDB extended json

This commit is contained in:
Ryan Fitzgerald 2010-10-29 05:42:54 -04:00 committed by Kyle Banker
parent 2188522687
commit 4a1bccefae
2 changed files with 13 additions and 0 deletions

View File

@ -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

View File

@ -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