Module: JSON

Defined in:
lib/json/encodable.rb,
lib/json/encodable/version.rb,
lib/json/encodable/property.rb

Overview

Makes an included module encodable into JSON format by putting .as_json method.

You can pass :only and :except options to .to_json method.

Examples:

class Blog
  include JSON::Encodable

  property :id
  property :title
  property :username

  def id
    1
  end

  def title
    "wonderland"
  end

  def username
    "alice"
  end
end

puts Blog.new.to_json
#=> {"id":1,"title":"wonderland","username":"alice"}
puts Blog.new.to_json(only: [:id, :username])
#=> {"id":1,"username":"alice"}

puts Blog.new.to_json(except: [:username])
#=> {"id":1,"title":"wonderland"}

Defined Under Namespace

Modules: Encodable