times equal to or earlier than 1970-01-01 00:00:00 UTC encode incorrectly #58

Closed
jmchambers opened this Issue Jan 9, 2013 · 7 comments

Projects

None yet

2 participants

@jmchambers

Times are encoded incorrectly (invalid JSON) when their integer representation is zero or negative:

require 'oj'
require 'yajl'
require 'json'
require 'bigdecimal'

stuff = [
  BigDecimal.new('10'),
  Date.new(1970,1,1),
  Time.utc(1970,1,1),
  DateTime.new(1970,1,1)
]

puts Yajl.dump stuff
puts JSON.dump stuff
puts Oj.dump stuff, :mode => :compat

#=> ["0.1E2","1970-01-01","1970-01-01 00:00:00 UTC","1970-01-01T00:00:00+00:00"]
#=> ["0.1E2","1970-01-01","1970-01-01 00:00:00 UTC","1970-01-01T00:00:00+00:00"]
#=> [0.1E2,"1970-01-01",.000000000,"1970-01-01T00:00:00+00:00"]
@ohler55
Owner
ohler55 commented Jan 9, 2013

Good catch. I'll fix it for the next release.

@jmchambers

cool, thx.

@ohler55
Owner
ohler55 commented Jan 18, 2013

Fixed in 2.0.1.

@ohler55
Owner
ohler55 commented Jan 20, 2013

I'll assume there are no problems with the fix.

@ohler55 ohler55 closed this Jan 20, 2013
@jmchambers

Hi @ohler55, sorry for not replying sooner. I'm afraid I still get the same erroneous output with version 2.0.1. Is it possibly an OS thing? My system details are:

$ uname -r

#=> 3.2.0-34-generic

$ lsb_release -a

#=> LSB Version:    core-2.0-amd64:core-2.0-noarch:core-3.0-amd64:core-3.0-noarch:core-3.1-amd64:core-3.1-//=> #=> noarch:core-3.2-amd64:core-3.2-noarch:core-4.0-amd64:core-4.0-noarch
#=> Distributor ID: Ubuntu
#=> Description:    Ubuntu 12.04.1 LTS
#=> Release:    12.04
#=> Codename:   precise
@ohler55 ohler55 reopened this Jan 20, 2013
@ohler55
Owner
ohler55 commented Jan 20, 2013

Now I get

["0.1E2","1970-01-01",0.000000000,"1970-01-01T00:00:00+00:00"]

Instead of .000000000 the result is now 0.000000000.

What are you seeing?

If you want to see some other time format then the :time_format must be specified to something like :xml_schema. If you want to change the precision of the :unix time format change the :second_precision to a value between 0 and 9 inclusive.

@jmchambers

Hi @ohler55. Appologies! I didn't notice the extra "0". It is indeed working fine now - I get valid JSON. And thanks for the tip regarding time format, the xml schema is what I was after. Thanks again.

@jmchambers jmchambers closed this Jan 21, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment