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

Optimize Symbol generation in strict mode #745

Merged
merged 1 commit into from
Feb 5, 2025

Conversation

etiennebarrie
Copy link
Contributor

In strict mode we don't need to check for to_json, we can just generate a String from a Symbol.

It changes behavior because previously Symbol caused a generator error. But keys in hashes already had support for automatic coercion to String, so it feels ok to do.

@etiennebarrie etiennebarrie force-pushed the optimize-symbol-generation branch from b3a8d5b to a43983e Compare February 5, 2025 11:41
@byroot byroot force-pushed the optimize-symbol-generation branch from a43983e to 1bb591e Compare February 5, 2025 11:48
ext/json/ext/generator/generator.c Outdated Show resolved Hide resolved
ext/json/ext/generator/generator.c Outdated Show resolved Hide resolved
@etiennebarrie etiennebarrie force-pushed the optimize-symbol-generation branch from 1bb591e to 8e0f3b7 Compare February 5, 2025 13:03
@byroot byroot force-pushed the optimize-symbol-generation branch 2 times, most recently from 50a87ba to 4e2078a Compare February 5, 2025 13:11
@byroot byroot requested a review from eregon February 5, 2025 13:11
ext/json/ext/generator/generator.c Show resolved Hide resolved
lib/json/truffle_ruby/generator.rb Show resolved Hide resolved
@@ -86,6 +86,9 @@ def test_dump_strict

assert_equal '42', dump(42, strict: true)
assert_equal 'true', dump(true, strict: true)

assert_equal '"hello"', dump(:hello, strict: true)
assert_equal '"World"', dump("World".to_sym, strict: true)
Copy link
Member

Choose a reason for hiding this comment

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

Probably should test that to_json is not called with strict

Copy link
Member

Choose a reason for hiding this comment

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

It's somewhat already asserting that because json/add/symbol is loaded in the test suite.

@byroot byroot force-pushed the optimize-symbol-generation branch from 4e2078a to d8b0960 Compare February 5, 2025 13:51
Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
@byroot byroot force-pushed the optimize-symbol-generation branch from d8b0960 to d37638e Compare February 5, 2025 13:57
@byroot byroot merged commit 2fb293d into ruby:master Feb 5, 2025
33 checks passed
@eregon
Copy link
Member

eregon commented Feb 5, 2025

FWIW I realized this means JSON.load(JSON.dump(obj, strict: true), strict: true) does no longer always round-trip cleanly (if no exception), but I'm not sure how much that matters.

@byroot
Copy link
Member

byroot commented Feb 5, 2025

I think that ship has sailed when symbols were accepted as hash keys regardless.

But yes, we could chose to do the opposite and reject them in strict mode. One goal of strict: true was indeed to help with roundtripping :/

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.

3 participants