ComplexJSON


NameComplexJSON JSON
Version 1.2.0 PyPI version JSON
download
home_pagehttps://github.com/dail45/ComplexJSON
SummaryThis is the simplest module for serialize and deserialize python complex objects
upload_time2024-05-06 12:52:30
maintainerNone
docs_urlNone
authordail45
requires_python>=3.6
licenseNone
keywords json complexobject serialize
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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](https://github.com/dail45/ComplexJSON).

## Quick Guide ##

Just use it like python builtin json module

```python
import ComplexJSON
...
json_obj = ComplexJSON.dumps(obj)
...
new_obj = ComplexJSON.loads(json_obj)
```

----------

## Warning ##

If you use this code:

```python
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:

```python
b[0].pop()
print(b)
>>> [[1, 2], [1, 2, 3]]
```

Although you might expect:
```python
>>> [[1, 2], [1, 2]]
```

----------

### Using ###

There are variants of usage:

```python
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

```python
class A:
    def __init__(self):
        self.a = 1
        self.b = 2
```

Object of this class will be serialized as:
```python
{"a": 1, "b": 2, "__module__": "__main__", "__class__": "A"}
```

But you can change the codewords for module and class:

```python
from ComplexJSON import vardef

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

Or

```python
import ComplexJSON

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

And now A class will be serialized as:
```python
{"a": 1, "b": 2, "_m_": "__main__", "_c_": "A"}
```

#### Remove codewords from json (v1.1+) ####

```python
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
```python
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+) ####

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

#### Type Association (v1.2+) ####

```python
import ComplexJSON


class A:
    def __init__(self):
        self.a = 1
        self.b = 2


rs = {1: A()}
json_obj = ComplexJSON.dumps(rs, useTypeAssociation=True)
print(json_obj)
#>>> {"types": {".": {"useTA": true}, "0": {"__module__": "__main__", "__class__": "A"}}, "obj": {"1": {"a": 1, "b": 2, "__cid__": "0"}}}
print(ComplexJSON.loads(json_obj))
#>>> {'1': <__main__.A object at 0x00000154D6013490>}
```

