This project is archived and is in readonly mode.
#to_json behavior still different between Rails 2.3.2.1 and Rails 2-3-stable
Reported by Cody Fauser | June 3rd, 2009 @ 04:17 PM | in 2.3.3
Ticket #2690 helped the problems with to_json serialization in the Rails 2.3 branch, but the behavior of to_json is still different between Rails 2.3.2.1 and Rails 2.3-stable. The following example illustrates the problem:
class A
def initialize
@a, @b = 1, 2
end
def to_json(options = {})
@a.to_json(options)
end
end
class B
def initialize
@a = A.new
end
end
In Rails 2.3.2.1 the output is the following:
b = B.new
b.to_json #=> {"a": 1}
In Rails 2.3-stable the output is:
b = B.new
b.to_json #=> {"a":{"a":1,"b":2}}
The problem is caused by ActiveSupport::JSON.encode calling rails_to_json instead of to_json on the objects. Overridden to_json() methods child objects are never called.
Going forward all objects will have to have a rails_to_json() method, but the problem is that this functionality changed in the stable 2-3 branch.
In Shopify we have monkey patched active_support/json/encoding.rb to call value.to_json() instead of value.rails_to_json() to achieve the same output from Rails 2.3.2.1.
Comments and changes to this ticket
-
Michael Koziarski June 3rd, 2009 @ 09:23 PM
- Assigned user set to Rick
Given this I think we have no choice but to revert the rails_to_json change from 2-3-stable and figure out a plan for deprecation.
It can remain in master of course.
-
Michael Koziarski June 6th, 2009 @ 05:05 AM
- Milestone changed from 2.x to 2.3.3
-
Jeremy Kemper June 7th, 2009 @ 12:32 AM
- State changed from new to open
- Assigned user changed from Rick to Jeremy Kemper
What a headache. Overriding to_json to provide custom JSON representations is a bad API decision. Instead we should be overriding a dump_json method to provide a Hash, Array, etc to encode. Then we don't have to walk the path of
class Custom def to_json(*strange_internal_state)
{ :alternate => 'representation' }.to_json(*strange_internal_state)
end end
class Custom def dump_json(options_hash)
{ :alternate => 'representation' }
end end -
Michael Koziarski June 7th, 2009 @ 04:44 AM
Sounds like an improvement for sure. Not sold on the name, but it
beats the 'return a json string' api that's very brittle.When you revert this from 2-3-stable move this ticket's milestone to 3.0
-
Repository June 8th, 2009 @ 09:24 PM
- State changed from open to resolved
(from [4a78dae2ab43b5ba94fa06b345e9edeb0305fe44]) Revert rails_to_json -> to_json so we don't break compatibility
[#2753 state:resolved] http://github.com/rails/rails/commit/4a78dae2ab43b5ba94fa06b345e9ed...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Referenced by
- 2753 #to_json behavior still different between Rails 2.3.2.1 and Rails 2-3-stable [#2753 state:resolved] http://github.com/rails/rails/com...