Allow any module or class to be converted to JSON in a simple way
This commit is contained in:
parent
9c091b4fd3
commit
cb40ae38c1
|
@ -49,6 +49,12 @@ end
|
||||||
klass.prepend(ActiveSupport::ToJsonWithActiveSupportEncoder)
|
klass.prepend(ActiveSupport::ToJsonWithActiveSupportEncoder)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Module
|
||||||
|
def as_json(options = nil) #:nodoc:
|
||||||
|
name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class Object
|
class Object
|
||||||
def as_json(options = nil) #:nodoc:
|
def as_json(options = nil) #:nodoc:
|
||||||
if respond_to?(:to_hash)
|
if respond_to?(:to_hash)
|
||||||
|
|
|
@ -68,6 +68,10 @@ module JSONTest
|
||||||
[ :this, %("this") ],
|
[ :this, %("this") ],
|
||||||
[ :"a b", %("a b") ]]
|
[ :"a b", %("a b") ]]
|
||||||
|
|
||||||
|
ModuleTests = [[ Module, %("Module") ],
|
||||||
|
[ Class, %("Class") ],
|
||||||
|
[ ActiveSupport, %("ActiveSupport") ],
|
||||||
|
[ ActiveSupport::MessageEncryptor, %("ActiveSupport::MessageEncryptor") ]]
|
||||||
ObjectTests = [[ Foo.new(1, 2), %({\"a\":1,\"b\":2}) ]]
|
ObjectTests = [[ Foo.new(1, 2), %({\"a\":1,\"b\":2}) ]]
|
||||||
HashlikeTests = [[ Hashlike.new, %({\"bar\":\"world\",\"foo\":\"hello\"}) ]]
|
HashlikeTests = [[ Hashlike.new, %({\"bar\":\"world\",\"foo\":\"hello\"}) ]]
|
||||||
StructTests = [[ MyStruct.new(:foo, "bar"), %({\"name\":\"foo\",\"value\":\"bar\"}) ],
|
StructTests = [[ MyStruct.new(:foo, "bar"), %({\"name\":\"foo\",\"value\":\"bar\"}) ],
|
||||||
|
|
Loading…
Reference in New Issue