support MongoDB extended JSON on ObjectID#to_json

This commit is contained in:
Kyle Banker 2010-01-22 14:56:00 -05:00
parent 05342ef8f2
commit d71f7d4e3a
2 changed files with 7 additions and 3 deletions

View File

@ -142,8 +142,12 @@ module Mongo
end end
alias_method :inspect, :to_s alias_method :inspect, :to_s
def to_json(*args) # Convert to MongoDB extended JSON format. Since JSON includes type information,
%Q("#{to_s}") # but lacks an ObjectID type, this JSON format encodes the type using an $id key.
#
# @return [String] the object id represented as MongoDB extended JSON.
def to_json(escaped=false)
"{\"$oid\": \"#{to_s}\"}"
end end
# @deprecated # @deprecated

View File

@ -151,6 +151,6 @@ class ObjectIDTest < Test::Unit::TestCase
def test_json def test_json
id = ObjectID.new id = ObjectID.new
assert_equal %Q("#{id}"), id.to_json assert_equal "{\"$oid\": \"#{id}\"}", id.to_json
end end
end end