Class: TingYun::Support::Serialize::JSONWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/ting_yun/support/serialize/json_wrapper.rb

Class Method Summary collapse

Class Method Details

.backend_nameObject



52
53
54
# File 'lib/ting_yun/support/serialize/json_wrapper.rb', line 52

def self.backend_name
  @backend_name
end

.dump(object, options = {}) ⇒ Object



60
61
62
63
64
65
# File 'lib/ting_yun/support/serialize/json_wrapper.rb', line 60

def self.dump(object, options={})
  object = normalize(object) if options[:normalize]
  # ok_json doesn't handle symbol keys, so we must stringify them before encoding
  object = TingYun::Support::HashExtensions.stringify_keys_in_object(object) if backend_name == :ok_json
  @dump_method.call(object)
end

.load(string) ⇒ Object



67
68
69
# File 'lib/ting_yun/support/serialize/json_wrapper.rb', line 67

def self.load(string)
  @load_method.call(string)
end

.load_native_jsonObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ting_yun/support/serialize/json_wrapper.rb', line 12

def self.load_native_json
  begin
    require 'json' unless defined?(::JSON)

    # yajl's replacement methods on ::JSON override both dump and generate.
    # Because stdlib dump just calls generate, we end up calling into yajl
    # when we don't want to. As such, we use generate directly instead of
    # dump, although we have to fuss with defaults to make that ok.
    generate_method = ::JSON.method(:generate)
    if ::JSON.respond_to?(:dump_default_options)
      options = ::JSON.dump_default_options
    else
      # These were the defaults from json 1.1.9 up to 1.6.1
      options = {:allow_nan => true, :max_nesting => false, :quirks_mode => true}
    end
    @dump_method = Proc.new do |obj|
      generate_method.call(obj, options)
    end

    @load_method = ::JSON.method(:load)
    @backend_name = :json
    return true
  rescue StandardError, ScriptError => err
    TingYun::Agent.logger.debug "%p while loading JSON library: %s" % [err, err.message] if defined?(TingYun::Agent) && TingYun::Agent.respond_to?(:logger)
  end
end

.load_ok_jsonObject



39
40
41
42
43
44
# File 'lib/ting_yun/support/serialize/json_wrapper.rb', line 39

def self.load_ok_json
  require 'ting_yun/support/serialize/ok_json'
  @load_method = OkJson.method(:decode)
  @dump_method = OkJson.method(:encode)
  @backend_name = :ok_json
end

.normalize(o) ⇒ Object



71
72
73
# File 'lib/ting_yun/support/serialize/json_wrapper.rb', line 71

def self.normalize(o)
  TingYun::Support::Serialize::EncodingNormalizer.normalize_object(o)
end

.supports_normalization?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/ting_yun/support/serialize/json_wrapper.rb', line 56

def self.supports_normalization?
  TingYun::Support::LanguageSupport.supports_string_encodings?
end

.usable_for_collector_serialization?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ting_yun/support/serialize/json_wrapper.rb', line 48

def self.usable_for_collector_serialization?
  @backend_name == :json
end