This project is archived and is in readonly mode.
#as_json returning self?
Reported by Brian Lopez | April 12th, 2010 @ 10:52 PM
I may have completely misunderstood what the #as_json method is for, but it was my assumption that it's supposed to return a JSON-mappable version of the object.
Meaning, it should return a String, Fixnum, TrueClass, FalseClass, NilClass, Array or Hash. In literally every example of it's use those are the types that are returned - which makes perfect sense.
But classes like Regexp and BigDecimal return self? If a JSON library checks to see if the object responds to #as_json, and calls it if so, it'll infinitely loop.
Should BigDecimal return to_f? And Regexp to_s?
Comments and changes to this ticket
-
Jeremy Kemper April 13th, 2010 @ 12:53 AM
- Assigned user set to Jeremy Kemper
That's right, though
as_json
maps to any objects which are JSON-encodable (respond toencode_json
), not just objects that directly correspond to JSON primitives.BigDecimal is Numeric which encodes itself using
to_s
.Regexp encodes itself using
inspect
, which is arguably wrong since it may not be valid JSON in some cases. -
Jeremy Kemper June 17th, 2010 @ 09:52 PM
- State changed from new to resolved
Was resolved by c1d73270717f30498f8f4d55d6695509107c2834
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
Referenced by
- 4376 #as_json returning self? Duplicates #4375
- 4378 #as_json returning self? Duplicates #4375
- 4379 #as_json returning self? Duplicates #4375
- 4380 #as_json returning self? Duplicates #4375
- 4377 #as_json returning self? Duplicates #4375
- 4377 #as_json returning self? Duplicates #4375