aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/serializers/json_serialization_test.rb
diff options
context:
space:
mode:
authorJoe Fiorini <joe@joefiorini.com>2011-07-01 22:48:11 -0400
committerJoe Fiorini <joe@joefiorini.com>2011-07-01 23:00:13 -0400
commitac5e6911536b0532498df5f650fc2d172087e174 (patch)
tree5448f786c5719ff176f9d11cb8a71764e71b711f /activemodel/test/cases/serializers/json_serialization_test.rb
parent36d289ad242c4c8da74eed35baeb21cdffcbe907 (diff)
downloadrails-ac5e6911536b0532498df5f650fc2d172087e174.tar.gz
rails-ac5e6911536b0532498df5f650fc2d172087e174.tar.bz2
rails-ac5e6911536b0532498df5f650fc2d172087e174.zip
Enable passing root: false to #to_json
Diffstat (limited to 'activemodel/test/cases/serializers/json_serialization_test.rb')
-rw-r--r--activemodel/test/cases/serializers/json_serialization_test.rb53
1 files changed, 52 insertions, 1 deletions
diff --git a/activemodel/test/cases/serializers/json_serialization_test.rb b/activemodel/test/cases/serializers/json_serialization_test.rb
index 500a5c575f..5e1e7d897a 100644
--- a/activemodel/test/cases/serializers/json_serialization_test.rb
+++ b/activemodel/test/cases/serializers/json_serialization_test.rb
@@ -8,6 +8,12 @@ class Contact
include ActiveModel::Serializers::JSON
include ActiveModel::Validations
+ def attributes=(hash)
+ hash.each do |k, v|
+ instance_variable_set("@#{k}", v)
+ end
+ end
+
def attributes
instance_values
end unless method_defined?(:attributes)
@@ -34,7 +40,7 @@ class JsonSerializationTest < ActiveModel::TestCase
assert_match %r{"preferences":\{"shows":"anime"\}}, json
end
- test "should not include root in json" do
+ test "should not include root in json (class method)" do
begin
Contact.include_root_in_json = false
json = @contact.to_json
@@ -50,6 +56,13 @@ class JsonSerializationTest < ActiveModel::TestCase
end
end
+ test "should not include root in json (option)" do
+
+ json = @contact.to_json(:root => false)
+
+ assert_no_match %r{^\{"contact":\{}, json
+ end
+
test "should include custom root in json" do
json = @contact.to_json(:root => 'json_contact')
@@ -135,6 +148,44 @@ class JsonSerializationTest < ActiveModel::TestCase
end
end
+ test "from_json should set the object's attributes" do
+ json = @contact.to_json
+ result = Contact.new.from_json(json)
+
+ assert_equal result.name, @contact.name
+ assert_equal result.age, @contact.age
+ assert_equal Time.parse(result.created_at), @contact.created_at
+ assert_equal result.awesome, @contact.awesome
+ assert_equal result.preferences, @contact.preferences
+ end
+
+ test "from_json should work without a root (method parameter)" do
+ json = @contact.to_json(:root => false)
+ result = Contact.new.from_json(json, false)
+
+ assert_equal result.name, @contact.name
+ assert_equal result.age, @contact.age
+ assert_equal Time.parse(result.created_at), @contact.created_at
+ assert_equal result.awesome, @contact.awesome
+ assert_equal result.preferences, @contact.preferences
+ end
+
+ test "from_json should work without a root (class attribute)" do
+ begin
+ Contact.include_root_in_json = false
+ json = @contact.to_json
+ result = Contact.new.from_json(json)
+
+ assert_equal result.name, @contact.name
+ assert_equal result.age, @contact.age
+ assert_equal Time.parse(result.created_at), @contact.created_at
+ assert_equal result.awesome, @contact.awesome
+ assert_equal result.preferences, @contact.preferences
+ ensure
+ Contact.include_root_in_json = true
+ end
+ end
+
test "custom as_json should be honored when generating json" do
def @contact.as_json(options); { :name => name, :created_at => created_at }; end
json = @contact.to_json