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 1080659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:03:56+00:00 2026-05-16T22:03:56+00:00

How to make a Python class serializable? class FileItem: def __init__(self, fname): self.fname =

  • 0

How to make a Python class serializable?

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

Attempt to serialize to JSON:

>>> import json
>>> x = FileItem('/foo/bar')
>>> json.dumps(x)
TypeError: Object of type 'FileItem' is not JSON serializable
  • 1 1 Answer
  • 1 View
  • 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-05-16T22:03:57+00:00Added an answer on May 16, 2026 at 10:03 pm

    Do you have an idea about the expected output? For example, will this do?

    >>> f  = FileItem("/foo/bar")
    >>> magic(f)
    '{"fname": "/foo/bar"}'
    

    In that case you can merely call json.dumps(f.__dict__).

    If you want more customized output then you will have to subclass JSONEncoder and implement your own custom serialization.

    For a trivial example, see below.

    >>> from json import JSONEncoder
    >>> class MyEncoder(JSONEncoder):
            def default(self, o):
                return o.__dict__    
    
    >>> MyEncoder().encode(f)
    '{"fname": "/foo/bar"}'
    

    Then you pass this class into the json.dumps() method as cls kwarg:

    json.dumps(cls=MyEncoder)
    

    If you also want to decode then you’ll have to supply a custom object_hook to the JSONDecoder class. For example:

    >>> def from_json(json_object):
            if 'fname' in json_object:
                return FileItem(json_object['fname'])
    >>> f = JSONDecoder(object_hook = from_json).decode('{"fname": "/foo/bar"}')
    >>> f
    <__main__.FileItem object at 0x9337fac>
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python is there any way to make a class, then make a second
In Python, I want to make selected instance attributes of a class be readonly
I'm trying to serialize a list of python objects with JSON (using simplejson) and
I want to make my Python Class behave in such a way that when
I have a class MyClass , which contains two member variables foo and bar
I want to make a class in python, which would store a tree.Keeps taking
I am trying to make an enum-type class in Python but it gets so
In python, why is recommended to inherit any class we make from the class
I'm fairly new to Python. And this is my first class: import config #
In python, I want to make a class variable static so I can to

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.