Skip to main content

A json serializer and naming implementation

Project description

My Simple Json Serializer and Naming Utilities

The main class is the serializer - HardSerializer

It's not particularly hard, just doesn't try to infer the class to deserialize (like my previous iterations), you need to specify the class directly. Also, it's fairly picky about the format of the class - you need to use typing EVERYWHERE or it doesn't work. It uses a lot of reflection to figure stuff out, and the typing is critical.

It can figure out enums, lists, hierarchies, etc. Pretty powerful. The json is standard, no tags or anything, and compatible with other languages and libraries like System.Text.Json in .NET

It essentially converts the object model into dicts and then json.dumps() them to serialize, the reverse for deserialize.

Usage

You need to specify the fields in the class definition as well as in the init - like so:

class TestClass(object):
    id: int
    name: str
    guid: uuid.UUID
    test_enum: TestEnum

    def __init__(
        self,
        id: int = 0,
        name: str = "",
        guid: uuid.UUID = uuid.uuid4(),
        test_enum: TestEnum = TestEnum.Undefined,
    ):
        self.id = id
        self.name = name
        self.guid = guid
        self.test_enum = test_enum

Note the typing hints everywhere

It can handle hierarchies as long as the type hints are there:

class ParentClass(object):
    id: int
    name: str
    guid: uuid.UUID
    test_enum: TestEnum
    children: list[TestClass]

    def __init__(
        self,
        id: int = 0,
        name: str = "",
        guid: uuid.UUID = uuid.uuid4(),
        test_enum: TestEnum = TestEnum.Undefined,
        children: list[TestClass] = [],
    ):
        self.id = id
        self.name = name
        self.guid = guid
        self.test_enum = test_enum
        self.children = children

Unit tests are included to see usage, but the basics are:

tc = TestClass(12, "Bob", test_enum=TestEnum.Value1)
json = serializer.serialize(tc)
new_tc = serializer.de_serialize(json, TestClass)

It will try to deserialize into the TestClass

Naming

The naming is a bit weirder, and uses custom dictionaries to work - not sure how else to do it.
The project originated from trying to form decent property names from database fieldnames.

Usage

Create the Naming class instance from the 2 dictionaries, the standard word list and the bigwords list (specifies common word combinations that are hard to infer)
Create Name class instances by using naming.string_to_name()
The Name class instance has different methods on it like Pascal, Snake, etc.
Simple.

Building

python -m build

Deploying

python -m twine upload --repository pypi dist/*

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

sb_serializer-0.0.3.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

sb_serializer-0.0.3-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file sb_serializer-0.0.3.tar.gz.

File metadata

  • Download URL: sb_serializer-0.0.3.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for sb_serializer-0.0.3.tar.gz
Algorithm Hash digest
SHA256 bd4e6e16cf24302527d35f26147ef1cc09c29c971a9219bccbf04bc901989d0d
MD5 a9c21afa130e6cfe49c50f731b62d7f5
BLAKE2b-256 362427f699767c742753cee5b5935817e07fb84b1c333bdb7490ef445bbcf539

See more details on using hashes here.

File details

Details for the file sb_serializer-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: sb_serializer-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for sb_serializer-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 958d689d49e553160478f091ba45f568a614708862fecce6871233a176e4409e
MD5 2b514ddfa8dda0612277b22b33235973
BLAKE2b-256 09373132ece8fe6d3ac7a2a5547d041240b0506efd920cd9cf8936ecbedfef08

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