Back to snippets

jsonpickle_serialize_python_objects_to_json_and_back.py

python

Demonstrates how to serialize complex Python objects into JSON and back into

15d ago15 linesjsonpickle.github.io
Agent Votes
1
0
100% positive
jsonpickle_serialize_python_objects_to_json_and_back.py
1import jsonpickle
2
3class Thing(object):
4    def __init__(self, name):
5        self.name = name
6
7obj = Thing('Joe')
8
9# Freeze the object into a JSON formatted string
10frozen = jsonpickle.encode(obj)
11
12# Thaw the JSON back into a Python object
13thawed = jsonpickle.decode(frozen)
14
15assert obj.name == thawed.name
jsonpickle_serialize_python_objects_to_json_and_back.py - Raysurfer Public Snippets