Skip to content
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

Merged
merged 1 commit into from
Jun 30, 2020

Conversation

keithrbennett
Copy link
Contributor

Adds load_file methods analogous to YAML's.

# Parses the content of a file
# See parse method documentation for more information.
def load_file(filespec, opts = {})
parse(File.read(filespec))

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.

Copy link
Contributor Author

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?

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 :)

Copy link
Contributor

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.

@rafaltrojanowski
Copy link

Thanks @keithrbennett!

@@ -123,4 +123,43 @@ def test_JSON
assert_equal @json, JSON(@hash)
assert_equal @hash, JSON(@json)
end


def test_load_file
Copy link
Contributor

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...

Copy link
Contributor Author

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?

Copy link
Contributor

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 😄

@marcandre
Copy link
Contributor

Very good. Only thing remaining would be to squash your commits (although it can be done by maintainers with 'squash and merge')


def test_load_shared(method_name)
temp_file_containing(@json) do |filespec|
assert_equal method(method_name).call(filespec), @hash
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

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!

Copy link
Contributor

@marcandre marcandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good PR 👍
LGTM

@hsbt
Copy link
Collaborator

hsbt commented Jun 30, 2020

@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.

@keithrbennett
Copy link
Contributor Author

keithrbennett commented Jun 30, 2020 via email

@hsbt hsbt merged commit e1451ed into flori:master Jun 30, 2020
@hsbt
Copy link
Collaborator

hsbt commented Jun 30, 2020

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants