mirror of
https://github.com/meineerde/rackstash.git
synced 2025-10-17 14:01:01 +00:00
Serialize Rational numbers to a String
With this, we are more compatible with the common JSON encoders, including the one on RUby core.
This commit is contained in:
parent
ab595d031b
commit
10993d2b1a
@ -5,6 +5,8 @@
|
||||
# of the MIT license. See the LICENSE.txt file for details.
|
||||
|
||||
require 'bigdecimal'
|
||||
require 'complex'
|
||||
require 'rational'
|
||||
require 'pathname'
|
||||
require 'uri'
|
||||
|
||||
@ -153,7 +155,7 @@ module Rackstash
|
||||
# number and no way to recover other than manually inspecting the
|
||||
# string with the JSON code itself.
|
||||
return value.to_s('F').encode!(Encoding::UTF_8).freeze
|
||||
when ::Complex
|
||||
when ::Complex, ::Rational
|
||||
# A complex number can not reliably converted to a float or rational,
|
||||
# thus we always transform it to a String
|
||||
return utf8_encode(value)
|
||||
|
||||
@ -491,10 +491,11 @@ describe Rackstash::Fields::AbstractCollection do
|
||||
expect(normalize(complex)).to be_frozen
|
||||
end
|
||||
|
||||
it 'transforms Rational to Float' do
|
||||
it 'transforms Rational to String' do
|
||||
rational = Rational(-8, 6)
|
||||
|
||||
expect(normalize(rational)).to be_a Float
|
||||
expect(normalize(rational)).to eql '-4/3'
|
||||
expect(normalize(rational).encoding).to eql Encoding::UTF_8
|
||||
expect(normalize(rational)).to be_frozen
|
||||
end
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user