Skip to main content

Type-safe and eazy json-to-object-and-back conversions.

Project description

Restorify

Convert json objects and files into python objects of type that you need.

Quick guide

Simple conversions

The code below will convert json list into list, tuple and set respectively.

from restorify import restore

j1 = """
[
	1, 2, 3, 1
]
"""
print( restore(j1, list[float]) )
print( restore(j1, tuple[float]) )
print( restore(j1, set[float]) )

Output:

[1.0, 2.0, 3.0, 1.0]
(1.0, 2.0, 3.0, 1.0)
{1.0, 2.0, 3.0}

Collections conversions

You can easily convert collections outside of simple built-ins. Note how nested tuple and set are accounted for.

from restorify import restore
from collections import OrderedDict

j2 = """
{
	"key1" : [1, 2, 3]
}
"""
print( restore(j2, OrderedDict[str, tuple]) )
print( restore(j2, OrderedDict[str, set]) )

Output:

OrderedDict({'key1': (1, 2, 3)})
OrderedDict({'key1': {1, 2, 3}})

Dataclasses

Dataclasses (and everything type-annotated really) can be converted too.

from restorify import restore
from dataclasses import dataclass

@dataclass
class TestClass1:
	id: str
	data: list[int]

j3 = """
{
	"id" : "MyId",
	"data": [ 1, 42, 69 ]
}
"""

@dataclass
class TestClassNested:
	test1_list: list[TestClass1]

j4 = """
{
	"test1_list": [
		{
			"id" : "MyId1",
			"data": [ 1, 42, 69 ]
		},
		{
			"id" : "MyId2",
			"data": [ 321, 421, 3011 ]
		}
	]
}
"""
print( restore(j4, TestClassNested) )
print( restore(j3, TestClass1) )

Output:

TestClass1(id='MyId', data=[1, 42, 69])
TestClassNested(test1_list=[TestClass1(id='MyId1', data=[1, 42, 69]), TestClass1(id='MyId2', data=[321, 421, 3011])])

Implicit file path conversions

Why loading jsons manually if you can just get and object from a file path? Note the use of @restorable decorator and RestorableClass.json_name class member. In the end, the new file is dumped into new directory (but with the same name).

from restorify import restore
from dataclasses import dataclass
from pathlib import Path

@dataclass
@restorable
class RestorableClass:
	field: int
	another_filed: list[list]

p1 = Path(RestorableClass.json_name)
obj = restore(p1)
print(obj)

p2 = Path('another_dir')
p2.mkdir()
obj.dump(p2)

Output:

RestorableClass(field=5, another_filed=[[1, 1.0, 'str'], [[4, 5, 6], 'hello!']])

The restorable_class.json file contents is the following:

{
    "field": 5,
    "another_filed": [
        [
            1,
            1.0,
            "str"
        ],
        [
            [
                4,
                5,
                6
            ],
            "hello!"
        ]
    ]
}

Failed conversion

Upon failed conversion TypeError is raised.

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

restorify-1.0.3.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

restorify-1.0.3-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file restorify-1.0.3.tar.gz.

File metadata

  • Download URL: restorify-1.0.3.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for restorify-1.0.3.tar.gz
Algorithm Hash digest
SHA256 4a6fb64ffc169f5b97a88b2440276a59f1e6c7e9ee8652c295bc73e1df78229a
MD5 f16c289d5a1ab35075cc65ea158de3f5
BLAKE2b-256 f9a3d1d9e71137b6096204ae5a9ee1c705808ccd6016624a538de12b2c7d081b

See more details on using hashes here.

File details

Details for the file restorify-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: restorify-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for restorify-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c6de17698c41502c2dcca1dd57b5908843919d030129f9e941886b41638f8b4a
MD5 577f27579ac9bbd49995f6b8268a5151
BLAKE2b-256 f6ab9cf8f4a9caec8d4b4e0afe8f65849e48c1cd72e2f7a60ed8a9b8c2a078b8

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