Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:21:35+00:00 2026-06-04T07:21:35+00:00

Why does the JSON serialization not work for datetime objects? As I understand JSON

  • 0

Why does the JSON serialization not work for datetime objects? As I understand JSON serialization, the basic idea for any object can be called the __str__ built-in function and then URL encode the object that you get as a response. But in case of datetime, I get the following error

TypeError: datetime.datetime(2012, 5, 23, 18, 38, 23, 37566) is not JSON serializable

while there is a __str__, i.e., a way of stringifying the object already available , But it seems like a conscious decision to not do it , why would that be the case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:21:36+00:00Added an answer on June 4, 2026 at 7:21 am

    No, it doesn’t work that way in the json module. The module provides you with a default encoder: json.JSONEncoder. You need to extend this to provide your implementation of default method to serialize objects. Something like this:

    import json
    import datetime
    from time import mktime
    
    class MyEncoder(json.JSONEncoder):
    
        def default(self, obj):
            if isinstance(obj, datetime.datetime):
                return int(mktime(obj.timetuple()))
    
            return json.JSONEncoder.default(self, obj)
    
    print json.dumps(obj, cls=MyEncoder)
    

    As others correctly pointed out, the reason is that the standard for JSON does not specify how date time can be represented.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does MVC asp.net serialization work for Json object on Controller actions ? For
jQuery's jqXHR object passed to the callback on failure does not parse json responses,
How does one turn any random object into a JSON string using Monodroid's System.Json
Is there any reasons why PHP's json_encode function does not escape all JSON control
Why does my cfc method (when returning JSON format or called via remoting) return
Does anyone know there have any other way that (by not using json_encode and
I downloaded the JSON2.js from https://github.com/douglascrockford/JSON-js/blob/master/json2.js and it does not have implementation for JSON2.stringify()
I need to persist a object. Unfortunately sqlite in android does not support ORM.
At the JSON site it says JSON does not support cyclic data structures, so
I am wondering if anyone knows if I can use System.Runtime.Serialization.Json (4.0) in monodroid.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.