Skip to content

add symbolize keys option to deserializer #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ The `jsonapi_deserialize` helper accepts the following options:
* `except`: returns exclusively attributes/relationship which are not in the list
* `polymorphic`: will add and detect the `_type` attribute and class to the
defined list of polymorphic relationships
* `symbolize_keys`: returns hash with symbolized keys

This functionality requires support for _inflections_. If your project uses
`active_support` or `rails` you don't need to do anything. Alternatively, we will
Expand Down
8 changes: 6 additions & 2 deletions lib/jsonapi/deserialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def jsonapi_deserialize(document, options = {})

# Transform keys and any option values.
options = options.as_json
['only', 'except', 'polymorphic'].each do |opt_name|
['only', 'except', 'polymorphic', 'symbolize_keys'].each do |opt_name|
opt_value = options[opt_name]
options[opt_name] = Array(opt_value).map(&:to_s) if opt_value
end
Expand Down Expand Up @@ -77,7 +77,11 @@ def jsonapi_deserialize(document, options = {})
end
end

parsed
if options['symbolize_keys']
parsed.symbolize_keys
else
parsed
end
end
end
end
11 changes: 11 additions & 0 deletions spec/deserialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,16 @@
)
end
end


context 'with `symbolize_keys`' do
it do
expect(
jsonapi_deserialize.call(
document, symbolize_keys: true
)
).to match(jsonapi_deserialize.call(document).symbolize_keys)
end
end
end
end