I have a Backbone model in my application that is not a typical flat object, it is a large nested object, and we store the nested parts in TEXT columns in the MySQL database.
I wanted to handle JSON encoding / decoding in the Rails API, so from the outside it looks like you can POST / GET this one large nested JSON object, even if parts of it are stored as compressed JSON text.
However, I ran into a problem when Rails magically converts empty arrays to nil values. For example, if I READ this:
{ name: "foo", surname: "bar", nested_json: { complicated: [] } }
My Rails controller sees this:
{ :name => "foo", :surname => "bar", :nested_json => { :complicated => nil } }
And so my JSON data has been changed.
Has anyone encountered this problem before? Why does Rails change my POST data?
UPDATE
This is where they do it:
https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/request.rb#L288
And thatโs why they do it:
https://github.com/rails/rails/pull/8862
So now the question is, what is the best way to handle this in my nested JSON API situation?
json ruby-on-rails
Karolis Feb 01 '13 at 13:48 2013-02-01 13:48
source share