In my Django application, I have a python dictionary that looks like this:
myDict = {"A": 1, "B": "Z", "C": {"D":"E"},}
When I want to serialize this and return this dict via a curl hittable endpoint, I do the following. It works:
return HttpResponse(
json.dumps(myDict),
content_type="application/json",
)
However, if I now add a value to myDict that is a PointField, like this:
myDict["myPoint"] = Point(70.0, 30.0,)
I get the following error:
TypeError: <Point object at 0x111bbed90> is not JSON serializable
So how do I serialize a dictionary that contains a PointField? I tried the following, but it definitely doesn't work:
return HttpResponse(
GeoJSONSerializer().serialize(myDict, use_natural_keys=True),
content_type="application/json",
)
Searching the internet doesn't show me any clear examples of how to serialize a dictionary that contains a PointField.