This is more or less what MultiJson is doing: https://github.com/intridea/multi_json/blob/master/lib/multi_json.rb#L154-L159