This is the simplest module for serialize and deserialize python complex objects
Project description
ComplexJSON Library
What is this?
The module allows you serialize your python complex objects to json and deserialize it.
How to install
To install, you can use the command:
pip install ComplexJSON
Or Download the repository from GitHub.
Quick Guide
Just use it like python builtin json module
import ComplexJSON
...
json_obj = ComplexJSON.dumps(obj)
...
new_obj = ComplexJSON.loads(json_obj)
Warning
If you use this code:
import ComplexJSON
...
a = [1, 2, 3]
b = [a, a]
c = ComplexJSON.dumps(b)
b = ComplexJSON.loads(c)
you will get multiple objects instead of 1 object with multiple links:
b[0].pop()
print(b)
>>> [[1, 2], [1, 2, 3]]
Although you might expect:
>>> [[1, 2], [1, 2]]
Using
There are variants of usage:
import ComplexJSON
...
json_obj = ComplexJSON.dumps(obj)
new_obj = ComplexJSON.loads(json_obj)
...
# or
with open("test.json", "wt", encoding="Utf-8") as f:
ComplexJSON.dump(obj, f)
...
with open("test.json", "rt", encoding="Utf-8") as f:
new_obj = ComplexJSON.load(json_obj, f)
# or
import json
json_obj = json.dumps(obj, cls=ComplexJSON.ComplexJSONEncoder)
new_obj = json.loads(json_obj, cls=ComplexJSON.ComplexJSONDecoder)
# or
import json
with open("test.json", "wt", encoding="Utf-8") as f:
json.dump(obj, f, cls=ComplexJSON.ComplexJSONEncoder)
...
with open("test.json", "rt", encoding="Utf-8") as f:
new_obj = json.load(json_obj, f, cls=ComplexJSON.ComplexJSONDecoder)
Change json module
This module by default uses builtin python json module in ComplexJSONEncoder and ComplexJSONDecoder. You can change it. Set environ "COMPLEX_JSON_MODULE_NAME" to name of your json library ("json" by default).
Change codewords for ComplexJSON
By default, the codewords for the module and class are "__module__" and "__class__", respectively
class A:
def __init__(self):
self.a = 1
self.b = 2
Object of this class will be serialized as:
{"a": 1, "b": 2, "__module__": "__main__", "__class__": "A"}
But you can change the codewords for module and class:
from ComplexJSON import vardef
vardef.classword = "_c_"
vardef.moduleword = "_m_"
And now A class will be serialized as:
{"a": 1, "b": 2, "_m_": "__main__", "_c_": "A"}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ComplexJSON-1.0.1.tar.gz.
File metadata
- Download URL: ComplexJSON-1.0.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
346201462406cb72b67405c9c8d3063c629177ec272233b191b33622a962e08f
|
|
| MD5 |
a4fe888ed7354c1532cc689e8be389be
|
|
| BLAKE2b-256 |
c6e6951dbdfba7edb662bc1aedfb6e8dc2e349fefbe577599ce3896cfbfc0433
|
File details
Details for the file ComplexJSON-1.0.1-py3-none-any.whl.
File metadata
- Download URL: ComplexJSON-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3535494b87254b3221c0ffe512fa0c2768d06d28305d97bfc3e0d451272254c
|
|
| MD5 |
f7b185501bcf5380d68cd7de41d97a83
|
|
| BLAKE2b-256 |
2a83cd8ca93345eb3e0585848319133af54c26dbe3a5d951b8f8f9299422581a
|