1

I want to serialize the class itself rather than a object instance.

For example if I do this

class Foo:
   pass
json.dumps(Foo)

It would throw an error saying Foo is not JSON serializable. Is it even possible to do this with python?

6
  • 1
    What do you expect to receive when you "serialize" a class? Commented Mar 25, 2017 at 7:22
  • I think you meant you want to use shelves or pickle Commented Mar 25, 2017 at 7:23
  • github.com/tschellenbach/Stream-Framework this framework does celery tasks with written classes. In the framework they use pickle, but i want to use json since celery supports json. In the framework they send serialized class references to celery. Hope this explains Commented Mar 25, 2017 at 7:23
  • @abccd no I don't think so Commented Mar 25, 2017 at 7:25
  • What would you expect the output for your example to be? Commented Mar 25, 2017 at 7:37

1 Answer 1

1

A bit late, but you can't actually directly serialize a class. Serializing is the act of turning an object into a string for storage and transmission. A class declaration, in your case Foo, is already a text file; Foo.py (or wherever you put it).

Serialization librairies usually keep the object's class in reference, so that the same program quand save and load an object and still know that it's a Foo instance. But, if I program my own little app that does not contain a Foo class, then unserializing your data will give me something that is pretty much useless.

Depending on what you want to do, there are alternatives.

  • If your goal is to save or transmit methods instead of data (as a class declaration is usually one of methods, unlike a class instance, which is composed of information, contained in properties and variables), then you could serialize functions or dynamic objects containing methods. Those would be understood by any program unserializing the data and those functions could be correctly run. It is actually a security risk to unserialize without fact-checking the data, as you could then transmit damaging methods to an unsuspecting program.
  • If your goal is to transmit data types, then you might have to consider transmitting your entire program instead. As stated, the goal of serialization is to save or transmit data. If you want to save or transmit functionality, then consider that your code, saved on your hard drive, already is a string representation of functionality; you could transmit your entire program. Also, there are librairies out there to convert your code into executables, in case the target computer does not support python.
  • A third alternative, and half the reason I ended up finding this question on Stack Overflow, is to structure your data as database entries. This only applies if your goal is to save or transmit an object as a "shape", or a structure, and not as a bunch of fuctions and actions. In other words, this assumes your goal is to transmit the fact that a Contact class is composed of a name, a phone number and an address.

For the last option, you, in a way, want to build a class of classes:

Class model:
    def __init__(self, class_name, fields):
        self.class_name = class_name
        self.fields = fields

Class model_instance:
    def __init__(self, model, values):
        self.fields = {}
        # add values to the dict with fields as keys, based on the model
        ...

When you instantiate a "model", you are actually creating the model of an object. There would then be multiple tricks to correctly implement an alternative instantiation system with this, at this point, some research will be required.

I understand this post is a bit old but I thought answering what I know while searching for what I don't is the entire point of Stack Overflow!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.