Allow any module or class to be converted to JSON in a simple way

This commit is contained in:
Tyler Rick 2019-01-23 15:07:16 -08:00 committed by Zachary Scott
parent 9c091b4fd3
commit cb40ae38c1
2 changed files with 10 additions and 0 deletions

View File

@ -49,6 +49,12 @@ end
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)

View File

@ -68,6 +68,10 @@ module JSONTest
[ :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\"}) ],