json-strong-typing
raw JSON → 0.4.3 verified Mon Apr 27 auth: no python
A Python library for type-safe JSON serialization and deserialization using Python dataclasses. It enforces strict type checking, supports nested dataclasses, generic types, and optional fields. Current version 0.4.3, requires Python >=3.9, released under MIT license. Maintenance is active with periodic updates.
pip install json-strong-typing Common errors
error ModuleNotFoundError: No module named 'json_strong_typing' ↓
cause Incorrect import name; the package name on PyPI is 'json-strong-typing' but the module is 'strong_typing'.
fix
Change import to 'import strong_typing' or 'from strong_typing import ...'.
error TypeError: 'JsonDataclass' is not a dataclass and should be used as a base class ↓
cause Using JsonDataclass as a decorator instead of a base class.
fix
Use 'class MyClass(JsonDataclass):' not '@JsonDataclass'.
error strong_typing.exceptions.ValidationError: Invalid value for field 'age': expected int, got str ↓
cause Mismatched types during deserialization.
fix
Ensure JSON input has correct types; use json_deserialize with proper type hints.
Warnings
gotcha The library is imported as 'strong_typing', not 'json_strong_typing'. Many users mistakenly pip install 'json-strong-typing' and then try import json_strong_typing, which fails. ↓
fix Use 'import strong_typing' or 'from strong_typing import ...'.
gotcha All fields in the dataclass must have type annotations; otherwise, the library may raise an error or silently skip them. ↓
fix Ensure every field has a type annotation (e.g., name: str, age: int).
gotcha When using Optional types, use typing.Optional or | None (Python 3.10+). The library distinguishes between missing and None values. ↓
fix Use field: Optional[int] = None or field: int | None = None.
deprecated In older versions (pre-0.4.0), the main class was JsonDataclass from 'strong_typing' but some users used 'from strong_typing import JsonDataclassJSON' which no longer exists. ↓
fix Upgrade to 0.4.x and use 'from strong_typing import JsonDataclass'.
Imports
- JsonDataclass wrong
from json_strong_typing import JsonDataclasscorrectfrom strong_typing import JsonDataclass - json_serialize
from strong_typing import json_serialize - json_deserialize
from strong_typing import json_deserialize
Quickstart
from dataclasses import dataclass
from strong_typing import JsonDataclass, json_serialize, json_deserialize
@dataclass
class Person(JsonDataclass):
name: str
age: int
p = Person(name="Alice", age=30)
serialized = json_serialize(p)
print(serialized)
deserialized = json_deserialize(Person, serialized)
print(deserialized)