2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2017-10-21 09:11:29 -04:00
|
|
|
require "active_support/core_ext/object/json"
|
|
|
|
require "active_support/core_ext/module/delegation"
|
2010-06-30 19:04:35 -04:00
|
|
|
|
2007-03-18 03:05:58 -04:00
|
|
|
module ActiveSupport
|
2009-06-05 21:25:07 -04:00
|
|
|
class << self
|
|
|
|
delegate :use_standard_json_time_format, :use_standard_json_time_format=,
|
2014-01-26 15:39:16 -05:00
|
|
|
:time_precision, :time_precision=,
|
2009-06-05 21:25:07 -04:00
|
|
|
:escape_html_entities_in_json, :escape_html_entities_in_json=,
|
2013-11-20 12:21:38 -05:00
|
|
|
:json_encoder, :json_encoder=,
|
2016-08-06 13:38:33 -04:00
|
|
|
to: :'ActiveSupport::JSON::Encoding'
|
2009-06-05 21:25:07 -04:00
|
|
|
end
|
|
|
|
|
2007-03-18 03:05:58 -04:00
|
|
|
module JSON
|
2012-09-14 23:47:21 -04:00
|
|
|
# Dumps objects in JSON (JavaScript Object Notation).
|
2014-10-15 10:15:20 -04:00
|
|
|
# See http://www.json.org for more info.
|
2012-05-28 04:10:51 -04:00
|
|
|
#
|
2012-09-14 23:47:21 -04:00
|
|
|
# ActiveSupport::JSON.encode({ team: 'rails', players: '36' })
|
2012-05-28 04:10:51 -04:00
|
|
|
# # => "{\"team\":\"rails\",\"players\":\"36\"}"
|
2009-06-05 21:25:07 -04:00
|
|
|
def self.encode(value, options = nil)
|
2013-11-20 12:21:38 -05:00
|
|
|
Encoding.json_encoder.new(options).encode(value)
|
2007-03-18 03:05:58 -04:00
|
|
|
end
|
|
|
|
|
2021-07-29 17:18:07 -04:00
|
|
|
module Encoding # :nodoc:
|
|
|
|
class JSONGemEncoder # :nodoc:
|
2013-02-25 00:21:45 -05:00
|
|
|
attr_reader :options
|
2009-06-05 21:25:07 -04:00
|
|
|
|
|
|
|
def initialize(options = nil)
|
2011-09-30 06:45:36 -04:00
|
|
|
@options = options || {}
|
2009-06-05 21:25:07 -04:00
|
|
|
end
|
|
|
|
|
2013-11-19 22:47:34 -05:00
|
|
|
# Encode the given object into a JSON string
|
2013-11-06 00:01:38 -05:00
|
|
|
def encode(value)
|
2013-11-19 22:47:34 -05:00
|
|
|
stringify jsonify value.as_json(options.dup)
|
2009-06-05 21:25:07 -04:00
|
|
|
end
|
|
|
|
|
2013-11-19 22:47:34 -05:00
|
|
|
private
|
|
|
|
# Rails does more escaping than the JSON gem natively does (we
|
|
|
|
# escape \u2028 and \u2029 and optionally >, <, & to work around
|
|
|
|
# certain browser problems).
|
|
|
|
ESCAPED_CHARS = {
|
|
|
|
"\u2028" => '\u2028',
|
|
|
|
"\u2029" => '\u2029',
|
2016-08-06 11:58:50 -04:00
|
|
|
">" => '\u003e',
|
|
|
|
"<" => '\u003c',
|
|
|
|
"&" => '\u0026',
|
2013-11-19 22:47:34 -05:00
|
|
|
}
|
2009-06-05 21:25:07 -04:00
|
|
|
|
2013-11-19 22:47:34 -05:00
|
|
|
ESCAPE_REGEX_WITH_HTML_ENTITIES = /[\u2028\u2029><&]/u
|
|
|
|
ESCAPE_REGEX_WITHOUT_HTML_ENTITIES = /[\u2028\u2029]/u
|
2009-06-05 21:25:07 -04:00
|
|
|
|
2013-11-19 22:47:34 -05:00
|
|
|
# This class wraps all the strings we see and does the extra escaping
|
2021-07-29 17:18:07 -04:00
|
|
|
class EscapedString < String # :nodoc:
|
2013-11-19 22:47:34 -05:00
|
|
|
def to_json(*)
|
|
|
|
if Encoding.escape_html_entities_in_json
|
2018-04-26 14:57:08 -04:00
|
|
|
s = super
|
|
|
|
s.gsub! ESCAPE_REGEX_WITH_HTML_ENTITIES, ESCAPED_CHARS
|
|
|
|
s
|
2013-11-19 22:47:34 -05:00
|
|
|
else
|
2018-04-26 14:57:08 -04:00
|
|
|
s = super
|
|
|
|
s.gsub! ESCAPE_REGEX_WITHOUT_HTML_ENTITIES, ESCAPED_CHARS
|
|
|
|
s
|
2013-11-19 22:47:34 -05:00
|
|
|
end
|
|
|
|
end
|
2015-06-15 14:23:01 -04:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
self
|
|
|
|
end
|
2013-11-19 22:47:34 -05:00
|
|
|
end
|
2009-06-05 21:25:07 -04:00
|
|
|
|
2013-11-19 22:47:34 -05:00
|
|
|
# Mark these as private so we don't leak encoding-specific constructs
|
2013-07-16 16:16:33 -04:00
|
|
|
private_constant :ESCAPED_CHARS, :ESCAPE_REGEX_WITH_HTML_ENTITIES,
|
2013-11-19 22:47:34 -05:00
|
|
|
:ESCAPE_REGEX_WITHOUT_HTML_ENTITIES, :EscapedString
|
2009-06-05 21:25:07 -04:00
|
|
|
|
2013-12-12 01:25:17 -05:00
|
|
|
# Convert an object into a "JSON-ready" representation composed of
|
2016-10-26 17:13:15 -04:00
|
|
|
# primitives like Hash, Array, String, Numeric,
|
|
|
|
# and +true+/+false+/+nil+.
|
2013-12-12 01:25:17 -05:00
|
|
|
# Recursively calls #as_json to the object to recursively build a
|
|
|
|
# fully JSON-ready object.
|
|
|
|
#
|
|
|
|
# This allows developers to implement #as_json without having to
|
|
|
|
# worry about what base types of objects they are allowed to return
|
|
|
|
# or having to remember to call #as_json recursively.
|
|
|
|
#
|
|
|
|
# Note: the +options+ hash passed to +object.to_json+ is only passed
|
|
|
|
# to +object.as_json+, not any of this method's recursive +#as_json+
|
|
|
|
# calls.
|
2013-11-19 22:47:34 -05:00
|
|
|
def jsonify(value)
|
2013-12-12 01:25:17 -05:00
|
|
|
case value
|
|
|
|
when String
|
2013-11-19 22:47:34 -05:00
|
|
|
EscapedString.new(value)
|
2013-12-12 01:25:17 -05:00
|
|
|
when Numeric, NilClass, TrueClass, FalseClass
|
2016-10-30 04:42:43 -04:00
|
|
|
value.as_json
|
2013-12-12 01:25:17 -05:00
|
|
|
when Hash
|
2020-01-11 06:03:20 -05:00
|
|
|
result = {}
|
|
|
|
value.each do |k, v|
|
|
|
|
result[jsonify(k)] = jsonify(v)
|
|
|
|
end
|
|
|
|
result
|
2013-12-12 01:25:17 -05:00
|
|
|
when Array
|
|
|
|
value.map { |v| jsonify(v) }
|
2009-06-05 21:25:07 -04:00
|
|
|
else
|
2013-11-19 22:47:34 -05:00
|
|
|
jsonify value.as_json
|
2009-06-05 21:25:07 -04:00
|
|
|
end
|
2013-11-19 22:47:34 -05:00
|
|
|
end
|
2009-06-05 21:25:07 -04:00
|
|
|
|
2013-11-19 22:47:34 -05:00
|
|
|
# Encode a "jsonified" Ruby data structure using the JSON gem
|
|
|
|
def stringify(jsonified)
|
|
|
|
::JSON.generate(jsonified, quirks_mode: true, max_nesting: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
# If true, use ISO 8601 format for dates and times. Otherwise, fall back
|
|
|
|
# to the Active Support legacy format.
|
|
|
|
attr_accessor :use_standard_json_time_format
|
|
|
|
|
|
|
|
# If true, encode >, <, & as escaped unicode sequences (e.g. > as \u003e)
|
|
|
|
# as a safety measure.
|
|
|
|
attr_accessor :escape_html_entities_in_json
|
2013-11-06 00:01:38 -05:00
|
|
|
|
2014-01-26 15:39:16 -05:00
|
|
|
# Sets the precision of encoded time values.
|
|
|
|
# Defaults to 3 (equivalent to millisecond precision)
|
|
|
|
attr_accessor :time_precision
|
2013-07-16 16:16:33 -04:00
|
|
|
|
2013-11-20 12:21:38 -05:00
|
|
|
# Sets the encoder used by Rails to encode Ruby objects into JSON strings
|
|
|
|
# in +Object#to_json+ and +ActiveSupport::JSON.encode+.
|
|
|
|
attr_accessor :json_encoder
|
2009-06-05 21:25:07 -04:00
|
|
|
end
|
|
|
|
|
2010-01-03 22:20:43 -05:00
|
|
|
self.use_standard_json_time_format = true
|
2012-05-14 02:02:45 -04:00
|
|
|
self.escape_html_entities_in_json = true
|
2013-11-20 12:21:38 -05:00
|
|
|
self.json_encoder = JSONGemEncoder
|
2014-01-26 16:09:06 -05:00
|
|
|
self.time_precision = 3
|
2009-06-05 21:25:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|