openfisca / openfisca-web-api

[DEPRECATED] Web API for OpenFisca
https://www.openfisca.fr/
GNU Affero General Public License v3.0
13 stars 11 forks source link

Dates are not JSON serializable #25

Closed MattiSG closed 9 years ago

MattiSG commented 9 years ago

If I send a date parameter and the API tries to send it back JSON-encoded, the server fails with the following TypeError: datetime.date(2014, 12, 9) is not JSON serializable.

Example: http://localhost:2000/api/2/formula/remuneration_apprenti?apprentissage_contrat_debut=2014-12-09&apprenti=true&age=19

eraviart commented 9 years ago

After conversion a date is no more a string. Use date.isoformat().

cbenz commented 9 years ago

respond_json is meant to receive serializable-ready data. The caller decides which serialization to apply (isoformat or other). For example, column.to_json(): https://github.com/openfisca/openfisca-web-api/blob/master/openfisca_web_api/controllers/fields.py#L162

The question is : where does your datetime.date type comes from? I can help to fix it directly where the error occurs :)

MattiSG commented 9 years ago

where does your datetime.date type comes from?

From echoing parsed parameters, as @eraviart recommended.

The caller decides which serialization to apply (isoformat or other).

That means iterating over all passed parameters just in case one is a date.

MattiSG commented 9 years ago

respond_json is meant to receive serializable-ready data.

I really don't get that. It feels like mixing responsibility: the caller is responsible for ensuring the data it passes to respond_json will pass through json.dumps, which is an implementation detail that should be encapsulated by respond_json(ctx, data). If I have to be concerned with knowing which encoding strategy is to be used, then better let me provide raw text!

cbenz commented 9 years ago

That means iterating over all passed parameters just in case one is a date.

Yes, and this is what we do with the specific to_json() or a more generic conv.jsonify_value() (which does not fit your need).

The general approach which is used through all the application code distinguishes Python data (objects, lists, dicts and data types), JSON-like data (lists, dicts and JSON compatible types, but still Python), and JSON-serialized string. respond_json is just the end of the chain which is not responsible for transforming Python data into JSON string, but just JSON-like data into JSON string.

This is just the way it has been design. I understand the need in the other side.

I could not reproduce the problem, could you give my any hint to? I'll try to see if I can parametrize the JSON encoder.

MattiSG commented 9 years ago

I could not reproduce the problem, could you give my any hint to?

http://openfisca.sgmap.fr/api/2/formula/remuneration_apprenti?apprentissage_contrat_debut=2014-12-09
http://localhost:2000/api/2/formula/remuneration_apprenti?apprentissage_contrat_debut=2014-12-09
cbenz commented 9 years ago

Fixed in #28