----------

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dail45/ComplexJSON",
    "name": "ComplexJSON",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "json complexobject serialize",
    "author": "dail45",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/c0/17/4e4f1761c233b6cf1f914fb2644fc5ef707a1f89500424b26789a2a390fd/ComplexJSON-1.2.0.tar.gz",
    "platform": null,
    "description": "# ComplexJSON Library #\r\n\r\n## What is this? ##\r\nThe module allows you serialize your python complex objects to json and deserialize it.\r\n\r\n## How to install ##\r\n\r\nTo install, you can use the command:\r\n```\r\npip install ComplexJSON\r\n```\r\n\r\nOr Download the repository from [GitHub](https://github.com/dail45/ComplexJSON).\r\n\r\n## Quick Guide ##\r\n\r\nJust use it like python builtin json module\r\n\r\n```python\r\nimport ComplexJSON\r\n...\r\njson_obj = ComplexJSON.dumps(obj)\r\n...\r\nnew_obj = ComplexJSON.loads(json_obj)\r\n```\r\n\r\n----------\r\n\r\n## Warning ##\r\n\r\nIf you use this code:\r\n\r\n```python\r\nimport ComplexJSON\r\n\r\n...\r\na = [1, 2, 3]\r\nb = [a, a]\r\nc = ComplexJSON.dumps(b)\r\nb = ComplexJSON.loads(c)\r\n```\r\n\r\nyou will get multiple objects instead of 1 object with multiple links:\r\n\r\n```python\r\nb[0].pop()\r\nprint(b)\r\n>>> [[1, 2], [1, 2, 3]]\r\n```\r\n\r\nAlthough you might expect:\r\n```python\r\n>>> [[1, 2], [1, 2]]\r\n```\r\n\r\n----------\r\n\r\n### Using ###\r\n\r\nThere are variants of usage:\r\n\r\n```python\r\nimport ComplexJSON\r\n...\r\njson_obj = ComplexJSON.dumps(obj)\r\nnew_obj = ComplexJSON.loads(json_obj)\r\n...\r\n# or\r\nwith open(\"test.json\", \"wt\", encoding=\"Utf-8\") as f:\r\n    ComplexJSON.dump(obj, f)\r\n...\r\nwith open(\"test.json\", \"rt\", encoding=\"Utf-8\") as f:\r\n    new_obj = ComplexJSON.load(json_obj, f)\r\n# or\r\nimport json\r\njson_obj = json.dumps(obj, cls=ComplexJSON.ComplexJSONEncoder)\r\nnew_obj = json.loads(json_obj, cls=ComplexJSON.ComplexJSONDecoder)\r\n# or\r\nimport json\r\nwith open(\"test.json\", \"wt\", encoding=\"Utf-8\") as f:\r\n    json.dump(obj, f, cls=ComplexJSON.ComplexJSONEncoder)\r\n...\r\nwith open(\"test.json\", \"rt\", encoding=\"Utf-8\") as f:\r\n    new_obj = json.load(json_obj, f, cls=ComplexJSON.ComplexJSONDecoder)\r\n```\r\n\r\n#### Change codewords for ComplexJSON ####\r\n\r\nBy default, the codewords for the module and class are \"\\_\\_module\\_\\_\" and \"\\_\\_class\\_\\_\", respectively\r\n\r\n```python\r\nclass A:\r\n    def __init__(self):\r\n        self.a = 1\r\n        self.b = 2\r\n```\r\n\r\nObject of this class will be serialized as:\r\n```python\r\n{\"a\": 1, \"b\": 2, \"__module__\": \"__main__\", \"__class__\": \"A\"}\r\n```\r\n\r\nBut you can change the codewords for module and class:\r\n\r\n```python\r\nfrom ComplexJSON import vardef\r\n\r\nvardef.classword = \"_c_\"\r\nvardef.moduleword = \"_m_\"\r\n```\r\n\r\nOr\r\n\r\n```python\r\nimport ComplexJSON\r\n\r\nComplexJSON.vardef.classword = \"_c_\"\r\nComplexJSON.vardef.moduleword = \"_m_\"\r\n```\r\n\r\nAnd now A class will be serialized as:\r\n```python\r\n{\"a\": 1, \"b\": 2, \"_m_\": \"__main__\", \"_c_\": \"A\"}\r\n```\r\n\r\n#### Remove codewords from json (v1.1+) ####\r\n\r\n```python\r\nfrom ComplexJSON import vardef\r\n\r\nvardef.classword = None  # remove classword from json\r\nvardef.moduleword = None  # remove moduleword from json\r\n```\r\n\r\nIf you remove both, object will be decoded as python dict.\r\nIf you remove moduleword, you need to provide class in classStorage argument in load, loads or ComplexJSONDecoder\r\n```python\r\nimport ComplexJSON as json\r\njson.vardef.moduleword = None\r\nclass A:\r\n    b = 1\r\n...\r\nclassStorage = [A]\r\n# or\r\nclassStorage = {\"A\": A}\r\n...\r\njson.loads(obj, classStorage=classStorage)\r\n# or\r\ndecoder = json.ComplexJSONDecoder(classStorage=classStorage)\r\n...\r\ndecoder.decode(obj)\r\n# or \r\njson.loads(obj, object_hook=decoder.object_hook)\r\n```\r\n\r\n#### Local codewords (v1.1+) ####\r\n\r\n```python\r\nimport ComplexJSON as json\r\n...\r\n_json = json.dumps(obj, localClassWord=\"Type\", localModuleWord=\"Module\")\r\nnew_obj = json.loads(_json, localClassWord=\"Type\", localModuleWord=\"Module\")\r\n```\r\n\r\n#### Type Association (v1.2+) ####\r\n\r\n```python\r\nimport ComplexJSON\r\n\r\n\r\nclass A:\r\n    def __init__(self):\r\n        self.a = 1\r\n        self.b = 2\r\n\r\n\r\nrs = {1: A()}\r\njson_obj = ComplexJSON.dumps(rs, useTypeAssociation=True)\r\nprint(json_obj)\r\n#>>> {\"types\": {\".\": {\"useTA\": true}, \"0\": {\"__module__\": \"__main__\", \"__class__\": \"A\"}}, \"obj\": {\"1\": {\"a\": 1, \"b\": 2, \"__cid__\": \"0\"}}}\r\nprint(ComplexJSON.loads(json_obj))\r\n#>>> {'1': <__main__.A object at 0x00000154D6013490>}\r\n```\r\n\r\n----------\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "This is the simplest module for serialize and deserialize python complex objects",
    "version": "1.2.0",
    "project_urls": {
        "Homepage": "https://github.com/dail45/ComplexJSON"
    },
    "split_keywords": [
        "json",
        "complexobject",
        "serialize"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2c2d57668cea2e2b278bdddeded702ea2e42e6821c8199712d03999a92771cd1",
                "md5": "faf10bfa5b673a2500cc984a387a45ce",
                "sha256": "642613ff218fedb6f1c40aa6149fa587bf25ebf33eb962cd82cf36d232a6aed4"
            },
            "downloads": -1,
            "filename": "ComplexJSON-1.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "faf10bfa5b673a2500cc984a387a45ce",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 7298,
            "upload_time": "2024-05-06T12:52:27",
            "upload_time_iso_8601": "2024-05-06T12:52:27.909620Z",
            "url": "https://files.pythonhosted.org/packages/2c/2d/57668cea2e2b278bdddeded702ea2e42e6821c8199712d03999a92771cd1/ComplexJSON-1.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c0174e4f1761c233b6cf1f914fb2644fc5ef707a1f89500424b26789a2a390fd",
                "md5": "371d5a0072dbdfa5263aad2568f85c37",
                "sha256": "a3ad13632d5248c9201facc4e883fdc9203a2df0774d0395e7252bbc93603c87"
            },
            "downloads": -1,
            "filename": "ComplexJSON-1.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "371d5a0072dbdfa5263aad2568f85c37",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 5584,
            "upload_time": "2024-05-06T12:52:30",
            "upload_time_iso_8601": "2024-05-06T12:52:30.093925Z",
            "url": "https://files.pythonhosted.org/packages/c0/17/4e4f1761c233b6cf1f914fb2644fc5ef707a1f89500424b26789a2a390fd/ComplexJSON-1.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-06 12:52:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dail45",
    "github_project": "ComplexJSON",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "complexjson"
}
        
Elapsed time: 0.25121s