[Python] JSON Serialize a Python Class

Francesco Stablum stablum a gmail.com
Sab 11 Gen 2014 19:18:18 CET


Hi,

indeed these objects are not serializable with the well known Python's JSON
serializers.
I would suggest you jsonpickle that allows the serialization that you need:
http://jsonpickle.github.io/

good luck!
-Francesco


On 11 January 2014 18:46, Marco Olimpi <badstorm a gmail.com> wrote:

> Hi,
> i'm traing to serialize my called in python. This is my example:
> I have this two classes:
>
> *class User():*
> *  def __init__(self, _name, _lastname):*
> *       self.Name = _name*
> *       self.LastName = _lastname*
>
> *class News():*
> *  def __init__(self, _news,_user):*
> *       self.News = _news*
> *       self.User = _user*
>
>
> than i create my class in my code:
>
> ...
> *myUser = User("Mario","Rossi")*
> *myNews = News("My last news", myUser)*
>
> *JSONstring = dumps(myNews)*
> ...
>
> but in the last line of code i get the error that the News class is not
> JSON serializable.
>
> How I can make this work?
>
> Thanks.
>
> Regards
>
> Marco
>
> _______________________________________________
> Python mailing list
> Python a lists.python.it
> http://lists.python.it/mailman/listinfo/python
>
>
-------------- parte successiva --------------
Un allegato HTML è stato rimosso...
URL: <http://lists.python.it/pipermail/python/attachments/20140111/6d6f5107/attachment.html>


Maggiori informazioni sulla lista Python