-
Notifications
You must be signed in to change notification settings - Fork 332
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
Add load_file
and load_file!
methods, with tests. Fixes issue #386.
#387
Conversation
lib/json/common.rb
Outdated
# Parses the content of a file | ||
# See parse method documentation for more information. | ||
def load_file(filespec, opts = {}) | ||
parse(File.read(filespec)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@keithrbennett I'm just wondering, shouldn't we pass options for 'File.read'? It might be useful for supporting different encodings, etc. It's not related but I have a case where I struggle with BOM character in the beginning of the CSV file and by passing encoding I can simply remove those additional bytes. Maybe that would work similarly in JSON.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First I want to thank you for pointing out to me that I was failing to pass the passed opts
along to the called methods. I've fixed that and pushed the fix.
Regarding your point about File.read
options, do I understand you correctly that you would want to specify options to File.read
, not JSON.parse
? If so, I'm not sure how we could do that, since we are already using the Ruby hash last parameter feature for the JSON.parse
options.
Any strategy to handle options to File.read
would need to be balanced with the cost of the complexity added to support it. Can you suggest a way to accommodate this need in the least complex way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but I'm happy with current solution. I don't have idea how to solve it in a elegant way. Maybe maintainer can help. And sorry for the oftop, anyway it's better than before :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best not mix options. If a user need options, they can do the File.read
. FWIW, YAML.load_file
has no file option either.
Thanks @keithrbennett! |
@@ -123,4 +123,43 @@ def test_JSON | |||
assert_equal @json, JSON(@hash) | |||
assert_equal @hash, JSON(@json) | |||
end | |||
|
|||
|
|||
def test_load_file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test with options...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about that. I've added this (and an equivalent function for load_file!
), would it suffice?:
def test_load_file_with_option
temp_file_containing(@json) do |filespec|
key_classes = load_file(filespec, symbolize_names: true).keys.map(&:class)
assert_true (key_classes.include?(Symbol) && (! key_classes.include?(String)))
end
end
Also, in order to get the tests to pass I had to rebase master
my feature branch. Is it ok to git push -f
this branch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it ok to git push -f this branch?
Yes, that's the best thing to do 😄
799f06b
to
4ac7ca3
Compare
Very good. Only thing remaining would be to squash your commits (although it can be done by maintainers with 'squash and merge') |
tests/json_common_interface_test.rb
Outdated
|
||
def test_load_shared(method_name) | ||
temp_file_containing(@json) do |filespec| | ||
assert_equal method(method_name).call(filespec), @hash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
method(method_name).call(filespec)
=> public_send(method_name, filespec)
. I think it's more idiomatic.
Same for test_load_file_with_option_shared
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made this change and squashed the commits. Thanks for your guidance, I agree that these are both improvements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the impressively quick turnaround and good PR!
483a0c7
to
c7afb22
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good PR 👍
LGTM
@keithrbennett Thanks. Could you rebase from this from master branch? I will merge this targeted json-2.4.0 after releasing json-2.3.1. |
c7afb22
to
0be363c
Compare
Done, thanks.
- Keith
… On Jun 30, 2020, at 07:32, Hiroshi SHIBATA ***@***.***> wrote:
@keithrbennett <https://github.com/keithrbennett> Thanks. Could you rebase from this from master branch?
I will merge this targeted json-2.4.0 after releasing json-2.3.1.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <#387 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAAG56SOJBP7OXTPANNUXJDRZHEOFANCNFSM4JDKZJMA>.
|
Thanks! |
Adds
load_file
methods analogous to YAML's.