json2pyclass
Project description
JSON Schema to Python Classes converter. I created this since it seems the other projects either are abandoned, or they weren’t able to parse K8s JSON Schema definitions.
Furthermore this converter generates definitions that mypy can consume.
Install
pip install json2pyclass
Usage
Usage: json2pyclass [OPTIONS] INPUT_FILE OUTPUT_NAME
Options:
--mode TEXT Output mode [class|dict]. Dict is not yet
implemented. [default: class]
--optionals / --no-optionals Disable Optional[] generation. [default:
True]
--help Show this message and exit.
The --optionals from the actual types can be disabled if the data will be read, and you don’t want to always do if checks. For example in K8s most fields are optional, even namespace names, or metadata. When reading these values are always present, so you might want to just be able to access them as they are, without having to assert each value to make mypy happy.
In the future the plan is to implement also a Dict writer, that doesn’t dump custom classes. The advantage will be that you could do after this json.load_safe and just cast to the type.
Currently this API was created for usage with either addict, or the kubeapi part of adhesive, that returns types with the properties as regular class “fields”.
Sample
Assuming we have a definition such as:
{
"definitions": {
"io.k8s.api.admissionregistration.v1.MutatingWebhook": {
"description": "MutatingWebhook describes an admission webhook and the resources and operations it applies to.",
"properties": {
"admissionReviewVersions": {
"description": "AdmissionReviewVersions is an ordered list of preferred `AdmissionReview` versions the Webhook expects. API server will try to use first version in the list which it supports. If none of the versions specified in this list supported by API server, validation will fail for this object. If a persisted webhook configuration specifies allowed versions and does not include any versions known to the API Server, calls to the webhook will fail and be subject to the failure policy.",
"items": {
"type": "string"
},
"type": "array",
...
We could call:
json2pyclass mydefinitions.json mydefinitions.py
And this will yield a file with all the types defined in there:
from typing import Optional, Union, List, Dict, Any
class MutatingWebhook: # io.k8s.api.admissionregistration.v1.MutatingWebhook
"""
MutatingWebhook describes an admission webhook and the resources and operations it applies to.
"""
admissionReviewVersions: List[str]
clientConfig: 'WebhookClientConfig'
failurePolicy: str
matchPolicy: str
# ...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file json2pyclass-1.0.1.tar.gz
.
File metadata
- Download URL: json2pyclass-1.0.1.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9cfcdf51ba69e01d32581c5d8c1c27c6b78cb6f315abe82c125d1f7390a0b48 |
|
MD5 | da6f2ad0cbfada9403a877a25358e9be |
|
BLAKE2b-256 | aa355656009d426379259967c93cc318e3abd7dec76899f4c0f02150ae53e8f4 |