Module: FastMultiJson
- Defined in:
- lib/fast_multi_json.rb,
lib/fast_multi_json/version.rb
Overview
Usage:
class Movie
def to_json(payload)
FastMultiJson.to_json(payload)
end
end
Constant Summary collapse
- VERSION =
"1.0.0"
Class Method Summary collapse
- .define_to_json(receiver) ⇒ Object
- .logger(logger = nil) ⇒ Object
- .reset_to_json! ⇒ Object
- .to_json(object) ⇒ Object
-
.to_json_method ⇒ Object
Encoder-compatible with default MultiJSON adapters and defaults.
Class Method Details
.define_to_json(receiver) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/fast_multi_json.rb', line 80 def self.define_to_json(receiver) cl = caller_locations[0] method_body = to_json_method logger.debug { "Defining #{receiver}._fast_to_json as #{method_body.inspect}" } receiver.instance_eval method_body, cl.absolute_path, cl.lineno end |
.logger(logger = nil) ⇒ Object
40 41 42 43 |
# File 'lib/fast_multi_json.rb', line 40 def self.logger(logger=nil) return @logger = logger if logger @logger ||= Logger.new(IO::NULL) end |
.reset_to_json! ⇒ Object
87 88 89 90 |
# File 'lib/fast_multi_json.rb', line 87 def self.reset_to_json! undef :_fast_to_json if method_defined?(:_fast_to_json) logger.debug { "Undefining #{receiver}._fast_to_json" } end |
.to_json(object) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/fast_multi_json.rb', line 73 def self.to_json(object) _fast_to_json(object) rescue NameError define_to_json(FastMultiJson) _fast_to_json(object) end |
.to_json_method ⇒ Object
Encoder-compatible with default MultiJSON adapters and defaults
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/fast_multi_json.rb', line 46 def self.to_json_method encode_method = String.new(%(def _fast_to_json(object)\n )) encode_method << Result.new { require 'oj' %(::Oj.dump(object, mode: :compat, time_format: :ruby, use_to_json: true)) }.rescue { require 'yajl' %(::Yajl::Encoder.encode(object)) }.rescue { require 'jrjackson' unless defined?(::JrJackson) %(::JrJackson::Json.dump(object)) }.rescue { require 'json' %(::JSON.fast_generate(object, create_additions: false, quirks_mode: true)) }.rescue { require 'gson' %(::Gson::Encoder.new({}).encode(object)) }.rescue { require 'active_support/json/encoding' %(::ActiveSupport::JSON.encode(object)) }.rescue { warn "No JSON encoder found. Falling back to `object.to_json`" %(object.to_json) }.value! encode_method << "\nend" end |