[Python] How to make a class JSON serializable

Discussão em 'Python' iniciado por Stack, Julho 3, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página