OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to make a class JSON serializable

  • Thread starter Thread starter Sergey
  • Start date Start date
S

Sergey

Guest
How to make a Python class serializable?

Code:
class FileItem:
    def __init__(self, fname):
        self.fname = fname

Attempt to serialize to JSON:

Code:
>>> import json
>>> x = FileItem('/foo/bar')
>>> json.dumps(x)
TypeError: Object of type 'FileItem' is not JSON serializable
<p>How to make a Python class serializable?</p>
<pre><code>class FileItem:
def __init__(self, fname):
self.fname = fname
</code></pre>
<p>Attempt to serialize to JSON:</p>
<pre><code>>>> import json
>>> x = FileItem('/foo/bar')
>>> json.dumps(x)
TypeError: Object of type 'FileItem' is not JSON serializable
</code></pre>
 

Latest posts

А
Replies
0
Views
1
Али-Мухаммад Закарьяев
А
M
Replies
0
Views
1
Marcos R. Guevara
M
M
Replies
0
Views
1
Marcos R. Guevara
M
Top