Skip to content

Commit ea17416

Browse files
etiennebarriebyroot
andcommitted
Add fast-path for Object#to_json(escape: false)
Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
1 parent 80827ca commit ea17416

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

activesupport/lib/active_support/json/encoding.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class << self
4444
# ActiveSupport::JSON.encode({ key: "\u2028<>&" }, escape: false)
4545
# # => "{\"key\":\"\u2028<>&\"}"
4646
def encode(value, options = nil)
47-
if options.nil? || options.empty?
47+
if options.nil? || options.empty? || options == { escape: false }.freeze
4848
Encoding.encode_without_options(value)
4949
else
5050
Encoding.json_encoder.new(options).encode(value)
@@ -161,7 +161,14 @@ class JSONGemCoderEncoder # :nodoc:
161161

162162

163163
def initialize(options = nil)
164-
@options = options ? options.dup.freeze : {}.freeze
164+
if options
165+
options = options.dup
166+
@escape = options.delete(:escape) { true }
167+
@options = options.freeze
168+
else
169+
@escape = true
170+
@options = {}.freeze
171+
end
165172
end
166173

167174
# Encode the given object into a JSON string
@@ -170,7 +177,7 @@ def encode(value)
170177

171178
json = CODER.dump(value)
172179

173-
return json unless @options.fetch(:escape, true)
180+
return json unless @escape
174181

175182
# Rails does more escaping than the JSON gem natively does (we
176183
# escape \u2028 and \u2029 and optionally >, <, & to work around

0 commit comments

Comments
 (0)