Skip to main content

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 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_"

Or

import ComplexJSON

ComplexJSON.vardef.classword = "_c_"
ComplexJSON.vardef.moduleword = "_m_"

And now A class will be serialized as:

{"a": 1, "b": 2, "_m_": "__main__", "_c_": "A"}

Remove codewords from json (v1.1+)

from ComplexJSON import vardef

vardef.classword = None  # remove classword from json
vardef.moduleword = None  # remove moduleword from json

If you remove both, object will be decoded as python dict. If you remove moduleword, you need to provide class in classStorage argument in load, loads or ComplexJSONDecoder

import ComplexJSON as json
json.vardef.moduleword = None
class A:
    b = 1
...
classStorage = [A]
# or
classStorage = {"A": A}
...
json.loads(obj, classStorage=classStorage)
# or
decoder = json.ComplexJSONDecoder(classStorage=classStorage)
...
decoder.decode(obj)
# or 
json.loads(obj, object_hook=decoder.object_hook)

Local codewords (v1.1+)

import ComplexJSON as json
...
_json = json.dumps(obj, localClassWord="Type", localModuleWord="Module")
new_obj = json.loads(_json, localClassWord="Type", localModuleWord="Module")

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

ComplexJSON-1.1.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ComplexJSON-1.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file ComplexJSON-1.1.0.tar.gz.

File metadata

  • Download URL: ComplexJSON-1.1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for ComplexJSON-1.1.0.tar.gz
Algorithm Hash digest
SHA256 36cd3f5cb927650bc3c442cd5c19878f56e4b1b4f939ee975688dfe5d8c738f7
MD5 cf744721fe4513f48bb4866c1e83ad4d
BLAKE2b-256 8e5a74d059bcbb9c5019082dc7b3fc69ef316dcd2ae2b6aec8471b101c5c67c4

See more details on using hashes here.

File details

Details for the file ComplexJSON-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: ComplexJSON-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for ComplexJSON-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3084f34e8370f76746c9b52fea0e2a1372f46354ae114996909091a106d041ba
MD5 ab0a1aac6dddc509478e30e76e8862a6
BLAKE2b-256 b8d65f172eabba24b6abdb1f129333da57d8c9612607771e5d1cf29bbc126ee8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page