require'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'endmoduleJSON# 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\"}"defself.encode(value,options=nil)Encoding.json_encoder.new(options).encode(value)endmoduleEncoding#:nodoc:classJSONGemEncoder#:nodoc:attr_reader:optionsdefinitialize(options=nil)@options=options||{}end# Encode the given object into a JSON stringdefencode(value)stringifyjsonifyvalue.as_json(options.dup)endprivate# 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','>'=>'\u003e','<'=>'\u003c','&'=>'\u0026',}ESCAPE_REGEX_WITH_HTML_ENTITIES=/[\u2028\u2029><&]/uESCAPE_REGEX_WITHOUT_HTML_ENTITIES=/[\u2028\u2029]/u# This class wraps all the strings we see and does the extra escapingclassEscapedString<String#:nodoc:defto_json(*)ifEncoding.escape_html_entities_in_jsonsuper.gsubESCAPE_REGEX_WITH_HTML_ENTITIES,ESCAPED_CHARSelsesuper.gsubESCAPE_REGEX_WITHOUT_HTML_ENTITIES,ESCAPED_CHARSendenddefto_sselfendend# Mark these as private so we don't leak encoding-specific constructsprivate_constant:ESCAPED_CHARS,:ESCAPE_REGEX_WITH_HTML_ENTITIES,:ESCAPE_REGEX_WITHOUT_HTML_ENTITIES,:EscapedString# Convert an object into a "JSON-ready" representation composed of# primitives like Hash, Array, String, 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)casevaluewhenStringEscapedString.new(value)whenNumeric,NilClass,TrueClass,FalseClassvaluewhenHashHash[value.map{|k,v|[jsonify(k),jsonify(v)]}]whenArrayvalue.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