Welcome

Serializing a Python Object Into a Plain Text String

Serialization in Python is very easy. Here is how to do it

What is serialization?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/serialization/

Using Pickle

Pickle is a standard library of Python allowing serializing any Python object into a binary string or into a binary file. It also allows recreate a new object from the a serialized data.

The following code snippet demonstrates how to serialize some object into a binary string and de-serializing it into another object:

import pickle

# Some class for demonstration
class Demo:
    def __init__(self):
        # It has some data which can be serialized
        self.some_data = "hello"

    def say_hello(self):
        print(self.some_data)

# Creating an instance of Demo class
obj = Demo()

# Serializing the object into a binary string
obj_string = pickle.dumps(obj)

# De-serializing into another object
obj2 = pickle.loads(obj_string)

# Checking the the new object is working
# It should print: Hello
obj2.say_hello()

The following code snippet demonstrates how to serialize some object into a binary file and de-serializing it into another object:

import pickle

# Some class for demonstration
class Demo:
    def __init__(self):
        # It has some data which can be serialized
        self.some_data = "hello"

    def say_hello(self):
        print(self.some_data)

# Creating an instance of Demo class
obj = Demo()

# Serializing the object into a binary file 'obj_file.bin'
pickle.dump(obj, open('obj_file.bin', 'wb'))

# Reading the binary file
# and de-serializing into another object
obj2 = pickle.load(open('obj_file.bin', 'rb'))

# Checking the the new object is working
# It should print: Hello
obj2.say_hello()

Converting Into Plain Text

Pickle serializes objects into binary strings or files, but sometimes we need to use plain text format, for example when we want to transfer the state of some object over a communication channel that allows only using plain text.

It is possible to convert a binary string into a base64 string which is plain text string.

The following code snippet demonstrates how to serialize some object into a plain text string and de-serializing it into another object:

import pickle
import base64

# Serialize an object into a plain text
def obj_to_txt(obj):
    message_bytes = pickle.dumps(obj)
    base64_bytes = base64.b64encode(message_bytes)
    txt = base64_bytes.decode('ascii')
    return txt

# De-serialize an object from a plain text
def txt_to_obj(txt):
    base64_bytes = txt.encode('ascii')
    message_bytes = base64.b64decode(base64_bytes)
    obj = pickle.loads(message_bytes)
    return obj

# Some class for demonstration
class Demo:
    def __init__(self):
        # It has some data which can be serialized
        self.some_data = "hello"

    def say_hello(self):
        print(self.some_data)

# Creating an instance of Demo class
obj = Demo()

# Serializing the object into a plain text
obj_string = obj_to_txt(obj)

# De-serializing the plain text into another object
obj2 = txt_to_obj(obj_string)

# Checking the the new object is working
# It should print: Hello
obj2.say_hello()

Leave a Reply

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami