json api到文档解析器
json-api-doc的Python项目详细描述
这个库提供了在规范化json api之间进行转换的能力。 (http://jsonapi.org/)文档和 在代码中更容易操作。 也可用作命令行实用程序和Python3模块。
反序列化
对于这个json api文档:
{"data":[{"type":"articles","id":"1","attributes":{"title":"JSON API paints my bikeshed!","body":"The shortest article. Ever.","created":"2015-05-22T14:56:29.000Z","updated":"2015-05-22T14:56:28.000Z"},"relationships":{"author":{"data":{"id":"42","type":"people"}}}}],"included":[{"type":"people","id":"42","attributes":{"name":"John","age":80,"gender":"male"}}]}
简化版为:
[{"type":"articles","id":"1","title":"JSON API paints my bikeshed!","body":"The shortest article. Ever.","created":"2015-05-22T14:56:29.000Z","updated":"2015-05-22T14:56:28.000Z","author":{"type":"people","id":"42","name":"John","age":80,"gender":"male"}}]
序列化
将dict转换为json api规范文档对象的根 必须包含一个$type键,其值与 对象的资源类型。任何子指令或指令子数组 包含$type键将被视为包含的文档并序列化 因此。
[{"$type":"articles","id":"1","title":"JSON API paints my bikeshed!","body":"The shortest article. Ever.","created":"2015-05-22T14:56:29.000Z","updated":"2015-05-22T14:56:28.000Z","author":{"$type":"people","id":"42","name":"John","age":80,"gender":"male"}}]
{"data":[{"type":"articles","id":"1","attributes":{"title":"JSON API paints my bikeshed!","body":"The shortest article. Ever.","created":"2015-05-22T14:56:29.000Z","updated":"2015-05-22T14:56:28.000Z"},"relationships":{"author":{"data":{"id":"42","type":"people"}}}}],"included":[{"type":"people","id":"42","attributes":{"name":"John","age":80,"gender":"male"}}]}
用作python模块
importjson_api_docdocument={'data':{'type':'article','id':'1','attributes':{'name':'Article 1'}}}json_api_doc.deserialize(document)
importjson_api_docdocument={'$type':'article','id':'1','name':'Article 1'}json_api_doc.serialize(document)
用作cli
$ jsonapidoc document.json
贡献者
- 朱利安·杜普切勒(https://github.com/noplay)
- 安东尼奥·马蒂诺维奇(https://github.com/TopHatCroat)
许可证
免费软件:apache软件许可证2.0
文件
提供完整的文档:https://json-api-doc.readthedocs.io。