support MongoDB extended JSON on ObjectID#to_json
This commit is contained in:
parent
05342ef8f2
commit
d71f7d4e3a
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue