JSON-LD API的Python实现
项目描述
简介
JSON,如RFC7159所述,是一种简单语言,用于在网络上表示对象。链接数据是一种在不同文档或网站之间描述内容的方法。网络资源使用IRI进行描述,通常是可解引用实体,可用于查找更多信息,创建“知识网络”。JSON-LD旨在成为在JSON中表示链接数据以及向现有JSON添加语义的简单发布方法。
JSON-LD被设计为一个轻量级的语法,用于表达链接数据。它主要用于在JavaScript和其他基于Web的编程环境中表达链接数据。在构建可互操作的Web服务和将链接数据存储在基于JSON的文档存储引擎中时也非常有用。它实用且设计得尽可能简单,利用了今天广泛使用的众多JSON解析器和现有代码。它能够表达键值对、RDF数据、RDFa数据、Microformats数据以及Microdata。也就是说,它支持目前所有主要的基于Web的结构化数据模型。
该语法不需要对许多应用程序的JSON进行太多修改,但可以通过添加上下文来轻松地添加意义,这种上下文可以是带内或带外的。语法设计为不会干扰已经部署在JSON上的系统,但提供了一条从JSON到带有语义的JSON的平滑迁移路径。最后,该格式旨在快速解析、快速生成、基于流和基于文档的处理兼容,并且为了运行需要非常小的内存占用。
符合性
本库旨在符合以下内容
JSON-LD 1.1,W3C候选推荐,2019-12-12或更新的版本
JSON-LD 1.1处理算法和API,W3C候选推荐,2019-12-12或更新的版本
JSON-LD 1.1框架,W3C候选推荐,2019-12-12或更新的版本
工作组测试套件
测试运行器经常更新以记录或跳过尚不支持的新测试。
要求
安装
PyLD可以使用pip 包进行安装
pip install PyLD
在pyld上定义依赖项不会拉入Requests或aiohttp。如果您需要其中一个用于文档加载器,则可以直接依赖所需的第三方库,或者将要求定义为PyLD[requests]或PyLD[aiohttp]。
快速示例
from pyld import jsonld
import json
doc = {
"http://schema.org/name": "Manu Sporny",
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
"http://schema.org/image": {"@id": "http://manu.sporny.org/images/manu.png"}
}
context = {
"name": "http://schema.org/name",
"homepage": {"@id": "http://schema.org/url", "@type": "@id"},
"image": {"@id": "http://schema.org/image", "@type": "@id"}
}
# compact a document according to a particular context
# see: https://json-ld.org/spec/latest/json-ld/#compacted-document-form
compacted = jsonld.compact(doc, context)
print(json.dumps(compacted, indent=2))
# Output:
# {
# "@context": {...},
# "image": "http://manu.sporny.org/images/manu.png",
# "homepage": "http://manu.sporny.org/",
# "name": "Manu Sporny"
# }
# compact using URLs
jsonld.compact('http://example.org/doc', 'http://example.org/context')
# expand a document, removing its context
# see: https://json-ld.org/spec/latest/json-ld/#expanded-document-form
expanded = jsonld.expand(compacted)
print(json.dumps(expanded, indent=2))
# Output:
# [{
# "http://schema.org/image": [{"@id": "http://manu.sporny.org/images/manu.png"}],
# "http://schema.org/name": [{"@value": "Manu Sporny"}],
# "http://schema.org/url": [{"@id": "http://manu.sporny.org/"}]
# }]
# expand using URLs
jsonld.expand('http://example.org/doc')
# flatten a document
# see: https://json-ld.org/spec/latest/json-ld/#flattened-document-form
flattened = jsonld.flatten(doc)
# all deep-level trees flattened to the top-level
# frame a document
# see: https://json-ld.org/spec/latest/json-ld-framing/#introduction
framed = jsonld.frame(doc, frame)
# document transformed into a particular tree structure per the given frame
# normalize a document using the RDF Dataset Normalization Algorithm
# (URDNA2015), see: https://www.w3.org/TR/rdf-canon/
normalized = jsonld.normalize(
doc, {'algorithm': 'URDNA2015', 'format': 'application/n-quads'})
# normalized is a string that is a canonical representation of the document
# that can be used for hashing, comparison, etc.
文档加载器
PyLD的默认文档加载器使用Requests。在生产环境中,您可能希望设置一个自定义加载器,该加载器至少设置超时值。您还可以强制请求使用https、设置客户端证书、禁用验证或设置其他Requests参数。
jsonld.set_document_loader(jsonld.requests_document_loader(timeout=...))
还提供了一种异步文档加载器,使用aiohttp。请注意,此文档加载器将异步性限制在获取文档上。处理循环仍然保持同步。
jsonld.set_document_loader(jsonld.aiohttp_document_loader(timeout=...))
如果没有指定文档加载器,则默认加载器设置为Requests。如果Requests不可用,则加载器设置为aiohttp。备用文档加载器是一个虚拟文档加载器,在每次调用时都会引发异常。
商业支持
如有需要,可从Digital Bazaar请求对本库的商业支持:support@digitalbazaar.com。
源代码
Python实现JSON-LD API的源代码可在以下网址找到
测试
本库包含一个示例测试工具,可用于验证对处理器的更改是否保持了正确的输出。
要运行示例测试,您需要通过克隆GitHub上托管的json-ld-api、json-ld-framing和normalization存储库来获取测试套件文件。
如果套件存储库作为PyLD源目录的兄弟目录可用,则可以使用以下命令运行所有测试
python tests/runtests.py
如果您想测试单个manifest .jsonld文件或包含manifest.jsonld的目录,则可以将这些文件或目录作为参数提供
python tests/runtests.py TEST_PATH [TEST_PATH...]
测试运行器支持通过设置-l requests或-l aiohttp来使用不同的文档加载器。默认文档加载器设置为Requests。
可以使用-e或--earl选项生成EARL报告。
项目详情
下载文件
为您的平台下载文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。