forked from camptocamp/papyrus
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a JSON encoder, to deal with decimal.Decimal, datetime.date and d…
…atetime.datetime
- Loading branch information
Showing
2 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
import decimal | ||
import datetime | ||
|
||
import geojson | ||
from geojson.codec import PyGFPEncoder as GeoJSONEncoder | ||
|
||
class Encoder(GeoJSONEncoder): | ||
# SQLAlchemy's Reflecting Tables mechanism uses decimal.Decimal | ||
# for numeric columns and datetime.date for dates. simplejson | ||
# does'nt deal with these types. This class provides a simple | ||
# encoder to deal with objects of these types. | ||
|
||
def default(self, obj): | ||
if isinstance(obj, (decimal.Decimal, datetime.date, datetime.datetime)): | ||
return str(obj) | ||
return GeoJSONEncoder.default(self, obj) | ||
|
||
def geojson_renderer_factory(info): | ||
def _render(value, system): | ||
request = system.get('request') | ||
if request is not None: | ||
if not hasattr(request, 'response_content_type'): | ||
request.response_content_type = 'application/json' | ||
return geojson.dumps(value) | ||
return geojson.dumps(value, cls=Encoder) | ||
return _render |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters