2
require 'active_support/core_ext/module/delegation'
3
require 'active_support/deprecation'
7
delegate :use_standard_json_time_format, :use_standard_json_time_format=,
8
:escape_html_entities_in_json, :escape_html_entities_in_json=,
9
:to => :'ActiveSupport::JSON::Encoding'
13
# matches YAML-formatted dates
14
DATE_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[ \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?))$/
17
delegate :encode, :to => :'ActiveSupport::JSON::Encoding'
20
module Encoding #:nodoc:
21
class CircularReferenceError < StandardError
37
# If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format.
38
attr_accessor :use_standard_json_time_format
40
attr_accessor :escape_regex
41
attr_reader :escape_html_entities_in_json
43
def escape_html_entities_in_json=(value)
45
if @escape_html_entities_in_json = value
46
/[\010\f\n\r\t"\\><&]/
53
string = string.dup.force_encoding(::Encoding::BINARY) if string.respond_to?(:force_encoding)
55
gsub(escape_regex) { |s| ESCAPED_CHARS[s] }.
56
gsub(/([\xC0-\xDF][\x80-\xBF]|
57
[\xE0-\xEF][\x80-\xBF]{2}|
58
[\xF0-\xF7][\x80-\xBF]{3})+/nx) { |s|
59
s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/n, '\\\\u\&')
64
# Converts a Ruby object into a JSON string.
65
def encode(value, options = nil)
66
options = {} unless Hash === options
67
seen = (options[:seen] ||= [])
68
raise CircularReferenceError, 'object references itself' if seen.include?(value)
70
value.to_json(options)
76
self.escape_html_entities_in_json = true
79
CircularReferenceError = Deprecation::DeprecatedConstantProxy.new('ActiveSupport::JSON::CircularReferenceError', Encoding::CircularReferenceError)
83
# Hack to load json gem first so we can overwrite its to_json.
89
require 'active_support/json/variable'
90
require 'active_support/json/encoders/date'
91
require 'active_support/json/encoders/date_time'
92
require 'active_support/json/encoders/enumerable'
93
require 'active_support/json/encoders/false_class'
94
require 'active_support/json/encoders/hash'
95
require 'active_support/json/encoders/nil_class'
96
require 'active_support/json/encoders/numeric'
97
require 'active_support/json/encoders/object'
98
require 'active_support/json/encoders/regexp'
99
require 'active_support/json/encoders/string'
100
require 'active_support/json/encoders/symbol'
101
require 'active_support/json/encoders/time'
102
require 'active_support/json/encoders/true_class'