0
require 'json'
# => true
x = { starting_time: Time.now }
# => {:starting_time=>2019-04-19 11:51:30 +0900}
x.to_json
# => "{\"starting_time\":\"2019-04-19 11:51:30 +0900\"}"

I want my date formats to automatically serialize in iso8601. How can I set the default for serialization?

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
Strawberry
  • 66,024
  • 56
  • 149
  • 197
  • 1
    `require 'time'; Time.now.utc.iso8601` no? – Sebastián Palma Apr 19 '19 at 03:12
  • ActiveSupport does this by default. If you don’t have other uses for it then you may not want to load in the gem. But the gem does do some nice stuff as well. – Nate Apr 19 '19 at 03:17
  • 1
    This post shows you how to make it use iso8601 when you call to_json, with a simple core ext. This method is used even when you call to_json on the Hash that you have there. https://stackoverflow.com/a/53289174/10068463 – Nate Apr 19 '19 at 03:39

0 Answers0