[issue21543] json library fails to serialize objects such as datetime

2014-05-20 Thread R. David Murray

Changes by R. David Murray :


--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21543] json library fails to serialize objects such as datetime

2014-05-20 Thread Dann Ke

Dann Ke added the comment:

I was reading the older 2.7 docs and it didn't click that default() was
what I wanted. Thank you for clarifying.

On Tue, May 20, 2014 at 10:09 AM, akira  wrote:

>
> akira added the comment:
>
> json module already allows you to customize the serialization: see
> *default* parameter for json.dumps [1]
>
> [1] https://docs.python.org/3/library/json.html#json.dump
>
> --
> nosy: +akira
>
> ___
> Python tracker 
> 
> ___
>

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21543] json library fails to serialize objects such as datetime

2014-05-20 Thread akira

akira added the comment:

json module already allows you to customize the serialization: see *default* 
parameter for json.dumps [1]

[1] https://docs.python.org/3/library/json.html#json.dump

--
nosy: +akira

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21543] json library fails to serialize objects such as datetime

2014-05-20 Thread Dann Ke

New submission from Dann Ke:

I'm using the json library to serialize objects out for web use.

Whenever I serialize a datetime or other included python library object (uuid 
as another example), it fails with 

TypeError: datetime.datetime(2014, 5, 20, 9, 37, 56, 133627) is not JSON 
serializable

While this behaviour is well and good, what I am suggesting is that I would 
like a way for this library to attempt to serialize an object when it 
encounters an exception. 

For instance, if it attempts to serialize a datetime and fails, I would like 
the option of submitting a method as a parameter to the json.dumps() method 
that the object is put into in an attempt to serialize it.

As a code example:

import datetime, json
json.dumps(dict(mydate=datetime.datetime.now()),
serialize=str)

That way, when it encounters a TypeError, it will set mydate to a string and 
will be serialized out.

Right now, I have to use str() on a few different fields because they are not 
standard types (str, int, bool, float, None, etc)

This isn't a priority and I'm open to criticisms. Would you accept a patch for 
this if you are too busy to work on this feature?

Thank you for your time.
Dann

--
components: Extension Modules, Library (Lib)
messages: 218849
nosy: Dann.Ke
priority: normal
severity: normal
status: open
title: json library fails to serialize objects such as datetime
type: behavior
versions: Python 3.4

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com