diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 489842d44b0d9..ddcb6b7026bad 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* Allow serializing any module or class to JSON by name + + *Tyler Rick*, *Zachary Scott* + * Raise `ActiveSupport::EncryptedFile::MissingKeyError` when the `RAILS_MASTER_KEY` environment variable is blank (e.g. `""`). diff --git a/activesupport/lib/active_support/core_ext/object/json.rb b/activesupport/lib/active_support/core_ext/object/json.rb index 8ba694266d05f..75fae96dc6d77 100644 --- a/activesupport/lib/active_support/core_ext/object/json.rb +++ b/activesupport/lib/active_support/core_ext/object/json.rb @@ -49,6 +49,12 @@ def to_json(options = nil) klass.prepend(ActiveSupport::ToJsonWithActiveSupportEncoder) end +class Module + def as_json(options = nil) #:nodoc: + name + end +end + class Object def as_json(options = nil) #:nodoc: if respond_to?(:to_hash) diff --git a/activesupport/test/json/encoding_test_cases.rb b/activesupport/test/json/encoding_test_cases.rb index daf662051a1ef..662e30c68c072 100644 --- a/activesupport/test/json/encoding_test_cases.rb +++ b/activesupport/test/json/encoding_test_cases.rb @@ -68,6 +68,10 @@ module EncodingTestCases [ :this, %("this") ], [ :"a b", %("a b") ]] + ModuleTests = [[ Module, %("Module") ], + [ Class, %("Class") ], + [ ActiveSupport, %("ActiveSupport") ], + [ ActiveSupport::MessageEncryptor, %("ActiveSupport::MessageEncryptor") ]] ObjectTests = [[ Foo.new(1, 2), %({\"a\":1,\"b\":2}) ]] HashlikeTests = [[ Hashlike.new, %({\"bar\":\"world\",\"foo\":\"hello\"}) ]] StructTests = [[ MyStruct.new(:foo, "bar"), %({\"name\":\"foo\",\"value\":\"bar\"}) ],