Class: BigDecimal

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/outputs/config/bigdecimal_patch.rb

Instance Method Summary collapse

Instance Method Details

#to_json(options = nil) ⇒ Object

Floating-point numbers that go through the ‘json’ Logstash filter get automatically converted into BigDecimals. Example of such a filter:

filter {

json {
  source => "message"
}

}

The problem is that { “value” => BigDecimal(‘0.12345’) } gets serialized into { “value”: “0.12345e0”}. We do want to keep floating point numbers serialized as floating point numbers, even at the expense of loosing a little bit of precision during the conversion. So, in the above example, the correct serialization would be: { “value”: 0.12345}



17
18
19
20
21
22
23
# File 'lib/logstash/outputs/config/bigdecimal_patch.rb', line 17

def to_json(options = nil) #:nodoc:
  if finite?
    self.to_f.to_s
  else
    'null'
  end
end