Hello
In MapFish, we use geojson and sqlalchemy. By default sqlalchemy uses
Python's decimal.Decimal for numeric columns. So we end up building
geojson Features with properties whose values are of the
decimal.Decimal type (e.g. {'pi': decimal.Decimal("3.14")}). This is
ok until we use geojson.dumps(), because simplejson doesn't know how
to deal with decimal.Decimal objects. To solve the problem, I thought
about having something along these lines in MapFish:
from geojson.codec import PyGFPEncoder
from geojson import dumps as _dumps
class MapFishJSONEncoder(PyGFPEncoder):
def default(self, obj):
if isinstance(obj, decimal.Decimal):
return float(obj)
return PyGFPEncoder.default(self, obj)
def dumps(obj, **kwargs):
return _dumps(obj, cls=MapFishJSONEncoder, **kwargs)
That seems to work correctly. What bothers me a bit is inheriting from
PyGFPEncoder because PyGFPEncoder isn't part of geojson's public
interface, which can potentially cause us trouble.
What do you think?
Thanks,
--
Eric
_______________________________________________
Community mailing list
[email protected]
http://lists.gispython.org/mailman/listinfo/community