Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Unreleased
-----
* Fix loading the JSON conversion table cache under json 3.0, which no longer accepts comments by default. (@jules-w2)

3.2.1
-----
Expand Down
5 changes: 4 additions & 1 deletion lib/measured/cache/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def exist?

def read
return unless exist?
decode(JSON.load(File.read(@path), nil, freeze: true))
# The cache files written by Measured::Cache::JsonWriter start with a `//` comment line, which is not
# valid JSON. json < 3.0 accepted comments by default, json >= 3.0 requires opting in. The option is
# ignored by json < 2.20, where comments are accepted anyway.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A three line comment is not required to add a single config option. LLMs dump this out, but it makes the codebase less legible, not more.

decode(JSON.load(File.read(@path), nil, freeze: true, allow_comments: true))
end

def write(table)
Expand Down
7 changes: 7 additions & 0 deletions test/cache/json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class Measured::Cache::JsonTest < ActiveSupport::TestCase
assert_equal @table_hash, @cache.read
end

test "#read loads a file that starts with the comment header written by the writer" do
commented_json = "// Do not modify this file directly. Regenerate it with 'rake cache:write'.\n#{@table_json}"
File.expects(:exist?).with(@cache.path).returns(true)
File.expects(:read).with(@cache.path).returns(commented_json)
assert_equal @table_hash, @cache.read
end

test "#write raises not implemented" do
assert_raises(ArgumentError) do
@cache.write({})
Expand Down
Loading