将JSON模式转换为python类 [英] Convert a JSON schema to a python class
问题描述
是否有一个python库,用于将JSON模式转换为python类定义,类似于jsonschema2pojo- https://github.com/joelittlejohn/jsonschema2pojo -适用于Java吗?
Is there a python library for converting a JSON schema to a python class definition, similar to jsonschema2pojo -- https://github.com/joelittlejohn/jsonschema2pojo -- for Java?
推荐答案
到目前为止,我能找到的最接近的东西是术士,该广告为该工作流程做广告:
So far the closest thing I've been able to find is warlock, which advertises this workflow:
构建您的架构
>>> schema = {
'name': 'Country',
'properties': {
'name': {'type': 'string'},
'abbreviation': {'type': 'string'},
},
'additionalProperties': False,
}
创建模型
>>> import warlock
>>> Country = warlock.model_factory(schema)
使用模型创建对象
>>> sweden = Country(name='Sweden', abbreviation='SE')
但是,这并不是那么容易.术士生产的物品缺少许多无法反省的东西.而且,如果它在初始化时支持嵌套字典,那么我将无法弄清楚如何使它们工作.
However, it's not quite that easy. The objects that Warlock produces lack much in the way of introspectible goodies. And if it supports nested dicts at initialization, I was unable to figure out how to make them work.
为了提供一些背景知识,我正在研究的问题是如何处理 Chrome的JSONSchema API ,并生成一棵请求生成器和响应处理程序的树.术士似乎并不太遥不可及,唯一的缺点是Python中的元类不能真正变成代码".
To give a little background, the problem that I was working on was how to take Chrome's JSONSchema API and produce a tree of request generators and response handlers. Warlock doesn't seem too far off the mark, the only downside is that meta-classes in Python can't really be turned into 'code'.
要查找的其他有用模块:
Other useful modules to look for:
- jsonschema -(Warlock构建于此之上)
- valideer -与jsonschema相似,但名称更差.
- bunch -一个有趣的结构构建器,介于构建
- jsonschema - (which Warlock is built on top of)
- valideer - similar to jsonschema but with a worse name.
- bunch - An interesting structure builder thats half-way between a dotdict and construct
如果您最终找到了一个不错的一站式解决方案,请跟进您的问题-我很乐意找到一个.我倾倒了github,pypi,googlecode,sourceforge等.然后找不到真正性感的东西.
If you end up finding a good one-stop solution for this please follow up your question - I'd love to find one. I poured through github, pypi, googlecode, sourceforge, etc.. And just couldn't find anything really sexy.
由于缺乏任何预制解决方案,我可能会自己和术士一起拼凑一些东西.因此,如果我击败您,我将更新我的答案. :p
For lack of any pre-made solutions, I'll probably cobble together something with Warlock myself. So if I beat you to it, I'll update my answer. :p
这篇关于将JSON模式转换为python类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!