Module: FastMultiJson
- Defined in:
- lib/fast_multi_json.rb,
lib/fast_multi_json/version.rb
Overview
Constant Summary collapse
- VERSION =
"1.1.0"
Class Method Summary collapse
- .define_from_json(receiver) ⇒ Object
- .define_to_json(receiver) ⇒ Object
- .from_json(string) ⇒ Object
-
.from_json_method ⇒ Object
Decoder-compatible with default MultiJSON adapters and defaults.
- .logger(logger = nil) ⇒ Object
- .reset_from_json! ⇒ 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_from_json(receiver) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/fast_multi_json.rb', line 80 def self.define_from_json(receiver) cl = caller_locations[0] method_body = from_json_method logger.debug { "Defining #{receiver}._fast_from_json as #{method_body.inspect}" } receiver.instance_eval method_body, cl.absolute_path, cl.lineno end |
.define_to_json(receiver) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/fast_multi_json.rb', line 36 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 |
.from_json(string) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/fast_multi_json.rb', line 29 def self.from_json(string) _fast_from_json(string) rescue NameError define_from_json(::FastMultiJson) _fast_from_json(string) end |
.from_json_method ⇒ Object
Decoder-compatible with default MultiJSON adapters and defaults
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/fast_multi_json.rb', line 88 def self.from_json_method decode_method = String.new(%(def _fast_from_json(string)\n )) decode_method << Result.new { require 'oj' %(::Oj.load(string, mode: :strict, symbol_keys: false)) }.rescue { require 'yajl' %(::Yajl::Parser.new(symbolize_keys: false).parse(string)) }.rescue { require 'jrjackson' unless defined?(::JrJackson) %(::JrJackson::Json.load(string)) }.rescue { require 'json' %(::JSON.parse(string, quirks_mode: false, symbolize_names: false)) }.rescue { require 'gson' %(::Gson::Decoder.new(::FastMultiJson::EMPTY_OPTIONS.dup).decode(string)) }.rescue { require 'active_support/json/decoding' %(::ActiveSupport::JSON.decode(string)) }.rescue { fail "No JSON decoder found." }.value! decode_method << "\nend" end |
.logger(logger = nil) ⇒ Object
17 18 19 20 |
# File 'lib/fast_multi_json.rb', line 17 def self.logger(logger=nil) return @logger = logger if logger @logger ||= Logger.new(IO::NULL) end |
.reset_from_json! ⇒ Object
114 115 116 117 |
# File 'lib/fast_multi_json.rb', line 114 def self.reset_from_json! undef :_fast_from_json if method_defined?(:_fast_from_json) logger.debug { "Undefining #{receiver}._fast_from_json" } end |
.reset_to_json! ⇒ Object
75 76 77 78 |
# File 'lib/fast_multi_json.rb', line 75 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
22 23 24 25 26 27 |
# File 'lib/fast_multi_json.rb', line 22 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
44 45 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 72 73 |
# File 'lib/fast_multi_json.rb', line 44 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, indent: 0)) }.rescue { require 'yajl' %(::Yajl::Encoder.encode(object)) }.rescue { require 'jrjackson' unless defined?(::JrJackson) if ::JrJackson::Json.method(:dump).arity == 1 %(::JrJackson::Json.dump(object)) else %(::JrJackson::Json.dump(object, ::FastMultiJson::EMPTY_OPTIONS.dup)) end }.rescue { require 'json' %(::JSON.fast_generate(object, create_additions: false, quirks_mode: false)) }.rescue { require 'gson' %(::Gson::Encoder.new(::FastMultiJson::EMPTY_OPTIONS.dup).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(::JSON::FAST_STATE_PROTOTYPE.to_h)) }.value! encode_method << "\nend" end |