Skip to content

Commit e1451ed

Browse files
authored
Merge pull request #387 from keithrbennett/add-load-file-methods
Add `load_file` and `load_file!` methods, with tests. Fixes issue #386.
2 parents ddc29e2 + 0be363c commit e1451ed

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

lib/json/common.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,16 @@ def parse!(source, opts = {})
282282
Parser.new(source, **(opts||{})).parse
283283
end
284284

285+
# Parses the content of a file (see parse method documentation for more information).
286+
def load_file(filespec, opts = {})
287+
parse(File.read(filespec), opts)
288+
end
289+
290+
# Parses the content of a file (see parse! method documentation for more information).
291+
def load_file!(filespec, opts = {})
292+
parse!(File.read(filespec), opts)
293+
end
294+
285295
# :call-seq:
286296
# JSON.generate(obj, opts = nil) -> new_string
287297
#

tests/json_common_interface_test.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,60 @@ def test_JSON
123123
assert_equal @json, JSON(@hash)
124124
assert_equal @hash, JSON(@json)
125125
end
126+
127+
def test_load_file
128+
test_load_shared(:load_file)
129+
end
130+
131+
def test_load_file!
132+
test_load_shared(:load_file!)
133+
end
134+
135+
def test_load_file_with_option
136+
test_load_file_with_option_shared(:load_file)
137+
end
138+
139+
def test_load_file_with_option!
140+
test_load_file_with_option_shared(:load_file!)
141+
end
142+
143+
private
144+
145+
def test_load_shared(method_name)
146+
temp_file_containing(@json) do |filespec|
147+
assert_equal JSON.public_send(method_name, filespec), @hash
148+
end
149+
end
150+
151+
def test_load_file_with_option_shared(method_name)
152+
temp_file_containing(@json) do |filespec|
153+
parsed_object = JSON.public_send(method_name, filespec, symbolize_names: true)
154+
key_classes = parsed_object.keys.map(&:class)
155+
assert_true key_classes.include?(Symbol) && (! key_classes.include?(String))
156+
end
157+
end
158+
159+
# Copied and slightly modified from https://github.com/keithrbennett/trick_bag
160+
# (https://github.com/keithrbennett/trick_bag/blob/master/lib/trick_bag/io/temp_files.rb).
161+
#
162+
# For the easy creation and deletion of a temp file populated with text,
163+
# wrapped around the code block you provide.
164+
#
165+
# @param text the text to write to the temporary file
166+
# @param file_prefix optional prefix for the temporary file's name
167+
# @yield filespec of the temporary file
168+
def temp_file_containing(text, file_prefix = '')
169+
raise "This method must be called with a code block." unless block_given?
170+
171+
filespec = nil
172+
begin
173+
Tempfile.open(file_prefix) do |file|
174+
file << text
175+
filespec = file.path
176+
end
177+
yield(filespec)
178+
ensure
179+
File.delete(filespec) if filespec && File.exist?(filespec)
180+
end
181+
end
126182
end

0 commit comments

Comments
 (0)