@@ -20,8 +20,8 @@ class << self
20
20
# ActiveSupport::JSON.encode({ team: 'rails', players: '36' })
21
21
# # => "{\"team\":\"rails\",\"players\":\"36\"}"
22
22
#
23
- # Generates JSON that is safe to include in JavaScript as it escapes
24
- # U+2028 (Line Separator) and U+2029 (Paragraph Separator):
23
+ # By default, it generates JSON that is safe to include in JavaScript, as
24
+ # it escapes U+2028 (Line Separator) and U+2029 (Paragraph Separator):
25
25
#
26
26
# ActiveSupport::JSON.encode({ key: "\u2028" })
27
27
# # => "{\"key\":\"\\u2028\"}"
@@ -32,11 +32,17 @@ class << self
32
32
# ActiveSupport::JSON.encode({ key: "<>&" })
33
33
# # => "{\"key\":\"\\u003c\\u003e\\u0026\"}"
34
34
#
35
- # This can be changed with the +escape_html_entities+ option, or the
35
+ # This behavior can be changed with the +escape_html_entities+ option, or the
36
36
# global escape_html_entities_in_json configuration option.
37
37
#
38
38
# ActiveSupport::JSON.encode({ key: "<>&" }, escape_html_entities: false)
39
39
# # => "{\"key\":\"<>&\"}"
40
+ #
41
+ # For performance reasons, you can set the +escape+ option to false,
42
+ # which will skip all escaping:
43
+ #
44
+ # ActiveSupport::JSON.encode({ key: "\u2028<>&" }, escape: false)
45
+ # # => "{\"key\":\"\u2028<>&\"}"
40
46
def encode ( value , options = nil )
41
47
if options . nil? || options . empty?
42
48
Encoding . encode_without_options ( value )
@@ -76,6 +82,8 @@ def encode(value)
76
82
end
77
83
json = stringify ( jsonify ( value ) )
78
84
85
+ return json unless @options . fetch ( :escape , true )
86
+
79
87
# Rails does more escaping than the JSON gem natively does (we
80
88
# escape \u2028 and \u2029 and optionally >, <, & to work around
81
89
# certain browser problems).
@@ -162,6 +170,8 @@ def encode(value)
162
170
163
171
json = CODER . dump ( value )
164
172
173
+ return json unless @options . fetch ( :escape , true )
174
+
165
175
# Rails does more escaping than the JSON gem natively does (we
166
176
# escape \u2028 and \u2029 and optionally >, <, & to work around
167
177
# certain browser problems).
0 commit comments