Ruby Time#to_json作为浮点数

时间:2018-11-13 20:15:53

标签: json ruby

Ruby的json库默认将Time对象转换为Strings

require 'json'
Time.at(1000).utc.to_json # => "\"1970-01-01 00:16:40 UTC\"" 

问题是我们失去了精度。我想用to_json来生成浮点数。

我还知道有些使用oj或要求使用json/add/time的解决方法,但是这两种方法都会在输出中添加多余的数据,并且不是最可移植的。

一种简单的方法是修补时间,尽管我不喜欢这样做,尤其是对于核心类

class Time
  def to_json(*a)
    self.to_f.to_json(*a)
  end
end

有没有更好的方法?

2 个答案:

答案 0 :(得分:3)

  

一种直接的方法是修补时间,尽管我不喜欢这样做,尤其是对于核心类

日期没有JSON格式,就JSON而言,它们只是字符串。大多数语言都能理解ISO 8601,这就是Time#to_json产生的。只要Time#to_json继续产生ISO 8601日期时间,您就将保持向后兼容。

require 'json'
require 'time'  # for Time#iso8601 and Time.parse

class Time
  def to_json
    return self.iso8601(6).to_json
  end
end

time = Time.at(1000.123456)
puts "Before: #{time.iso8601(6)}"

json_time = Time.at(1000.123456).to_json
puts "As JSON: #{json_time}"

# Demonstrate round tripping.
puts "Round trip: #{Time.parse(JSON.parse(json_time)).iso8601(6)}"
Before: 1969-12-31T16:16:40.123456-08:00
As JSON: "1969-12-31T16:16:40.123456-08:00"
Round trip: 1969-12-31T16:16:40.123456-08:00

如果您对全局猴子修补不满意,可以通过实现around来单独进行猴子修补。

class Time
  require 'time'
  require 'json'

  def precise_to_json(*args)
    return iso8601(6).to_json(*args)
  end

  alias_method :original_to_json, :to_json
end

module PreciseJson
  def self.around
    # Swap in our precise_to_json as Time#to_json
    Time.class_eval {
      alias_method :to_json, :precise_to_json
    }

    # This block will use Time#precise_to_json as Time#to_json
    yield

  # Always put the original Time#to_json back.
  ensure
    Time.class_eval {
      alias_method :to_json, :original_to_json
    }
  end
end

obj = { 
  time: Time.at(1000.123456),
  string: "Basset Hounds Got Long Ears"
}

puts "Before: #{obj.to_json}"

PreciseJson.around {
  puts "Around: #{obj.to_json}"
}

puts "After: #{obj.to_json}"

begin
  PreciseJson.around {
    raise Exception
  }
rescue Exception
end

puts "After exception: #{obj.to_json}"
Before: {"time":"1969-12-31 16:16:40 -0800","string":"Basset Hounds Got Long Ears"}
Around: {"time":"1969-12-31T16:16:40.123456-08:00","string":"Basset Hounds Got Long Ears"}
After: {"time":"1969-12-31 16:16:40 -0800","string":"Basset Hounds Got Long Ears"}
After exception: {"time":"1969-12-31 16:16:40 -0800","string":"Basset Hounds Got Long Ears"}

答案 1 :(得分:0)

您可以保存自大纪元以来的纳秒数。

require 'time'
require 'json'

t = Time.now
  #=> 2018-11-13 13:23:32 -0800
json = [t.to_i, t.nsec].to_json
  #=> "[1542144212,883611300]"

secs, nsecs = JSON.parse(json)
  #=> [1542144212, 883611300]
r = secs + 10**-9 * nsecs
  #=> (15421442128836113/10000000)
tt = Time.at r
  #=> 2018-11-13 13:23:32 -0800

t == tt
  #=> true

注意(10**-9).class #=> Rational