Skip to main content
Shorten.
Source Link
Mateen Ulhaq
  • 27.9k
  • 21
  • 121
  • 155

How to make a Python class serializable?

A simple class:

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

What should I doAttempt to be ableserialize to get output ofJSON:

>>> import json   >>> my_filex = FileItem('/foo/bar') >>> json.dumps(my_filex) TypeError: Object of type 'FileItem' is not JSON serializable 

Without the error

How to make a Python class serializable?

A simple class:

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

What should I do to be able to get output of:

>>> import json   >>> my_file = FileItem('/foo/bar') >>> json.dumps(my_file) TypeError: Object of type 'FileItem' is not JSON serializable 

Without the error

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 
changed example code to be repeatable
Source Link
FraggaMuffin
  • 4.2k
  • 3
  • 27
  • 29

How to make a Python class serializable?

A simple class:

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

What should I do to be able to get output of:

>>> import json >>> my_file = FileItem('/foo/bar') >>> json.dumps(my_file) TypeError: Object of type 'FileItem' is not JSON serializable 

Without anthe error (FileItem instance at ... is not JSON serializable)

How to make a Python class serializable?

A simple class:

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

What should I do to be able to get output of:

json.dumps() 

Without an error (FileItem instance at ... is not JSON serializable)

How to make a Python class serializable?

A simple class:

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

What should I do to be able to get output of:

>>> import json >>> my_file = FileItem('/foo/bar') >>> json.dumps(my_file) TypeError: Object of type 'FileItem' is not JSON serializable 

Without the error

Question Protected by Sheldore
fixed phrasing
Source Link
Jeff Wilbert
  • 4.5k
  • 1
  • 24
  • 36

How to make a Python class serializable?

aA simple class:

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

What should I do to be able to get output of:

json.dumps() 

withoutWithout an error (FileItem instance at ... is not JSON serializable)

How to make a Python class serializable?

a simple class:

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

What should I do to be able to get output of:

json.dumps() 

without an error (FileItem instance at ... is not JSON serializable)

How to make a Python class serializable?

A simple class:

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

What should I do to be able to get output of:

json.dumps() 

Without an error (FileItem instance at ... is not JSON serializable)

Notice removed Reward existing answer by Duncan Jones
Bounty Ended with Onur Yıldırım's answer chosen by Duncan Jones
Notice added Reward existing answer by Duncan Jones
Bounty Started worth 50 reputation by Duncan Jones
Added "JSON" keyword to title: Users searching for a JSON solution will more likely come across your issue (I thought I already proposed that change but it seems that I didn't)
Link
Loading
Fix indentation
Source Link
Ben James
  • 126k
  • 27
  • 196
  • 155
Loading
Source Link
Sergey
  • 21.5k
  • 14
  • 48
  • 70
Loading