Skip to content

Commit

Permalink
Support :json_parser and :json_serializer application options as defa…
Browse files Browse the repository at this point in the history
…ult implementations for parsing/serializing JSON

This also removes the DEFAULT_PARSER and DEFAULT_SERIALIZER from
the json_parser and json plugins, but those were not designed for
direct use.
  • Loading branch information
jeremyevans committed Jul 4, 2018
1 parent ce8ca49 commit 3b242d1
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
= master

* Support :json_parser and :json_serializer application options as default implementations for parsing/serializing JSON (jeremyevans)

* Add :handle_result option to middleware plugin for modifying rack result before returning it (jeremyevans)

* Make the flash plugin work correctly when sessions are serialized with JSON (jeremyevans)
Expand Down
4 changes: 4 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,10 @@ The following options are respected by the default library or multiple plugins:
:add_script_name :: Prepend the SCRIPT_NAME for the request to paths. This is
useful if you mount the app as a path under another app.
:freeze_middleware :: Whether to freeze all middleware when building the rack app.
:json_parser :: A callable for parsing JSON (+JSON.parse+ in general used by
default).
:json_serializer :: A callable for serializing JSON (+to_json+ in general used
by default).
:root :: Set the root path for the app. This defaults to the current working
directory of the process.

Expand Down
4 changes: 2 additions & 2 deletions lib/roda/plugins/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def self.configure(app, opts = {})

if opts[:precompiled] && !opts[:compiled] && ::File.exist?(opts[:precompiled])
require 'json'
opts[:compiled] = ::JSON.parse(::File.read(opts[:precompiled]))
opts[:compiled] = (app.opts[:json_parser] || ::JSON.method(:parse)).call(::File.read(opts[:precompiled]))
end

if opts[:early_hints]
Expand Down Expand Up @@ -451,7 +451,7 @@ def compile_assets(type=nil)
if assets_opts[:precompiled]
require 'json'
::FileUtils.mkdir_p(File.dirname(assets_opts[:precompiled]))
::File.open(assets_opts[:precompiled], 'wb'){|f| f.write(assets_opts[:compiled].to_json)}
::File.open(assets_opts[:precompiled], 'wb'){|f| f.write((opts[:json_serializer] || :to_json.to_proc).call(assets_opts[:compiled]))}
end

assets_opts[:compiled]
Expand Down
4 changes: 1 addition & 3 deletions lib/roda/plugins/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ module RodaPlugins
#
# plugin :json, content_type: 'application/xml'
module Json
DEFAULT_SERIALIZER = :to_json.to_proc

# Set the classes to automatically convert to JSON, and the serializer to use.
def self.configure(app, opts=OPTS)
classes = opts[:classes] || [Array, Hash]
Expand All @@ -63,7 +61,7 @@ def self.configure(app, opts=OPTS)
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_serializer] = opts[:serializer] || app.opts[:json_result_serializer] || app.opts[:json_serializer] || :to_json.to_proc

app.opts[:json_result_include_request] = opts[:include_request] if opts.has_key?(:include_request)

Expand Down
3 changes: 1 addition & 2 deletions lib/roda/plugins/json_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ module RodaPlugins
# header for the request includes "json".
module JsonParser
DEFAULT_ERROR_HANDLER = proc{|r| r.halt [400, {}, []]}
DEFAULT_PARSER = JSON.method(:parse)

# Handle options for the json_parser plugin:
# :error_handler :: A proc to call if an exception is raised when
Expand All @@ -32,7 +31,7 @@ module JsonParser
# only wrap values that are not already hashes.
def self.configure(app, opts=OPTS)
app.opts[:json_parser_error_handler] = opts[:error_handler] || app.opts[:json_parser_error_handler] || DEFAULT_ERROR_HANDLER
app.opts[:json_parser_parser] = opts[:parser] || app.opts[:json_parser_parser] || DEFAULT_PARSER
app.opts[:json_parser_parser] = opts[:parser] || app.opts[:json_parser_parser] || app.opts[:json_parser] || JSON.method(:parse)
app.opts[:json_parser_include_request] = opts[:include_request] if opts.has_key?(:include_request)

case opts[:wrap]
Expand Down

0 comments on commit 3b242d1

Please sign in to comment.