Skip to main content

Recursively cast json to python dataclasses, typing, and class. Also provides reverse.

Project description

Smart Cast

Recursively cast json to python dataclasses, typing, and class

Features

Supports

  • List[TV]
  • Dict[TK, TV]
  • Optional[TV]
  • Union
  • dict
  • list
  • dataclass
  • int
  • str
  • float
  • boolean
  • datetime

useage

from smartcast import normal, cast
from typing import List, Optional, Union
import json
from dataclasses import dataclass
from enum import Enum, auto


class Option(Enum):
    A = auto()
    B = auto()


@dataclass
class Config:
    value: Option


def test_simple():
    value = Config(Option.A)
    nobj = normal(value)
    jstr = json.dumps(nobj)
    jobj = json.loads(jstr)
    revalue = cast(jobj, Config)
    assert value == revalue

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

smartcast-0.2.0.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

smartcast-0.2.0-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

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