This project is archived and is in readonly mode.
JSON serialization is broken
Reported by Kent Sibilev | September 2nd, 2010 @ 06:32 AM | in 3.x
JSON serialization is broken in 3.0.0 when I try to serialize a hash which includes an ActiveModel object and passing no-nil options parameter, like so:
options = {}
{:model => model_obj, :some_hash =>
sub_hash}.to_json(options)
The result is that sub_hash object is serialized as an empty hash and options parameter is modified to contain:
options = {:only => [], :except =[]}
The reason is that activemodel/lib/active_model/serialization.rb#serializable_hash modifies passed parameter, which it shouldn't.
Comments and changes to this ticket
-
José Valim September 2nd, 2010 @ 06:36 AM
- Milestone set to 3.x
- Assigned user cleared.
- Importance changed from to Low
Patch with tests please.
-
Kent Sibilev September 2nd, 2010 @ 07:39 AM
I've just sent a pull request with my proposed patch. The test to reproduce the problem is below:
require 'rubygems' require 'active_model' require 'active_support/core_ext' require 'active_support/json' class User include ActiveModel::Serialization include ActiveModel::Serializers::JSON attr_accessor :name def attributes @attributes ||= {'name' => 'nil'} end end u = User.new u.name = 'kent' json = { :user => u, :x => { :name => 1 } }.to_json puts json # => {"user":{"user":{"name":"kent"}},"x":{"name":1}} options = {} json = { :user => u, :x => { :name => 1 } }.to_json( options ) puts json # => {"user":{"user":{"name":"kent"}},"x":{}} p options # => {:except=>[], :only=>[]}
My ignorance prevents me from implementing proper test cases. Sorry.
-
José Valim September 2nd, 2010 @ 07:45 AM
So could you please create a patch from your pull request and attach it here? It will allow other people to create a test case for your patch so it can be finally applied.
-
David Trasbo September 2nd, 2010 @ 02:49 PM
- Assigned user set to José Valim
-
Adrian September 3rd, 2010 @ 04:34 AM
Hello,
Im having trouble overriding the as_json method in one of my models,
Is this issue related to my problem?
Thanks
-
David Trasbo September 3rd, 2010 @ 01:18 PM
Adrian,
Maybe, maybe not - I can't tell. Why don't you hop into #railsbridge at irc.freenode.org and ping me there? I'm dtrasbo.
-
Jeff Kreeftmeijer October 10th, 2010 @ 08:54 AM
- Tag set to json, patch
-
José Valim October 11th, 2010 @ 11:46 PM
Kent, could you please rebase your patch? Sorry but it no longer applies.
-
Kent Sibilev October 12th, 2010 @ 04:20 PM
You can close this ticket, since it looks like the required changes have been submitted already with another patch.
Thanks.
-
José Valim October 12th, 2010 @ 04:33 PM
- State changed from new to resolved
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>