# frozen_string_literal: truerequire"active_support/core_ext/object/json"require"active_support/core_ext/module/delegation"moduleActiveSupportclass<<selfdelegate:use_standard_json_time_format,:use_standard_json_time_format=,:time_precision,:time_precision=,:escape_html_entities_in_json,:escape_html_entities_in_json=,:json_encoder,:json_encoder=,to: :'ActiveSupport::JSON::Encoding'endmoduleJSONclass<<self# Dumps objects in JSON (JavaScript Object Notation).# See http://www.json.org for more info.## ActiveSupport::JSON.encode({ team: 'rails', players: '36' })# # => "{\"team\":\"rails\",\"players\":\"36\"}"## Generates JSON that is safe to include in JavaScript as it escapes# U+2028 (Line Separator) and U+2029 (Paragraph Separator):## ActiveSupport::JSON.encode({ key: "\u2028" })# # => "{\"key\":\"\\u2028\"}"## By default, it also generates JSON that is safe to include in HTML, as# it escapes <tt><</tt>, <tt>></tt>, and <tt>&</tt>:## ActiveSupport::JSON.encode({ key: "<>&" })# # => "{\"key\":\"\\u003c\\u003e\\u0026\"}"## This can be changed with the +escape_html_entities+ option, or the# global escape_html_entities_in_json configuration option.## ActiveSupport::JSON.encode({ key: "<>&" }, escape_html_entities: false)# # => "{\"key\":\"<>&\"}"defencode(value,options=nil)Encoding.json_encoder.new(options).encode(value)endalias_method:dump,:encodeendmoduleEncoding# :nodoc:classJSONGemEncoder# :nodoc:attr_reader:optionsdefinitialize(options=nil)@options=options||{}end# Encode the given object into a JSON stringdefencode(value)unlessoptions.empty?value=value.as_json(options.dup.freeze)endjson=stringify(jsonify(value))# Rails does more escaping than the JSON gem natively does (we# escape \u2028 and \u2029 and optionally >, <, & to work around# certain browser problems).if@options.fetch(:escape_html_entities,Encoding.escape_html_entities_in_json)json.gsub!(">",'\u003e')json.gsub!("<",'\u003c')json.gsub!("&",'\u0026')endjson.gsub!("\u2028",'\u2028')json.gsub!("\u2029",'\u2029')jsonendprivate# Convert an object into a "JSON-ready" representation composed of# primitives like Hash, Array, String, Symbol, Numeric,# and +true+/+false+/+nil+.# 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.defjsonify(value)casevaluewhenString,Integer,Symbol,nil,true,falsevaluewhenNumericvalue.as_jsonwhenHashresult={}value.eachdo|k,v|k=k.to_sunlessSymbol===k||String===kresult[k]=jsonify(v)endresultwhenArrayvalue.map{|v|jsonify(v)}elsejsonifyvalue.as_jsonendend# Encode a "jsonified" Ruby data structure using the JSON gemdefstringify(jsonified)::JSON.generate(jsonified,quirks_mode: true,max_nesting: false)endendclass<<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# Sets the precision of encoded time values.# Defaults to 3 (equivalent to millisecond precision)attr_accessor:time_precision# Sets the encoder used by \Rails to encode Ruby objects into JSON strings# in +Object#to_json+ and +ActiveSupport::JSON.encode+.attr_accessor:json_encoderendself.use_standard_json_time_format=trueself.escape_html_entities_in_json=trueself.json_encoder=JSONGemEncoderself.time_precision=3endendend