Serialization in python

Serialization is a process by which data structure or an object state can be translated into a format which can be stored and restored as well in the same or another computer environment. The process of getting data from series of bytes is called deserialization. The JSON module helps in serializing to standard data format. Python also has ‘pickle’ standard library that provides a core serialization mechanism, but the serialization format provided by it may not be safe as it stores even complex Python data structures.

‘Pickling’ (Serializing) is the technique where a Python object hierarchy is converted into character (byte) stream. Conversely ‘unpickling’ is the mechanism where the byte stream gets converted back to a Python object hierarchy. ‘Pickling’ is decisively needed for distributed and parallel computing.

The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure. Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” or “flattening”, however, to avoid confusion, the terms used here are “pickling” and “unpickling”.

Pickle module can store all the native data types (Booleans, integers, complex numbers, bytes) and its combinations, which are supported by Python.

In most of the cases, JSON is preferred over pickle because pickle is not easy to understand and impossible for humans to write. Pickle is also very python-specific.

Therefore, while working with Python, you can use built-in JSON libraries to encode and decode. Jonpickle is a Python library for serialization and deserialization of complex Python objects to and from JSON. The standard Python libraries for encoding Python into JSON, such as the stdlib’s json, simplejson, and demjson, can only handle Python primitives that have a direct JSON equivalent (e.g. dicts, lists, strings, ints, etc.). Jsonpickle builds on top of these libraries and allows more complex data structures to be serialized to JSON. Jsonpickle is highly configurable and extendable–allowing the user to choose the JSON backend and add additional backends.

  • In Python version 2.5 the module that made this possible was called simplejson, whereas,
  • In version 2.7 it has been named as JSON. In order to practice this module it must be imported as follows:

JSON data can be of two types:

  1. String: This format is used to pass data into another program.
  2. Object data structure: It comprises of lists and dictionaries nested into each other. With the help of Python, you can add, remove, list or search elements in the data structure.

What data type can pickle store?

Here are few things that the pickle module store:

  • All the native data types that Python supports: Booleans, integers, floating point numbers, complex numbers, strings, bytes objects, byte arrays.
  • Lists, tuples, dictionaries, and sets containing any combination of native data types.
  • Functions, classes, and instances of classes (with caveats).

The ‘dump’ method is used to encode data structure to JSON. It will take an object as an argument and returns as a string. The load method is used to load JSON back to the data structure. This method takes a string as an input and returns an object data structure.




            Enroll for Instructor Led Live Python Training

0 Responses on Serialization in python"

Leave a Message


Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.