Skip to content

Commit

Permalink
Change #as_json adding the option stringify_keys to it
Browse files Browse the repository at this point in the history
  • Loading branch information
locawebemailmarketing authored and fabioperrella committed Jul 13, 2020
1 parent 1309919 commit a000efa
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.22.0
* Adds option `stringify_keys: true` to #as_json methods (fix #151)

## 0.21.1
* MPEG: Ensure parsing does not inadvertently return an Integer instead of Result|nil
* MPEG: Scan further into the MPEG file than previously (scan 32 1KB chunks)
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ img_info = FormatParser.parse(File.open("myimage.jpg", "rb"))
JSON.pretty_generate(img_info) #=> ...
```

To convert the result to a Hash

```ruby
img_info = FormatParser.parse(File.open("myimage.jpg", "rb"))
img_info.as_json

# it's also possible to convert all keys to string
img_info.as_json(stringify_keys: true)
```


## Creating your own parsers

See the [section on writing parsers in CONTRIBUTING.md](CONTRIBUTING.md#so-you-want-to-contribute-a-new-parser)
Expand Down Expand Up @@ -188,7 +199,7 @@ Unless specified otherwise in this section the fixture files are MIT licensed an

## Copyright

Copyright (c) 2019 WeTransfer.
Copyright (c) 2020 WeTransfer.

`format_parser` is distributed under the conditions of the [Hippocratic License](https://firstdonoharm.dev/version/1/2/license.html)
- See LICENSE.txt for further details.
10 changes: 9 additions & 1 deletion lib/attributes_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ module FormatParser::AttributesJSON

# Implements a sane default `as_json` for an object
# that accessors defined
def as_json(root: false)
#
# @param root[Bool] if true, it surrounds the result in a hash with a key
# `format_parser_file_info`
# @param stringify_keys[Bool] if true, it transforms all the hash keys to a string.
# The default value is false for backward compatibility
def as_json(root: false, stringify_keys: false)
h = {}
h['nature'] = nature if respond_to?(:nature) # Needed for file info structs
methods.grep(/\w\=$/).each_with_object(h) do |attr_writer_method_name, h|
Expand All @@ -27,6 +32,9 @@ def as_json(root: false)
sanitized_value = _sanitize_json_value(unwrapped_attribute_value)
h[reader_method_name] = sanitized_value
end

h = FormatParser::HashUtils.deep_transform_keys(h, &:to_s) if stringify_keys

if root
{'format_parser_file_info' => h}
else
Expand Down
2 changes: 1 addition & 1 deletion lib/format_parser/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module FormatParser
VERSION = '0.21.1'
VERSION = '0.22.0'
end
20 changes: 20 additions & 0 deletions spec/attributes_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,24 @@ def nature
JSON.pretty_generate(object_with_attributes_module)
}.to raise_error(/structure too deep/)
end

it 'converts all hash keys to string when stringify_keys: true' do
fixture_path = fixtures_dir + '/ZIP/arch_few_entries.zip'
fi_io = File.open(fixture_path, 'rb')

result = FormatParser::ZIPParser.new.call(fi_io).as_json(stringify_keys: true)
expect(
result['entries'].flat_map { |entry| entry.keys.map(&:class) }.uniq
).to eq([String])
end

it 'does not convert hash keys to string when stringify_keys: false' do
fixture_path = fixtures_dir + '/ZIP/arch_few_entries.zip'
fi_io = File.open(fixture_path, 'rb')

result = FormatParser::ZIPParser.new.call(fi_io).as_json
expect(
result['entries'].flat_map { |entry| entry.keys.map(&:class) }.uniq
).to eq([Symbol])
end
end
28 changes: 28 additions & 0 deletions spec/parsers/mp3_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,32 @@
subject.call(StringIO.new(''))
}.to raise_error(FormatParser::IOUtils::InvalidRead)
end

describe '#as_json' do
it 'converts all hash keys to string when stringify_keys: true' do
fpath = fixtures_dir + '/MP3/Cassy.mp3'
result = subject.call(File.open(fpath, 'rb')).as_json(stringify_keys: true)

expect(
result['intrinsics'].keys.map(&:class).uniq
).to eq([String])

expect(
result['intrinsics']['id3tags'].map(&:class).uniq
).to eq([ID3Tag::Tag])
end

it 'does not convert the hash keys to string when stringify_keys: false' do
fpath = fixtures_dir + '/MP3/Cassy.mp3'
result = subject.call(File.open(fpath, 'rb')).as_json

expect(
result['intrinsics'].keys.map(&:class).uniq
).to eq([Symbol])

expect(
result['intrinsics'][:id3tags].map(&:class).uniq
).to eq([ID3Tag::Tag])
end
end
end

0 comments on commit a000efa

Please # to comment.