1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Make the JSON encoder pluggable

This commit is contained in:
Godfrey Chan 2013-11-20 09:21:38 -08:00
parent 80e7552073
commit d4ef6c0029

View file

@ -5,6 +5,7 @@ module ActiveSupport
class << self
delegate :use_standard_json_time_format, :use_standard_json_time_format=,
:escape_html_entities_in_json, :escape_html_entities_in_json=,
:json_encoder, :json_encoder=,
:to => :'ActiveSupport::JSON::Encoding'
end
@ -15,11 +16,11 @@ module ActiveSupport
# ActiveSupport::JSON.encode({ team: 'rails', players: '36' })
# # => "{\"team\":\"rails\",\"players\":\"36\"}"
def self.encode(value, options = nil)
Encoding::Encoder.new(options).encode(value)
Encoding.json_encoder.new(options).encode(value)
end
module Encoding #:nodoc:
class Encoder #:nodoc:
class JSONGemEncoder #:nodoc:
attr_reader :options
def initialize(options = nil)
@ -108,6 +109,10 @@ module ActiveSupport
# as a safety measure.
attr_accessor :escape_html_entities_in_json
# 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
# Deprecate CircularReferenceError
def const_missing(name)
if name == :CircularReferenceError
@ -133,6 +138,7 @@ module ActiveSupport
self.use_standard_json_time_format = true
self.escape_html_entities_in_json = true
self.json_encoder = JSONGemEncoder
end
end
end