Skip to content

Commit

Permalink
Fix JSON serialization of ObjectIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacques Crocker committed Jul 29, 2010
1 parent 5992037 commit dffffa9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/mongoid/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,10 @@ class TrueClass #:nodoc

class BSON::ObjectID #:nodoc
extend Mongoid::Extensions::ObjectID::Conversions

unless self.new.respond_to?(:as_json)
def as_json(options = nil)
to_s
end
end
end
13 changes: 12 additions & 1 deletion spec/integration/mongoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -553,15 +553,26 @@
it "returns the json string" do
@person.to_json.should include('"pets":false')
end

it "should return the id field correctly" do
@person.to_json.should include('"_id":"'+@person.id.to_s+'"')
end
end

context "on a persisted document" do
before do
@person.save
end

it "returns the json string" do
@person.save
from_db = Person.find(@person.id)
from_db.to_json.should include('"pets":false')
end

it "should return the id field correctly" do
@person.to_json.should include('"_id":"'+@person.id.to_s+'"')
end

end
end

Expand Down

0 comments on commit dffffa9

Please sign in to comment.