Skip to content

Commit

Permalink
Fix AR json encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Jun 9, 2009
1 parent f9b2227 commit 63d0c33
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
5 changes: 3 additions & 2 deletions activerecord/lib/active_record/serialization.rb
Expand Up @@ -5,8 +5,9 @@ module Serialization
class Serializer #:nodoc: class Serializer #:nodoc:
attr_reader :options attr_reader :options


def initialize(record, options = {}) def initialize(record, options = nil)
@record, @options = record, options.dup @record = record
@options = options ? options.dup : {}
end end


# To replicate the behavior in ActiveRecord#attributes, # To replicate the behavior in ActiveRecord#attributes,
Expand Down
27 changes: 8 additions & 19 deletions activerecord/lib/active_record/serializers/json_serializer.rb
@@ -1,8 +1,10 @@
require 'active_support/json'
require 'active_support/core_ext/module/model_naming'

module ActiveRecord #:nodoc: module ActiveRecord #:nodoc:
module Serialization module Serialization
def self.included(base) def self.included(base)
base.cattr_accessor :include_root_in_json, :instance_writer => false base.cattr_accessor :include_root_in_json, :instance_writer => false
base.extend ClassMethods
end end


# Returns a JSON string representing the model. Some configuration is # Returns a JSON string representing the model. Some configuration is
Expand Down Expand Up @@ -72,29 +74,16 @@ def self.included(base)
# {"comments": [{"body": "Don't think too hard"}], # {"comments": [{"body": "Don't think too hard"}],
# "title": "So I was thinking"}]} # "title": "So I was thinking"}]}
def to_json(options = {}) def to_json(options = {})
json = JsonSerializer.new(self, options).to_s hash = Serializer.new(self, options).serializable_record
if include_root_in_json hash = { self.class.model_name.element => hash } if include_root_in_json
"{#{self.class.json_class_name}:#{json}}" ActiveSupport::JSON.encode(hash)
else
json
end
end end


def as_json(options = nil) self end #:nodoc:

def from_json(json) def from_json(json)
self.attributes = ActiveSupport::JSON.decode(json) self.attributes = ActiveSupport::JSON.decode(json)
self self
end end

class JsonSerializer < ActiveRecord::Serialization::Serializer #:nodoc:
def serialize
ActiveSupport::JSON.encode(serializable_record)
end
end

module ClassMethods
def json_class_name
@json_class_name ||= name.demodulize.underscore.inspect
end
end
end end
end end
8 changes: 4 additions & 4 deletions activerecord/test/cases/json_serialization_test.rb
Expand Up @@ -170,18 +170,18 @@ def @david.favorite_quote; "Constraints are liberating"; end
def test_should_allow_only_option_for_list_of_authors def test_should_allow_only_option_for_list_of_authors
authors = [@david, @mary] authors = [@david, @mary]


assert_equal %([{"name":"David"},{"name":"Mary"}]), authors.to_json(:only => :name) assert_equal %([{"name":"David"},{"name":"Mary"}]), ActiveSupport::JSON.encode(authors, :only => :name)
end end


def test_should_allow_except_option_for_list_of_authors def test_should_allow_except_option_for_list_of_authors
authors = [@david, @mary] authors = [@david, @mary]


assert_equal %([{"id":1},{"id":2}]), authors.to_json(:except => [:name, :author_address_id, :author_address_extra_id]) assert_equal %([{"id":1},{"id":2}]), ActiveSupport::JSON.encode(authors, :except => [:name, :author_address_id, :author_address_extra_id])
end end


def test_should_allow_includes_for_list_of_authors def test_should_allow_includes_for_list_of_authors
authors = [@david, @mary] authors = [@david, @mary]
json = authors.to_json( json = ActiveSupport::JSON.encode(authors,
:only => :name, :only => :name,
:include => { :include => {
:posts => { :only => :id } :posts => { :only => :id }
Expand All @@ -200,6 +200,6 @@ def test_should_allow_options_for_hash_of_authors
2 => @mary 2 => @mary
} }


assert_equal %({"1":{"name":"David"}}), authors_hash.to_json(:only => [1, :name]) assert_equal %({"1":{"name":"David"}}), ActiveSupport::JSON.encode(authors_hash, :only => [1, :name])
end end
end end

0 comments on commit 63d0c33

Please sign in to comment.