All Projects → ltworf → Typedload

ltworf / Typedload

Licence: gpl-3.0
Python library to load dynamically typed data into statically typed data structures

Programming Languages

python
139335 projects - #7 most used programming language
python3
1442 projects
types
53 projects

Projects that are alternatives of or similar to Typedload

Newtonsoft.json.schema
Json.NET Schema is a powerful, complete and easy to use JSON Schema framework for .NET
Stars: ✭ 167 (+39.17%)
Mutual labels:  json-schema, json, schema
Vue Json Ui Editor
Edit JSON in UI form with JSON Schema and Vue.js
Stars: ✭ 392 (+226.67%)
Mutual labels:  json-schema, json, schema
Vue Form Json Schema
Create forms using JSON schema. Bring your components!
Stars: ✭ 253 (+110.83%)
Mutual labels:  json-schema, json, schema
Schema Registry
Confluent Schema Registry for Kafka
Stars: ✭ 1,647 (+1272.5%)
Mutual labels:  json-schema, json, schema
Json Schema To Ts
Infer TS types from JSON schemas 📝
Stars: ✭ 261 (+117.5%)
Mutual labels:  json-schema, json, schema
Plank
A tool for generating immutable model objects
Stars: ✭ 449 (+274.17%)
Mutual labels:  json-schema, json, schema
Univalue
High performance RAII C++ JSON library and universal value object class
Stars: ✭ 46 (-61.67%)
Mutual labels:  json-schema, json
Jsonschema Key Compression
Compress json-data based on its json-schema while still having valid json
Stars: ✭ 59 (-50.83%)
Mutual labels:  json-schema, json
Autocomplete Json
Atom autocomplete for JSON files
Stars: ✭ 60 (-50%)
Mutual labels:  json-schema, schema
Ducky
Duck-Typed Value Handling for JavaScript
Stars: ✭ 71 (-40.83%)
Mutual labels:  json, typing
Brutusin Rpc
Self-describing JSON-RPC web services over HTTP, with automatic API description based on JSON-Schema
Stars: ✭ 36 (-70%)
Mutual labels:  json-schema, json
Schemasafe
A reasonably safe JSON Schema validator with draft-04/06/07/2019-09 support.
Stars: ✭ 67 (-44.17%)
Mutual labels:  json-schema, json
Mongoose Patch History
Mongoose plugin that saves a history of JSON patch operations for all documents belonging to a schema in an associated 'patches' collection
Stars: ✭ 82 (-31.67%)
Mutual labels:  json, schema
Graphql Factory
A toolkit for building GraphQL
Stars: ✭ 44 (-63.33%)
Mutual labels:  json, schema
Oakdex Pokedex
Ruby Gem and Node Package for comprehensive Generation 1-7 Pokedex data, including 809 Pokémon, uses JSON schemas to verify the data
Stars: ✭ 44 (-63.33%)
Mutual labels:  json-schema, json
Govalid
Data validation library for golang. [MIGRATING TO NEW ADDRESS]
Stars: ✭ 59 (-50.83%)
Mutual labels:  json, schema
Uvicorn Gunicorn Fastapi Docker
Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python 3.6 and above with performance auto-tuning. Optionally with Alpine Linux.
Stars: ✭ 1,014 (+745%)
Mutual labels:  json-schema, json
Avocado
Strongly-typed MongoDB driver for Rust
Stars: ✭ 70 (-41.67%)
Mutual labels:  json-schema, json
Json Node Normalizer
'json-node-normalizer' - NodeJS module that normalize json data types from json schema specifications.
Stars: ✭ 105 (-12.5%)
Mutual labels:  json-schema, json
Object Editor React
Schema-aware editor for structured JSON objects (drop-in React component)
Stars: ✭ 104 (-13.33%)
Mutual labels:  json, schema

typedload

Load and dump json-like data into typed data structures in Python3, enforcing a schema on the data.

This module provides an API to load dictionaries and lists (usually loaded from json) into Python's NamedTuples, dataclass, sets, enums, and various other typed data structures; respecting all the type-hints and performing type checks or casts when needed.

It can also dump from typed data structures to json-like dictionaries and lists.

It is very useful for projects that use Mypy and deal with untyped data like json, because it guarantees that the data will follow the specified schema.

Note that it is released with a GPL license.

Example

For example this dictionary, loaded from a json:

data = {
    'users': [
        {
            'username': 'salvo',
            'shell': 'bash',
            'sessions': ['pts/4', 'tty7', 'pts/6']
        },
        {
            'username': 'lop'
        }
    ],
}

Can be treated more easily if loaded into this type:

@dataclasses.dataclass
class User:
    username: str
    shell: str = 'bash'
    sessions: List[str] = dataclasses.field(default_factory=list)

class Logins(NamedTuple):
    users: List[User]

And the data can be loaded into the structure with this:

t_data = typedload.load(data, Logins)

And then converted back:

data = typedload.dump(t_data)

Supported types

Since this is not magic, not all types are supported.

The following things are supported:

  • Basic python types (int, str, bool, float, NoneType)
  • NamedTuple
  • Enum
  • Optional[SomeType]
  • List[SomeType]
  • Dict[TypeA, TypeB]
  • Tuple[TypeA, TypeB, TypeC] and Tuple[SomeType, ...]
  • Set[SomeType]
  • Union[TypeA, TypeB]
  • dataclass (requires Python 3.7)
  • attr.s
  • ForwardRef (Refer to the type in its own definition)
  • Literal (requires Python 3.8)
  • TypedDict (requires Python 3.8)
  • datetime.date, datetime.time, datetime.datetime
  • Path
  • IPv4Address, IPv6Address
  • typing.Any

Using Mypy

# This is treated as Any, no checks done.
data = json.load(f)

# This is treated as Dict[str, int]
# but there will be runtime errors if the data does not
# match the expected format
data = json.load(f)  # type: Dict[str, int]

# This is treated as Dict[str, int] and an exception is
# raised if the actual data is not Dict[str, int]
data = typedload.load(json.load(f), Dict[str, int])

So when using Mypy, it makes sense to make sure that the type is correct, rather than hoping the data will respect the format.

Install

  • pip install typedload
  • apt install python3-typedload
  • Latest and greatest .deb file is in releases

Documentation

The tests are hard to read but provide more in depth examples of the capabilities of this module.

Used by

As dependency, typedload is used by those entities. Feel free to add to the list.

  • Several universities around the world
  • United States Air Force
  • Exxonmobil
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].