Skip to content

Optimize JSON escaping #54484

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

Merged
merged 1 commit into from
Feb 12, 2025
Merged
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
36 changes: 24 additions & 12 deletions activesupport/lib/active_support/json/encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ def encode(value, options = nil)
end

module Encoding # :nodoc:
U2028 = -"\u2028".b
U2029 = -"\u2029".b

ESCAPED_CHARS = {
U2028 => '\u2028'.b,
U2029 => '\u2029'.b,
">".b => '\u003e'.b,
"<".b => '\u003c'.b,
"&".b => '\u0026'.b,
}

ESCAPE_REGEX_WITH_HTML_ENTITIES = Regexp.union(*ESCAPED_CHARS.keys)
ESCAPE_REGEX_WITHOUT_HTML_ENTITIES = Regexp.union(U2028, U2029)

class JSONGemEncoder # :nodoc:
attr_reader :options

Expand All @@ -47,14 +61,13 @@ def encode(value)
# Rails does more escaping than the JSON gem natively does (we
# escape \u2028 and \u2029 and optionally >, <, & to work around
# certain browser problems).
json.force_encoding(::Encoding::BINARY)
if @options.fetch(:escape_html_entities, Encoding.escape_html_entities_in_json)
json.gsub!(">", '\u003e')
json.gsub!("<", '\u003c')
json.gsub!("&", '\u0026')
json.gsub!(ESCAPE_REGEX_WITH_HTML_ENTITIES, ESCAPED_CHARS)
else
json.gsub!(ESCAPE_REGEX_WITHOUT_HTML_ENTITIES, ESCAPED_CHARS)
end
json.gsub!("\u2028", '\u2028')
json.gsub!("\u2029", '\u2029')
json
json.force_encoding(::Encoding::UTF_8)
end

private
Expand Down Expand Up @@ -130,14 +143,13 @@ def encode(value)
# Rails does more escaping than the JSON gem natively does (we
# escape \u2028 and \u2029 and optionally >, <, & to work around
# certain browser problems).
json.force_encoding(::Encoding::BINARY)
if @options.fetch(:escape_html_entities, Encoding.escape_html_entities_in_json)
json.gsub!(">", '\u003e')
json.gsub!("<", '\u003c')
json.gsub!("&", '\u0026')
json.gsub!(ESCAPE_REGEX_WITH_HTML_ENTITIES, ESCAPED_CHARS)
else
json.gsub!(ESCAPE_REGEX_WITHOUT_HTML_ENTITIES, ESCAPED_CHARS)
end
json.gsub!("\u2028", '\u2028')
json.gsub!("\u2029", '\u2029')
json
json.force_encoding(::Encoding::UTF_8)
end
end
end
Expand Down