Skip to main content

No project description provided

Project description

Json Codec

It's a simple library to encode and decode json to strict python types using dataclasses and builtin python types.

Installation

pip install json-codec

poetry add json-codec

Usage

Parse a simple primitive type

from json_codec import decode
import json

assert decode(json.loads("true"), bool) is True
assert decode(json.loads("false"), bool) is False
assert decode(json.loads("null"), Optional[bool]) is None
assert decode(json.loads("1"), int) == 1
assert decode(json.loads("1"), Decimal) == Decimal("1")
assert decode(json.loads('"1.1"'), Decimal) == Decimal("1.1")
assert decode(json.loads('"1.1"'), float) == 1.1
assert decode(json.loads('"1.1"'), str) == "1.1"

assert decode(json.loads('[1,1]'), List[int]) == [1, 1]

Parse a dataclass

from dataclasses import dataclass
from json_codec import decode
import json

@dataclass(frozen=True)
class User:
    name: str
    age: int

assert decode({"name": "John", "age": 30}, User) == User(name="John", age=30)


@dataclass
class Dummy:
    text_list: List[str]
    text_dict: Dict[str, Decimal]
    optional_text: Optional[str]

dummy_json_text = """
{
    "text_list": ["a", "b", "c"],
    "text_dict": {
        "a": 1.0,
        "b": 2,
        "c": "3.3",
        "d": 2.2
    },
    "optional_text": "hello"
}
"""

dummy_json = json.loads(dummy_json_text)

parsed = decode(dummy_json, Dummy)

assert parsed.text_list == ["a", "b", "c"]
assert parsed.text_dict["a"] == Decimal("1.0")
assert parsed.text_dict["b"] == Decimal("2.0")
assert parsed.text_dict["c"] == Decimal("3.3")
assert parsed.text_dict["d"].quantize(Decimal("1.0")) == Decimal("2.2")
assert parsed.optional_text == "hello"

Parse a dataclass with a nested dataclass

from dataclasses import dataclass
from json_codec import decode
import json

 @dataclass
class NestedDummy:
    text: str
    number: Decimal

    boolean: bool

@dataclass
class Dummy:
    text_list: List[str]
    text_dict: Dict[str, Decimal]
    nested: NestedDummy

dummy_json_text = """
{

    "text_list": ["a", "b", "c"],
    "text_dict": {
        "a": 1.0,
        "b": 2,
        "c": "3.3",
        "d": 2.2
    },
    "nested": {
        "text": "hello",
        "number": 1.1,
        "boolean": true
    }
}
"""

dummy_json = json.loads(dummy_json_text)

parsed = decode(dummy_json, Dummy)

assert parsed.text_list == ["a", "b", "c"]
assert parsed.text_dict["a"] == Decimal("1.0")
assert parsed.text_dict["b"] == Decimal("2.0")
assert parsed.text_dict["c"] == Decimal("3.3")
assert parsed.text_dict["d"].quantize(Decimal("1.0")) == Decimal("2.2")
assert parsed.nested.text == "hello"
assert parsed.nested.number.quantize(Decimal("1.0")) == Decimal("1.1")
assert parsed.nested.boolean is True

Parse a newtype

from json_codec import decode
from typing import NewType
import json

UserId = NewType("UserId", int)

assert decode(json.loads("1"), UserId) == UserId(1)
assert isinstance(decode(json.loads("1"), UserId), int)

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

json_codec-0.3.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

json_codec-0.3.0-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file json_codec-0.3.0.tar.gz.

File metadata

  • Download URL: json_codec-0.3.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.7.10 Linux/5.15.90.1-microsoft-standard-WSL2

File hashes

Hashes for json_codec-0.3.0.tar.gz
Algorithm Hash digest
SHA256 3ab669f81bf025c9567c04c47daecae0ee45061e929ac6359a8230664cde1240
MD5 3744232d88762392bfb752c8fdde1b6d
BLAKE2b-256 5a351aec234d8dbe519aff7d68ac455c52cc8f6e8651788ff0fd141c2effe4bf

See more details on using hashes here.

File details

Details for the file json_codec-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: json_codec-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.7.10 Linux/5.15.90.1-microsoft-standard-WSL2

File hashes

Hashes for json_codec-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9e9ce1c6e229c9080a4c8e5df64df3a84a4bffd290e5d10907a16c7860ec0011
MD5 29617bc1caae5001ad872d90828c24aa
BLAKE2b-256 82056a64a0406acdc8fc27ceaf9837ba8689a3308873af4599ae7d898c930102

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