Skip to content

Commit 6447fbe

Browse files
etiennebarriebyroot
andcommitted
Improve ActiveSupport::JSON.encode documentation
Co-authored-by: Jean Boussier <[email protected]>
1 parent 9f16995 commit 6447fbe

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

activesupport/lib/active_support/json/encoding.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,30 @@ class << self
1313
end
1414

1515
module JSON
16-
# Dumps objects in JSON (JavaScript Object Notation).
17-
# See http://www.json.org for more info.
18-
#
19-
# ActiveSupport::JSON.encode({ team: 'rails', players: '36' })
20-
# # => "{\"team\":\"rails\",\"players\":\"36\"}"
2116
class << self
17+
# Dumps objects in JSON (JavaScript Object Notation).
18+
# See http://www.json.org for more info.
19+
#
20+
# ActiveSupport::JSON.encode({ team: 'rails', players: '36' })
21+
# # => "{\"team\":\"rails\",\"players\":\"36\"}"
22+
#
23+
# Generates JSON that is safe to include in JavaScript as it escapes
24+
# U+2028 (Line Separator) and U+2029 (Paragraph Separator):
25+
#
26+
# ActiveSupport::JSON.encode({ key: "\u2028" })
27+
# # => "{\"key\":\"\\u2028\"}"
28+
#
29+
# By default, it also generates JSON that is safe to include in HTML, as
30+
# it escapes <tt><</tt>, <tt>></tt>, and <tt>&</tt>:
31+
#
32+
# ActiveSupport::JSON.encode({ key: "<>&" })
33+
# # => "{\"key\":\"\\u003c\\u003e\\u0026\"}"
34+
#
35+
# This can be changed with the +escape_html_entities+ option, or the
36+
# global escape_html_entities_in_json configuration option.
37+
#
38+
# ActiveSupport::JSON.encode({ key: "<>&" }, escape_html_entities: false)
39+
# # => "{\"key\":\"<>&\"}"
2240
def encode(value, options = nil)
2341
if options.nil?
2442
Encoding.encode_without_options(value)

0 commit comments

Comments
 (0)