Skip to content

Commit 424b9b5

Browse files
authored
Merge pull request rails#54480 from etiennebarrie/json-skip-allocating-encoder
Skip allocation when JSON options are nil
2 parents 118b39e + b54385c commit 424b9b5

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

activesupport/lib/active_support/json/encoding.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ module JSON
2020
# # => "{\"team\":\"rails\",\"players\":\"36\"}"
2121
class << self
2222
def encode(value, options = nil)
23-
Encoding.json_encoder.new(options).encode(value)
23+
if options.nil?
24+
Encoding.encode_without_options(value)
25+
else
26+
Encoding.json_encoder.new(options).encode(value)
27+
end
2428
end
2529
alias_method :dump, :encode
2630
end
@@ -108,7 +112,16 @@ class << self
108112

109113
# Sets the encoder used by \Rails to encode Ruby objects into JSON strings
110114
# in +Object#to_json+ and +ActiveSupport::JSON.encode+.
111-
attr_accessor :json_encoder
115+
attr_reader :json_encoder
116+
117+
def json_encoder=(encoder)
118+
@json_encoder = encoder
119+
@encoder_without_options = encoder.new
120+
end
121+
122+
def encode_without_options(value) # :nodoc:
123+
@encoder_without_options.encode(value)
124+
end
112125
end
113126

114127
self.use_standard_json_time_format = true

0 commit comments

Comments
 (0)