title: In Ruby, MongoDB returns a BSON::OrderedHash. How to I convert it to JSON? Using to_json gives a "stack level too deep" error
body: <p>I am trying to make a web service using Ruby Sinatra and MongoDB. It will return JSON objects. I decided to use MongoDB, in part, because it stores documents internally in a "JSON-like" structure. I thought this would make it easy to me to run a query and send a JSON result to the client. However, I have run into problems converting the results from MongoDB into JSON.</p>
  
  <p>MongoDB's find_one() returns a BSON::OrderedHash. From the <a href="http://api.mongodb.org/ruby/1.0.2/BSON/OrderedHash.html" rel="noreferrer">documentation</a> it seems like this should behave similar to Ruby's Hash type. When I try to convert it to JSON using the .to_json function, I get a "stack level too deep" error. Trying to convert an identical works just fine.</p>
  
  <p>This code works like I would expect:</p>
  
  <pre><code>require "json"
  
  my_hash = Hash.new
  my_hash[ "a" ] = "aaa"
  my_hash[ "b" ] = 9
  
  puts my_hash.to_json
  </code></pre>
  
  <p>This code produces `to_json': stack level too deep (SystemStackError):</p>
  
  <pre><code>require "json"
  require "bson"
  
  my_bson = BSON::OrderedHash.new
  my_bson[ "a" ] = "aaa"
  my_bson[ "b" ] = 9
  
  puts my_bson.to_json
  </code></pre>
  
  <p>Trying to convert to hash first didn't help. Same error.</p>
  
  <pre><code>puts my_bson.to_hash.to_json
  </code></pre>
  
  <p>Why am I getting the stack level too deep error? This is a simple hash. Is there an easy to to convert the MongoDB results to JSON? I don't want to write a conversion function that is specific to my data. That defeats the point of have a schemaless database.</p>