Encoding NaN in JSON

Roland Koebler r.koebler at yahoo.de
Wed Apr 17 18:40:30 EDT 2013


Hi,

> > Easiest way is probably to transform your object before you try to write
> Yeah, that's what I ended up doing. Wondered if there's a better way ...
yes, there is: subclass+extend the JSON-encoder, see pydoc json.

e.g.:
class JsonNanEncoder(json.JSONEncoder):
    def default(self, obj):
	if some-check-if-obj-is-NaN:
	    return 'NaN'
	return json.JSONEncoder.default(self, obj)

Roland



More information about the Python-list mailing list