question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SerializationError for datetime.time object

See original GitHub issue

JSONSerializer 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:closed
  • Created 9 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
honzakralcommented, Jan 6, 2018

@zeinababbasi there is no need to serialize a datetime object, elasticsearch-py will do it for you automatically (by using isoformat()) - see https://github.com/elastic/elasticsearch-py/blob/master/elasticsearch/serializer.py#L29 for details

0reactions
zeinababbasicommented, Jan 7, 2018

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

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found