This project is archived and is in readonly mode.
ActiveSupport::JSON.decode breaks for strings that look like dates ruby1.9.2
Reported by pederbl (at jobstar) | January 13th, 2011 @ 11:39 AM
Repro:
ActiveSupport::JSON.decode("{
Stack (after disabling the invalid json exception):
/usr/local/ruby/lib/ruby/1.9.1/date.rb:810:in civil': invalid
date (ArgumentError)
from /usr/local/ruby/lib/ruby/1.9.1/syck.rb:136:in `node_import'
from /usr/local/ruby/lib/ruby/1.9.1/syck.rb:136:in `load'
from /usr/local/ruby/lib/ruby/1.9.1/syck.rb:136:in `load'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0/lib/active_support/json/backends/yaml.rb:18:in `decode'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0/lib/active_support/json/decoding.rb:14:in `decode'
The error seems to be in the Parser which I haven't found yet.
Ruby Version: ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
Comments and changes to this ticket
-
pederbl (at jobstar) January 13th, 2011 @ 11:44 AM
"{str
seems like lighthouse formatting has a problem with displaying json strings
-
Repository February 11th, 2011 @ 03:56 PM
- State changed from new to resolved
(from [b17d8d727fb510ad8b6eb4302984d290dc2e53b0]) Fixes an issue when decoding a json string which looks like a date but is invalid. This DateTime parse error is now caught and the original string is instead passed back [#6286 state:resolved]
Signed-off-by: Santiago Pastorino santiago@wyeworks.com
https://github.com/rails/rails/commit/b17d8d727fb510ad8b6eb4302984d... -
pederbl (at jobstar) February 12th, 2011 @ 05:22 AM
Thanks for fixing this problem. However, I suggest that you should never convert string values to DateTime in the JSON parser. Generally, the current logic violates the Keep it Simple Smartypants (KISS) principle. Specifically, I will have to do the following unintuitive and cumbersome programming pattern because of the current logic:
hash = ActiveSupport::JSON.decode(json_string)
hash['reference'] = hash['reference'].to_s # make sure to convert DateTime to string if the string matched the datetime regexp in the parser.I will have to convert all string values to strings in the hash because otherwise something like the following might happen:
subject = "Product reference: #{hash['reference']}" # => "Product reference: 2011-02-12T00:00:00+00:00"
which would be incorrect. The expected value of subject should be: "Product reference: 2011-02-12" because the reference string just happens to look like a date but really isn't.
So by converting string values to DateTime in the parser you:
1) require a unintuitive and cumbersome programming pattern
2) make it hard to avoid bugs that are hard to find and debug and appear seldom.Also, by converting strings to DateTime the parse code become unnecessarily complicated which makes it harder to maintain increases the risk of bugs and makes it slower.
-
Repository February 12th, 2011 @ 03:35 PM
(from [e605165d16bc0eff8e4bea9865d9963b2d24dcf1]) Fixes an issue when decoding a json string which looks like a date but is invalid. This DateTime parse error is now caught and the original string is instead passed back [#6286 state:resolved]
Signed-off-by: Santiago Pastorino santiago@wyeworks.com
https://github.com/rails/rails/commit/e605165d16bc0eff8e4bea9865d99...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Tags
Referenced by
- 6286 ActiveSupport::JSON.decode breaks for strings that look like dates ruby1.9.2 (from [b17d8d727fb510ad8b6eb4302984d290dc2e53b0]) Fixes a...
- 6286 ActiveSupport::JSON.decode breaks for strings that look like dates ruby1.9.2 (from [e605165d16bc0eff8e4bea9865d9963b2d24dcf1]) Fixes a...