Skip to content

Commit

Permalink
Merge pull request #107 from hanami/support-multi_json-for-body-parsers
Browse files Browse the repository at this point in the history
Support multi_json for body parsers
  • Loading branch information
jodosha committed May 4, 2016
2 parents b587bb9 + 4e042d6 commit f420860
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ if !ENV['TRAVIS']
end

gem 'hanami-utils', '~> 0.8', require: false, github: 'hanami/utils', branch: '0.8.x'
gem 'multi_json', '~> 1.0', require: false
gem 'coveralls', require: false
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,10 @@ If the json can't be parsed an exception is raised:
Hanami::Routing::Parsing::BodyParsingError
```

##### `multi_json`

If you want to use a different JSON backend, include `multi_json` in your `Gemfile`.

#### Custom Parsers

```ruby
Expand Down
15 changes: 12 additions & 3 deletions lib/hanami/routing/parsing/json_parser.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
require 'json'
begin
require 'multi_json'
rescue LoadError
require 'json'
end

module Hanami
module Routing
module Parsing
class JsonParser < Parser
unless defined?(MultiJson)
MultiJson = JSON
MultiJson::ParseError = JSON::ParserError
end

def mime_types
['application/json', 'application/vnd.api+json']
end
Expand All @@ -18,8 +27,8 @@ def mime_types
#
# @since 0.2.0
def parse(body)
JSON.parse(body)
rescue JSON::ParserError => e
MultiJson.load(body)
rescue MultiJson::ParseError => e
raise BodyParsingError.new(e.message)
end
end
Expand Down

0 comments on commit f420860

Please sign in to comment.