implementing as_json for object id to return structure of mongoDB extended json
This commit is contained in:
parent
2188522687
commit
4a1bccefae
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue