GeospatialPython / pyshp

This library reads and writes ESRI Shapefiles in pure Python.
MIT License
1.1k stars 259 forks source link

__geo_interface__ not serializable to json when shapefile contains dates #209

Closed tjwebb closed 2 years ago

tjwebb commented 3 years ago

This simple example fails:

record = sf.shapeRecord(0)
json.dumps(record.__geo_interface__)

result:

TypeError: Object of type date is not JSON serializable

If I want the bona fide date object, that is already available in the record. But if I'm using __geo_interface__, it's because I want to serialize it into geojson. __geo_interface__ is trying to be too clever for its own good.

GDAL's .ExportToJson() simply writes the date as a string in the properties hash.

tjwebb commented 3 years ago

In case anyone else runs into this issue, this is a pretty clean workaround:

json.dumps(record.__geo_interface__, default=str)
karimbahgat commented 3 years ago

Nice workaround @tjwebb! This has now been fixed in https://github.com/GeospatialPython/pyshp/commit/7ef3456521be80c699d06d802adec176ca2b9169, and will be available in the next version.

karimbahgat commented 2 years ago

This is fixed in the latest v2.2.0.