How pek/jsno works
Purpose and public API
jsno is a dependency-free Python library that converts arbitrary supported Python values to JSON-compatible trees and reconstructs typed values from those trees. It deliberately delegates JSON text encoding/decoding to the standard library. Public package exports and convenience APIs are rooted in jsno/__init__.py; primary entry points are implemented in jsno/jsonify.py, jsno/unjsonify.py, and likely JSON-string wrappers in jsno/jsonize.py.
Serialization dispatch
jsonify is the outbound recursive conversion engine. It uses singledispatch, allowing built-in handlers and consumers to register serializers per concrete type. Native JSON values pass through; containers and domain objects recursively become lists, dictionaries, strings, or scalar JSON values. Type-family handlers are separated by concern in jsno/standard.py, jsno/datetime.py, jsno/tuple.py, and jsno/abc.py.
Typed deserialization dispatch
unjsonify is the inbound, target-type-driven engine: callers select a decoder through bracket syntax (for example, unjsonify[MyType](data)). Its dispatch and target-type interpretation live in jsno/unjsonify.py, with annotation/type-introspection support in jsno/utils.py and field-level decoding in jsno/fields_unjsonifier.py. This is the key boundary: JSON-compatible input is untyped, while output is validated and constructed according to Python type metadata.
Structured model reconstruction
Dataclasses are serialized as JSON objects and reconstructed from resolved field annotations. The decoder handles recursive/future annotations, optional and union-like typing, field naming, defaults, and unexpected-key policy. Relevant model features are distributed across jsno/unjsonify.py, jsno/property_name.py, and jsno/extra_data.py. Extra JSON keys normally cause an unjsonification error; context can instead ignore them, supporting backward-compatible persisted data.
Type-system extensions and validation
The library recognizes typing constructs beyond ordinary classes: Annotated, Literal, TypedDict, NewType, named tuples, unions, and schemas. jsno/constraint.py attaches boolean validation rules to annotations/classes and applies them after deserialization. jsno/typeddict.py implements TypedDict-specific shape semantics. These mechanisms make inbound conversion both structural and semantic rather than merely coercive.
Dynamic schemas and callable boundaries
Schema supports runtime-defined object shapes, including requiredness, ignored/typed extra fields, and extraction from function signatures for message-to-keyword-argument conversion. This enables integrations such as HTTP/event handler boundaries without defining a dataclass. The architecture is centered in jsno/schema.py and relies on jsno/unjsonify.py for actual value conversion.
Polymorphism and external boundaries
Variant families encode class identity into JSON object labels so a base type can be reconstructed as a registered subclass; this is implemented in jsno/variant.py. The package runs as a Python library (release notes indicate Python 3.13), has no third-party dependencies, and uses only standard-library facilities such as json, dataclasses, typing, datetime/path/UUID/regex support. Consumers provide persistence, APIs, files, or databases; jsno’s boundary output is JSON-compatible data or standard JSON strings through its convenience wrappers.
Main entry points: unjsonify.py unjsonify[T] · jsonify.py jsonify · __init__.py Package exports · fields_unjsonifier.py Field decoder
Components 16
Public API
- Package exports
jsno/__init__.py - JSON string wrappers · Delegates JSON text encoding and decoding to the standard library.
jsno/jsonize.py
Serialization
- jsonify · Recursively converts supported values into JSON-compatible trees.
jsno/jsonify.py - Type-family serializers · Serializers for standard, datetime, tuple, and ABC types.
jsno/standard.py
Typed deserialization
- unjsonify[T] · Interprets target types and constructs validated typed values.
jsno/unjsonify.py - Type introspection · Resolves typing constructs and recursive annotations.
jsno/utils.py - Field decoder · Decodes resolved annotations for individual fields.
jsno/fields_unjsonifier.py - Property naming
jsno/property_name.py - Extra-key policy · Rejects or ignores unexpected object keys.
jsno/extra_data.py
Model extensions
- Schema · Defines dynamic shapes and maps messages to callable keyword arguments.
jsno/schema.py - TypedDict support
jsno/typeddict.py - Constraints · Applies boolean annotation or class rules after decoding.
jsno/constraint.py - Variant families · Encodes class labels and reconstructs registered subclasses.
jsno/variant.py
Other components
- Consumer application · Supplies Python values or JSON-compatible data and target types.
- JSON-compatible tree · Lists, objects, strings, and JSON scalars.
- Typed Python values · Dataclasses, typing constructs, schemas, and variant instances.
Connections 20
- Consumer application → Package exports: imports API
- Package exports → jsonify: exports
- Package exports → unjsonify[T]: exports
- Package exports → JSON string wrappers: exports
- Consumer application → Typed Python values: provides
- Typed Python values → jsonify: serialize
- jsonify → Type-family serializers: dispatches by type
- jsonify → JSON-compatible tree: produces
- JSON-compatible tree → JSON string wrappers: encodes or decodes
- JSON-compatible tree → unjsonify[T]: input data
- unjsonify[T] → Type introspection: resolves target metadata
- unjsonify[T] → Field decoder: binds fields
- Field decoder → Property naming: maps keys
- Field decoder → Extra-key policy: enforces key policy
- unjsonify[T] → TypedDict support: decodes TypedDicts
- unjsonify[T] → Schema: executes schema conversion
- unjsonify[T] → Constraints: validates result
- unjsonify[T] → Variant families: reconstructs subtype
- unjsonify[T] → Typed Python values: constructs
- Variant families → jsonify: adds class label