Skip to content

Commit 0d48e5e

Browse files
Change #as_json adding the option stringify_keys to it
1 parent 6f4ef24 commit 0d48e5e

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.22.0
2+
* Adds option `stringify_keys: true` to #as_json methods (fix #151)
3+
14
## 0.21.1
25
* MPEG: Ensure parsing does not inadvertently return an Integer instead of Result|nil
36
* MPEG: Scan further into the MPEG file than previously (scan 32 1KB chunks)

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ img_info = FormatParser.parse(File.open("myimage.jpg", "rb"))
7575
JSON.pretty_generate(img_info) #=> ...
7676
```
7777

78+
To convert the result to a Hash or a structure suitable for JSON serialization
79+
80+
```ruby
81+
img_info = FormatParser.parse(File.open("myimage.jpg", "rb"))
82+
img_info.as_json
83+
84+
# it's also possible to convert all keys to string
85+
img_info.as_json(stringify_keys: true)
86+
```
87+
88+
7889
## Creating your own parsers
7990

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

189200
## Copyright
190201

191-
Copyright (c) 2019 WeTransfer.
202+
Copyright (c) 2020 WeTransfer.
192203

193204
`format_parser` is distributed under the conditions of the [Hippocratic License](https://firstdonoharm.dev/version/1/2/license.html)
194205
- See LICENSE.txt for further details.

lib/attributes_json.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ module FormatParser::AttributesJSON
1515

1616
# Implements a sane default `as_json` for an object
1717
# that accessors defined
18-
def as_json(root: false)
18+
#
19+
# @param root[Bool] if true, it surrounds the result in a hash with a key
20+
# `format_parser_file_info`
21+
# @param stringify_keys[Bool] if true, it transforms all the hash keys to a string.
22+
# The default value is false for backward compatibility
23+
def as_json(root: false, stringify_keys: false, **)
1924
h = {}
2025
h['nature'] = nature if respond_to?(:nature) # Needed for file info structs
2126
methods.grep(/\w\=$/).each_with_object(h) do |attr_writer_method_name, h|
@@ -27,6 +32,9 @@ def as_json(root: false)
2732
sanitized_value = _sanitize_json_value(unwrapped_attribute_value)
2833
h[reader_method_name] = sanitized_value
2934
end
35+
36+
h = FormatParser::HashUtils.deep_transform_keys(h, &:to_s) if stringify_keys
37+
3038
if root
3139
{'format_parser_file_info' => h}
3240
else

lib/format_parser/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module FormatParser
2-
VERSION = '0.21.1'
2+
VERSION = '0.22.0'
33
end

spec/attributes_json_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,30 @@ def nature
140140
JSON.pretty_generate(object_with_attributes_module)
141141
}.to raise_error(/structure too deep/)
142142
end
143+
144+
it 'converts all hash keys to string when stringify_keys: true' do
145+
fixture_path = fixtures_dir + '/ZIP/arch_few_entries.zip'
146+
fi_io = File.open(fixture_path, 'rb')
147+
148+
result = FormatParser::ZIPParser.new.call(fi_io).as_json(stringify_keys: true)
149+
150+
result['entries'].each do |entry|
151+
entry.each do |key, _value|
152+
expect(key).to be_a(String)
153+
end
154+
end
155+
end
156+
157+
it 'does not convert hash keys to string when stringify_keys: false' do
158+
fixture_path = fixtures_dir + '/ZIP/arch_few_entries.zip'
159+
fi_io = File.open(fixture_path, 'rb')
160+
161+
result = FormatParser::ZIPParser.new.call(fi_io).as_json
162+
163+
result['entries'].each do |entry|
164+
entry.each do |key, _value|
165+
expect(key).to be_a(Symbol)
166+
end
167+
end
168+
end
143169
end

spec/parsers/mp3_parser_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,32 @@
110110
subject.call(StringIO.new(''))
111111
}.to raise_error(FormatParser::IOUtils::InvalidRead)
112112
end
113+
114+
describe '#as_json' do
115+
it 'converts all hash keys to string when stringify_keys: true' do
116+
fpath = fixtures_dir + '/MP3/Cassy.mp3'
117+
result = subject.call(File.open(fpath, 'rb')).as_json(stringify_keys: true)
118+
119+
expect(
120+
result['intrinsics'].keys.map(&:class).uniq
121+
).to eq([String])
122+
123+
expect(
124+
result['intrinsics']['id3tags'].map(&:class).uniq
125+
).to eq([ID3Tag::Tag])
126+
end
127+
128+
it 'does not convert the hash keys to string when stringify_keys: false' do
129+
fpath = fixtures_dir + '/MP3/Cassy.mp3'
130+
result = subject.call(File.open(fpath, 'rb')).as_json
131+
132+
expect(
133+
result['intrinsics'].keys.map(&:class).uniq
134+
).to eq([Symbol])
135+
136+
expect(
137+
result['intrinsics'][:id3tags].map(&:class).uniq
138+
).to eq([ID3Tag::Tag])
139+
end
140+
end
113141
end

0 commit comments

Comments
 (0)