Module: Roda::RodaPlugins::Json

Defined in:
lib/roda/plugins/json.rb

Overview

The json plugin allows match blocks to return arrays or hashes, and have those arrays or hashes be converted to json which is used as the response body. It also sets the response content type to application/json. So you can take code like:

r.root do
  response['Content-Type'] = 'application/json'
  [1, 2, 3].to_json
end
r.is "foo" do
  response['Content-Type'] = 'application/json'
  {'a'=>'b'}.to_json
end

and DRY it up:

plugin :json
r.root do
  [1, 2, 3]
end
r.is "foo" do
  {'a'=>'b'}
end

By default, only arrays and hashes are handled, but you can specifically set the allowed classes to json by adding using the :classes option when loading the plugin:

plugin :json, :classes=>[Array, Hash, Sequel::Model]

By default objects are serialized with to_json, but you can pass in a custom serializer, which can be any object that responds to call(object).

plugin :json, :serializer=>proc{|o| o.to_json(root: true)}

If you need the request information during serialization, such as HTTP headers or query parameters, you can pass in the :include_request option, which will pass in the request object as the second argument when calling the serializer.

plugin :json, :include_request=>true, :serializer=>proc{|o, request| ...}

The default content-type is ‘application/json’, but you can change that using the :content_type option:

plugin :json, :content_type=>'application/xml'

Defined Under Namespace

Modules: ClassMethods, RequestMethods

Constant Summary collapse

OPTS =
{}.freeze
DEFAULT_SERIALIZER =
lambda{|o| o.to_json}
DEFAULT_CONTENT_TYPE =
'application/json'.freeze

Class Method Summary collapse

Class Method Details

.configure(app, opts = OPTS) ⇒ Object

Set the classes to automatically convert to JSON, and the serializer to use.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/roda/plugins/json.rb', line 61

def self.configure(app, opts=OPTS)
  classes = opts[:classes] || [Array, Hash]
  app.opts[:json_result_classes] ||= []
  app.opts[:json_result_classes] += classes
  app.opts[:json_result_classes].uniq!
  app.opts[:json_result_classes].freeze

  app.opts[:json_result_serializer] = opts[:serializer] || app.opts[:json_result_serializer] || DEFAULT_SERIALIZER

  app.opts[:json_result_include_request] = opts[:include_request] || app.opts[:json_result_include_request]

  app.opts[:json_result_content_type] = opts[:content_type] || DEFAULT_CONTENT_TYPE
end