SerializationError for datetime.time object
See original GitHub issueJSONSerializer does not support time type.
elasticsearch/serializer.py
class JSONSerializer(object):
mimetype = 'application/json'
def default(self, data):
if isinstance(data, (date, datetime)):
return data.isoformat()
elif isinstance(data, Decimal):
return float(data)
raise TypeError("Unable to serialize %r (type: %s)" % (data, type(data)))
so I get a following error:
TypeError("Unable to serialize datetime.time
(10, 34) (type: <type 'datetime.time'>)"
A fix is quick and easy. Return time in isoformat like for date and datetime types:
class JSONSerializer(object):
mimetype = 'application/json'
def default(self, data):
if isinstance(data, (date, time, datetime)):
return data.isoformat()
elif isinstance(data, Decimal):
return float(data)
raise TypeError("Unable to serialize %r (type: %s)" % (data, type(data)))
Issue Analytics
- State:
- Created 9 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
python - Logging dictionaries with datetime objects without ...
Logging dictionaries with datetime objects without Serialization error ... I'm trying to log python dictionary as JSON like so logging.info(dict( ...
Read more >msrest.serialization — Azure SDK for Python 2.0.0 ... - NET
:rtype: str, dict :raises: SerializationError if serialization fails. ... :param datetime.time attr: Object to be serialized.
Read more >ActiveJob::SerializationError - Rails API
Raised when an unsupported argument type is set as a job argument. We currently support String , Integer , Float , NilClass ,...
Read more >Anvil Serialization Error
In form code, I have the lines: response = anvil.http.request(self.cp_QIS.tag['url']) csv_string_QIS = response.get_bytes() self.
Read more >dynamic_preferences.serializers - Django-dynamic-preferences
... decimal import os from datetime import date, timedelta, datetime, time, ... django.db.models.fields.files import FieldFile class UnsetValue(object): ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@zeinababbasi there is no need to serialize a
datetime
object,elasticsearch-py
will do it for you automatically (by usingisoformat()
) - see https://github.com/elastic/elasticsearch-py/blob/master/elasticsearch/serializer.py#L29 for details@HonzaKral, thank you. I’ve tried it yesterday with
isoformat()
. BTW, I appreciate your response in a thread which is closed for 2 and a half years.