File tree Expand file tree Collapse file tree 3 files changed +30
-1
lines changed Expand file tree Collapse file tree 3 files changed +30
-1
lines changed Original file line number Diff line number Diff line change
1
+ * Add ` escape_html_entities ` option to ` ActiveSupport::JSON.encode ` .
2
+
3
+ This allows for overriding the global configuration found at
4
+ ` ActiveSupport.escape_html_entities_in_json ` for specific calls to ` to_json ` .
5
+
6
+ This should be usable from controllers in the following manner:
7
+ ``` ruby
8
+ class MyController < ApplicationController
9
+ def index
10
+ render json: { hello: " world" }, escape_html_entities: false
11
+ end
12
+ end
13
+ ```
14
+
15
+ * Nigel Baillie *
16
+
1
17
* Raise when using key which can' t respond to `#to_sym` in `EncryptedConfiguration`.
2
18
3
19
As is the case when trying to use an Integer or Float as a key, which is unsupported.
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ class JSONGemEncoder # :nodoc:
31
31
32
32
def initialize ( options = nil )
33
33
@options = options || { }
34
+
34
35
end
35
36
36
37
# Encode the given object into a JSON string
@@ -43,7 +44,7 @@ def encode(value)
43
44
# Rails does more escaping than the JSON gem natively does (we
44
45
# escape \u2028 and \u2029 and optionally >, <, & to work around
45
46
# certain browser problems).
46
- if Encoding . escape_html_entities_in_json
47
+ if @options . fetch ( :escape_html_entities , Encoding . escape_html_entities_in_json )
47
48
json . gsub! ( ">" , '\u003e' )
48
49
json . gsub! ( "<" , '\u003c' )
49
50
json . gsub! ( "&" , '\u0026' )
Original file line number Diff line number Diff line change @@ -59,6 +59,18 @@ def test_hash_keys_encoding
59
59
ActiveSupport . escape_html_entities_in_json = false
60
60
end
61
61
62
+ def test_hash_keys_encoding_option
63
+ global_config = ActiveSupport . escape_html_entities_in_json
64
+
65
+ ActiveSupport . escape_html_entities_in_json = true
66
+ assert_equal "{\" <>\" :\" <>\" }" , ActiveSupport ::JSON . encode ( { "<>" => "<>" } , escape_html_entities : false )
67
+
68
+ ActiveSupport . escape_html_entities_in_json = false
69
+ assert_equal "{\" \\ u003c\\ u003e\" :\" \\ u003c\\ u003e\" }" , ActiveSupport ::JSON . encode ( { "<>" => "<>" } , escape_html_entities : true )
70
+ ensure
71
+ ActiveSupport . escape_html_entities_in_json = global_config
72
+ end
73
+
62
74
def test_utf8_string_encoded_properly
63
75
result = ActiveSupport ::JSON . encode ( "€2.99" )
64
76
assert_equal '"€2.99"' , result
You can’t perform that action at this time.
0 commit comments