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

strict option is broken for pure-Ruby implementation #584

Open
eregon opened this issue May 9, 2024 · 1 comment · May be fixed by #585
Open

strict option is broken for pure-Ruby implementation #584

eregon opened this issue May 9, 2024 · 1 comment · May be fixed by #585

Comments

@eregon
Copy link
Contributor

eregon commented May 9, 2024

In this repo:

$ bundle exec ruby -rjson/pure -e 'p JSON.dump({"a" => 1}, strict: true)'
/home/eregon/code/json/lib/json/pure/generator.rb:358:in `block in json_transform': Integer not allowed in JSON (JSON::GeneratorError)
	from /home/eregon/code/json/lib/json/pure/generator.rb:353:in `each'
	from /home/eregon/code/json/lib/json/pure/generator.rb:353:in `json_transform'
	from /home/eregon/code/json/lib/json/pure/generator.rb:337:in `to_json'
	from /home/eregon/code/json/lib/json/pure/generator.rb:289:in `generate'
	from /home/eregon/code/json/lib/json/common.rb:306:in `generate'
	from /home/eregon/code/json/lib/json/common.rb:627:in `dump'
	from -e:1:in `<main>'

But of course Integer should be allowed.

The bug seems clear when looking at

if state.strict?
raise GeneratorError, "#{value.class} not allowed in JSON"
elsif value.respond_to?(:to_json)
result << value.to_json(state)
else
result << %{"#{String(value)}"}
end

if state.strict?
raise GeneratorError, "#{value.class} not allowed in JSON"
elsif value.respond_to?(:to_json)
result << value.to_json(state)
else
result << %{"#{String(value)}"}
end

It was added in #519 but with too few tests so they didn't catch this bug.
@byroot / @casperisfine Could you fix this?

casperisfine pushed a commit to casperisfine/json that referenced this issue May 9, 2024
@casperisfine casperisfine linked a pull request May 9, 2024 that will close this issue
@casperisfine
Copy link

#585

casperisfine pushed a commit to casperisfine/json that referenced this issue May 9, 2024
casperisfine pushed a commit to casperisfine/json that referenced this issue May 9, 2024
casperisfine pushed a commit to casperisfine/json that referenced this issue May 9, 2024
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 a pull request may close this issue.

2 participants