Plone内容的JSON表示
项目描述
ftw.jsondump 为Plone对象提供JSON表示。通过使用适配器,可以轻松地自定义JSON表示。
安装
将包作为依赖项添加到您的 setup.py
setup(...
install_requires=[
...
'ftw.jsondump',
])
或构建配置文件中
[instance]
eggs += ftw.jsondump
并重新运行构建。
使用
要提取对象的JSON,请使用 IJSONRepresentation 适配器
from ftw.jsondump.interfaces import IJSONRepresentation
from zope.component import getMultiAdapter
json_representation = getMultiAdapter((context, request), IJSONRepresentation)
print json_representation.json()
部分
JSON是通过“部分”构建的,这些部分合并为一个 dict。
有各种默认部分
metadata 部分提供有关 _type 和 _class 的信息
fields 部分提取Archetypes和Dexterity字段数据
uid 部分提供 _uid 中的UID
localroles 部分提取本地角色
workflow 部分提供 _workflow_chain 和 _workflow_history
properties 部分提供 _properties 中的本地属性
interfaces 部分提取在 _directly_provided 中直接提供的接口
选择部分
提取JSON时可以选择所需的部
from ftw.jsondump.interfaces import IJSONRepresentation
from zope.component import getMultiAdapter
json_representation = getMultiAdapter((context, request), IJSONRepresentation)
print json_representation.json(only=['fields', 'metadata'])
print json_representation.json(exclude=['localroles'])
文件blob数据 默认情况下,文件数据作为base64编码的字符串提取并嵌入到JSON文档中。
可以使用 filedata 配置排除此文件数据
from ftw.jsondump.interfaces import IJSONRepresentation
from zope.component import getMultiAdapter
json_representation = getMultiAdapter((context, request), IJSONRepresentation)
print json_representation.json(filedata=False)
对于对filedata进行自定义操作,可以使用回调函数
from ftw.jsondump.interfaces import IJSONRepresentation
from zope.component import getMultiAdapter
def file_callback(context, key, fieldname, data, filename, mimetype, jsondata):
with open('./tmp/' + filename, 'w+b') as target:
target.write(data)
json_representation = getMultiAdapter((context, request), IJSONRepresentation)
print json_representation.json(file_callback=file_callback)
创建自定义部分
自定义部分可以轻松注册为适配器
configure.zcml
<adapter factory=".partial.CustomAnnotations" name="custom_annotations" />
partial.py
from ftw.jsondump.interfaces import IPartial
from my.package.interfaces import ICustomContent
from zope.annotation import IAnnotations
from zope.component import adapts
from zope.interface import Interface
from zope.interface import implements
class CustomAnnotations(object):
implements(IPartial)
adapts(ICustomContent, Interface)
def __init__(self, context, request):
self.context = context
self.request = request
def __call__(self, config):
annotations = IAnnotations(self.context)
return {'_custom_annotations': dict(annotations.get('custom_config'))}
字段数据提取器
Archetypes和Dexterity部分使用字段数据提取适配器来提取字段数据并将其转换为可序列化为JSON的值。
可以轻松为自定义字段注册自定义提取器
configure.zcml
<adapter factory=".extractor.CustomFieldExtractor" />
extractor.py
from ftw.jsondump.interfaces import IFieldExtractor
from my.package import ICustomField
from zope.component import adapts
from zope.interface import implements
from zope.interface import Interface
class CustomFieldExtractor(object):
implements(IFieldExtractor)
adapts(Interface, Interface, ICustomField)
def __init__(self, context, request, field):
self.context = context
self.request = request
self.field = field
def extract(self, name, data, config):
value = self.field.get(self.context)
value = value.prepare_for_serialization()
data.update({name: value})
链接
版权
本软件包的版权归4teamwork所有。
ftw.jsondump使用GNU通用公共许可证,版本2。
变更日志
1.1.0 (2015-10-11)
将file_callback签名更改为同时包含在字典中使用的键。对于Dexterity内容,键与字段名不同,因为它以接口点名称为前缀。
旧:file_callback(context, fieldname, data, filename, mimetype, jsondata)
新:file_callback(context, key, fieldname, data, filename, mimetype, jsondata)
[jone]
Dexterity:支持导出RichTextValue对象。[jone]
1.0.0 (2015-05-05)
初始实现[maethu, jone]
项目详情
ftw.jsondump-1.1.0.tar.gz的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 9b66c3a98752620e8d9790c5a55d453eaa2b1f7c7f3150d3767b4e57017939fa |
|
MD5 | b56976b804364098f2c11fd4f6f298ca |
|
BLAKE2b-256 | 054ba38f4f4d5eb9fcb88146338d0fd1b35168b64aba0ac53bf392f0d308c4b4 |