Skip to main content

Object to JSON serialization with none to minimal need for custom declarations

Project description

Object-to-JSON

JSON serializer for class instances with minimal declaration overhead.

Features

  • Attributes that represent object state are automatically detected by checking __init__ argument names
  • Custom serializers for objects with complex state
  • Automatic generation of __eq__ method

Usage

Installation

pip install objtojson

Example

from objtojson import Serialized

class SomeClass(Serialized):
    def __init__(self, someAttribute, other):
        self.someAttribute = someAttribute
        self.other = other

class AnotherClass(Serialized):
    def __init__(self, a, b):
        self.a = a
        self.b = b

a = SomeClass(4, [AnotherClass(['a', 'list'], {'mykey': 34}), 'somestring'])      
print(a.Serialize())

Result:

{
    "__someclass__": {
        "someAttribute": 4,
        "other": [
            {
                "__anotherclass__": {
                    "a": [
                        "a",
                        "list"
                    ],
                    "b": {
                        "mykey": 34
                    }
                }
            },
            "somestring"
        ]
    }
}

Saving to file, loading from file

from objtojson import Serializer

Serializer.Save('data.json', a)
a_reloaded = Serializer.Load('data.json')

Restrictions

  • Attribute names must match __init__ argument names
  • Tuples will be serialized/deserialized as lists
  • Dict keys must be valid JSON object keys (i.e., strings)

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

objtojson-0.4.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

objtojson-0.4.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file objtojson-0.4.0.tar.gz.

File metadata

  • Download URL: objtojson-0.4.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5

File hashes

Hashes for objtojson-0.4.0.tar.gz
Algorithm Hash digest
SHA256 e075368842e653ad554236f5b5150233589db11042bbf8434dc9f43a34d7a6b9
MD5 1e89c9b2ef1fc59ac3092471444edd9c
BLAKE2b-256 2585f742a5144443a69c07b1b89c50e64f28c4f7dada965a2251bf408414c9d2

See more details on using hashes here.

File details

Details for the file objtojson-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: objtojson-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5

File hashes

Hashes for objtojson-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b302117328d3066305eaecfa9a69253290e5790582fa0a79c27f0a2b8edf0e0
MD5 5f35c025e8e265bd7c022227b0742552
BLAKE2b-256 7b45487e273f871df58ffc308e86219e6ca5d5459fa0ebcdb28078aff250eced

See more details on using hashes here.

Supported by

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