jsonschema到python类转换器

jschema2p的Python项目详细描述


jschema2py

jschema2py能够从jsonschema开始生成python类,并为类型提供自动支持 以及约束检查。

安装

该软件包可以通过pip安装:

$ pip install jschema2py

或者从GitHub下载。

示例

例如,给定以下模式:

{"title":"UserInfo","type":"object","properties":{"name":{"type":"string","pattern":"^[^a-z0-9]"},"userName":{"type":"string"},"age":{"type":"integer","minimum":0,"maximum":100}}}

jschema2py可以很容易地以这种方式将其转换为python类(这里假设模式存储在变量中 调用的架构):

fromjschema2pyimportbuild_classUserInfo=build_class(schema)user=UserInfo()user.name="Jacopo"user.userName="JDL"user.age=24print(user)

将对对象操作执行验证:

user.name="jacopo"# raise ConstraintError (pattern: ^[^a-z0-9])user.age="24"# raise TypeError

对象可以序列化为json文档:

jsdoc=str(user)

访问生成的类

如果架构的一个属性引用另一个对象,则可以访问表示引用的 使用方法获取对象类:

{"title":"Nested","type":"object","properties":{"inner":{"title":"Inner","type":"object","properties":{"string":{"type":"string","default":"I'm inner!"}}}}}
fromjschema2pyimportbuild_classNested=build_class(schema)nested=Nested()nested.inner=nested.get_class("inner")()# Gets the class associated with the property "inner" print(nested.inner.string)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java序列化和复制对象   Java:一种JPA搜索规范,用于在另一个字符串中查找子字符串标记,该字符串也包含标记   函数式编程在Java 8中如何在不转换为数组的情况下检查流对象的内容?   java方法来查找要添加对象的ArrayList索引   数组Java Arrayindexoutofboundexception   java找不到安卓。拱生命周期:扩展:2.1.0。所需人员:项目:app>com。贝宝。签出:安卓sdk:0.1.0   Java Spring的JpaRepository:找不到用户类型的findbyUser属性   JAVAlang.NoSuchMethodError:安卓。操作系统。环境isExternalStorageRemovable   java使用JAX RS捕获未受影响的异常   如何在JavaSpringTomcat中快速关闭无响应websocket?   java ExoPlayer:将控制器放置在视频下方,不与视频重叠   windows server 2008为什么即使我是管理员,在使用Java访问某些文件时也会被拒绝访问?   java将表连接到jtextbox