Hello Eric,

I think it's should be ok to subclass PyGFPEncoder, although I'm at best
dubious about the name, i'm not even sure what GFP stands for!

I think that decimal.Decimal usage is likely so common that perhaps
PyGFPEncoder could handle decimals by default.

Since Decimal(float_obj) is not allowed:
>>> Decimal(1.1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/decimal.py",
line 648, in __new__
    "First convert the float to a string")
TypeError: Cannot convert float to Decimal.  First convert the float to a
string
>>>

perhaps "Encoder.default" should convert to str/unicode instead of float.
(Is conversion of decimal > float lossless?)

There's no need to do the "import as _dumps" and wrap dumps, since
geojson.dump(s) pass through their arguemnts to simplejson:

>>> geojson.dumps(obj, cls=MapFishJSONEncoder)

Assuming it can be arranged such that decimals in input GeoJOSN get
encoded correctly with geojson.dump(s), and are retuned as decimals in
the result of geojson.load(s), I think it'd be a nice-to-have in geojson.

Regadless, provdiing a custom encoder should always be ok,
with the proviso that the super class contract is obeyed. (this should be
documented in geojson).
i.e ommiting the "super" call to PyGFPEncoder.default would obviously
break geojson encoding.

Sorry to be verbose, in summary I'm +1 for PyGFPEncoder being part of
the public API (but under a more sensible name),
and +0 on adding decimal support to geojson

Sean, what do you reckon?

Matt



2008/7/12 Eric Lemoine <[EMAIL PROTECTED]>:

> 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
>



-- 
Cheers,
Matt
_______________________________________________
Community mailing list
[email protected]
http://lists.gispython.org/mailman/listinfo/community

Reply via email to