This project is archived and is in readonly mode.
#as_json should only ever return a Hash, Array, Numeric, String, nil, true or false
-
Brian Lopez
- Tag set to activerecord, activesupport, json, serialization
-
Santiago Pastorino
- State changed from new to open
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino
- State changed from open to stale
-
John Firebaugh
- State changed from stale to open
I agree. It would be very useful to have the following invariant:
∀o, o.as_json == JSON.parse(o.to_json)
The principal violator is ActiveModel::Serializers::JSON#as_json, which does not call #as_json on the serializable_hash. This means that the hash values are whatever types came back or were typecasted from the database (the most common non-JSON-native types being Time, Date, and DateTime). This necessitates a further workaround in Hash#encode_json which should not be necessary.
Attaching a patch that fixes this.
[state:open]
-
John Firebaugh
Another issue: TrueClass, FalseClass, and NilClass return an ActiveSupport::JSON::Variable from #as_json. Instead, they should return self and override encode_json.
-
Santiago Pastorino
- Milestone set to 3.1
- Assigned user set to Santiago Pastorino
- Importance changed from to Low
-
rke
Instead of Numeric shouldn't this be Float? http://www.ietf.org/rfc/rfc4627.txt does not allow Rationals or Complex numbers and although it does not impose a limit on precision and exponent, http://www.json.org/ describes itself as a subset of ECMA-262 which just says:
8.5 The Number Type
The Number type has exactly 18437736874454810627 (that is, 264−253+3) values, representing the double- precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic, ...
