Class: OneApm::JSONWrapper

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

Defined Under Namespace

Classes: EncodingNormalizer, IconvNormalizer

Class Method Summary collapse

Class Method Details

.backend_nameObject



46
47
48
# File 'lib/one_apm/support/json_wrapper.rb', line 46

def self.backend_name
  @backend_name
end

.choose_normalizerObject



55
56
57
58
59
60
61
# File 'lib/one_apm/support/json_wrapper.rb', line 55

def self.choose_normalizer
  if OneApm::LanguageSupport.supports_string_encodings?
    @normalizer = EncodingNormalizer
  else
    @normalizer = IconvNormalizer
  end
end

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



121
122
123
124
# File 'lib/one_apm/support/json_wrapper.rb', line 121

def self.dump(object, options={})
  object = normalize(object) if options[:normalize]
  @dump_method.call(object)
end

.load(string) ⇒ Object



126
127
128
# File 'lib/one_apm/support/json_wrapper.rb', line 126

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

.load_native_jsonObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/one_apm/support/json_wrapper.rb', line 5

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 }
    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
    OneApm::Manager.logger.debug "%p while loading JSON library: %s" % [ err, err.message ] if
      defined?( OneApm::Agent ) && OneApm::Manager.respond_to?( :logger )
  end
end

.load_okjsonObject



33
34
35
36
37
38
# File 'lib/one_apm/support/json_wrapper.rb', line 33

def self.load_okjson
  require 'one_apm/support/okjson'
  @load_method = ::OneApm::Support::OkJson.method(:decode)
  @dump_method = ::OneApm::Support::OkJson.method(:encode)
  @backend_name = :okjson
end

.normalize(object) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/one_apm/support/json_wrapper.rb', line 98

def self.normalize(object)
  case object
  when String
    normalize_string(object)
  when Symbol
    normalize_string(object.to_s)
  when Array
    return object if object.empty?
    object.map { |x| normalize(x) }
  when Hash
    return object if object.empty?
    hash = {}
    object.each_pair do |k, v|
      k = normalize_string(k)      if k.is_a?(String)
      k = normalize_string(k.to_s) if k.is_a?(Symbol)
      hash[k] = normalize(v)
    end
    hash
  else
    object
  end
end

.normalize_string(s) ⇒ Object



50
51
52
53
# File 'lib/one_apm/support/json_wrapper.rb', line 50

def self.normalize_string(s)
  choose_normalizer unless @normalizer
  @normalizer.normalize(s)
end

.usable_for_collector_serialization?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/one_apm/support/json_wrapper.rb', line 42

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