JSON::Encodable
Make a class encodable into JSON format.
Usage
- Include
JSON::Encodablemodule - Call
.propertymethod with property name - Then the instance will be able to respond to
.to_jsonmethod
#to_json
class Blog
include JSON::Encodable
property :id
property :title
property :username
def id
1
end
def title
"wonderland"
end
def username
"alice"
end
end
Blog.new.to_json
#=> '{"id":1,"title":"wonderland","username":"alice"}'
#as_json(options = {})
You can also call .as_json method with :except and :only options.
Blog.new.as_json(only: [:id, :username])
#=> {"id":1,"username":"alice"}
Blog.new.as_json(except: [:username])
#=> {"id":1,"title":"wonderland"}