title: Recursively dump Ruby object to json
body: <p>I have a hash that I need to dump to json, but one of its values is an object:</p>
  
  <p><code>hash = { a: my_object }</code></p>
  
  <p>If I try to dump this with something like <code>MultiJson.dump(hash)</code>, it will only serialize the top-level hash, and not any deeper, so what I end up with is</p>
  
  <p><code>'{"a": #&lt;...&gt;}'</code></p>
  
  <p>This happens even if the object has to_json, to_hash, etc. methods already, and <code>MultiJson.dump(my_object)</code> works just fine.</p>
  
  <p>This seems like something JSON libraries should do, but I guess they don't. Why not? Is there a programmatic reason they shouldn't/cant? Or am I just missing something?</p>
  
  <p>Edit:</p>
  
  <p>A bit more searching around -- I must have messed up something in my initial tests. Oj can dump json recursively with the :compat option: </p>
  
  <p><code>Oj.dump(my_object, mode: :compat)</code></p>
  
  <p>MultiJson using Oj passes this option in by default, so that will work too. Although, according to Oj documentation, I'm not sure why this works. It works even without any as_hash or to_json methods.</p>