Skip to content
Merged
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
10 changes: 5 additions & 5 deletions lib/json/truffle_ruby/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def json_transform(state)

if empty?
state.depth -= 1
return '{}'
return +'{}'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be faster to do the +/.dup in .dump vs in every "empty case" as most of these do not need to be mutable.
Probably just .dup here would be enough:
https://github.com/rwstauner/json/blob/cd2f0195a2824bcb88e2173f1f4ecf7ec68528cc/lib/json/truffle_ruby/generator.rb#L352
I'll try it.

Though that wouldn't work if people expect obj.to_json to return a mutable String, but not sure there is such an expectation. Notably true.to_s is already frozen?, even on CRuby.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Though that wouldn't work if people expect obj.to_json to return a mutable String, but not sure there is such an expectation.

It's best to stay consistent with the C version that is largely used, and it does always return a mutable string.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mmh, OTOH it's natural to have frozen strings when implementing in Ruby and rather unusual to have frozen strings in C, so I wonder if the semantics here are more influenced by that than intentional. We have some differences in core classes in TruffleRuby like frozen exception messages and we made the choice to mostly keep them frozen.

I think it's worth doing #964, but if you disagree let me know and I'll just drop the first commit.
The second commit is safe regardless.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

so I wonder if the semantics here are more influenced by that than intentional.

Of course, but my point is that regardless of the intent, it has been this way for a long time and may cause a compatibility issue.

Which is worse because it means to_json would sometimes return frozen string sometimes mutable ones, meaning it's very easy to miss that case when testing. So I'm not in favor of method returning either, it has to be consistent.

It's not that rare to have code such as object.to_json << "\n".

end

delim = ",#{state.object_nl}"
Expand Down Expand Up @@ -609,7 +609,7 @@ def json_transform(state)

if empty?
state.depth -= 1
return '[]'
return +'[]'
end

result = '['.dup
Expand Down Expand Up @@ -734,17 +734,17 @@ def to_json(state = nil, *args)

module TrueClass
# Returns a JSON string for true: 'true'.
def to_json(*) 'true' end
def to_json(*) +'true' end
end

module FalseClass
# Returns a JSON string for false: 'false'.
def to_json(*) 'false' end
def to_json(*) +'false' end
end

module NilClass
# Returns a JSON string for nil: 'null'.
def to_json(*) 'null' end
def to_json(*) +'null' end
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ def test_dump_strict
assert_equal '"World"', "World".to_json(strict: true)
end

def test_not_frozen
[
[[], '[]'],
[{}, '{}'],
["string", '"string"'],
[:sym, '"sym"'],
[1, '1'],
[1.0, '1.0'],
[true, 'true'],
[false, 'false'],
[nil, 'null'],
].each do |(obj, exp)|
dumped = dump(obj, strict: true)
assert_equal exp, dumped
refute_predicate dumped, :frozen?
end
end

def test_state_depth_to_json
depth = Object.new
def depth.to_json(state)
Expand Down