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
3 changes: 3 additions & 0 deletions lib/json/pure/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ module String
def to_json(state = nil, *args)
state = State.from_state(state)
if encoding == ::Encoding::UTF_8
unless valid_encoding?
raise GeneratorError, "source sequence is illegal/malformed utf-8"
Copy link
Member

Choose a reason for hiding this comment

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

We also need it in fast_serialize_string: #633

end
string = self
else
string = encode(::Encoding::UTF_8)
Expand Down
7 changes: 7 additions & 0 deletions tests/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,13 @@ def to_s; self; end
end
end

def test_invalid_encoding_string
error = assert_raise(JSON::GeneratorError) do
"\x82\xAC\xEF".to_json
end
assert_includes error.message, "source sequence is illegal/malformed utf-8"
end

if defined?(JSON::Ext::Generator) and RUBY_PLATFORM != "java"
def test_string_ext_included_calls_super
included = false
Expand Down
Loading