Skip to main content

ftw.jsondump provides JSON representations for Plone objects. By using adapters the JSON representation can easily be customized.

Installation

Add the package as dependency to your setup.py:

setup(...
      install_requires=[
        ...
        'ftw.jsondump',
      ])

or to your buildout configuration:

[instance]
eggs += ftw.jsondump

and rerun buildout.

Usage

For extracting the JSON of an object, use the IJSONRepresentation adapter:

from ftw.jsondump.interfaces import IJSONRepresentation
from zope.component import getMultiAdapter

json_representation = getMultiAdapter((context, request), IJSONRepresentation)
print json_representation.json()

Partials

The JSON is built using “partials”, which are merged into one dict.

There are various default partials:

  • metadata partial, providing infos such as _type and _class

  • fields partial extracting Archetypes and Dexterity field data

  • uid partial, providing the UID in _uid

  • localroles partial, extracting the local roles

  • workflow partial, providing the _workflow_chain and the _workflow_history

  • properties partial, providing local properties in _properties

  • interfaces partial, extracting the directly provided interfaces in _directly_provided

Selecting partials

The desired partials can be selected when extracting the 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'])

File blob data The file data is extracted by default as base64 encoded string and embedded in the JSON document.

This fieldata can be excluded with the filedata configuration:

from ftw.jsondump.interfaces import IJSONRepresentation
from zope.component import getMultiAdapter

json_representation = getMultiAdapter((context, request), IJSONRepresentation)
print json_representation.json(filedata=False)

For doing custom things with the filedata, a callback can be used:

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)

Creating custom partials

Custom partials can easily be registered as adapter:

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'))}

Field data extractors

The Archetypes and Dexterity partial use field data extractor adapters for extracting the field data and converting it to a JSON serializable value.

Custom extractors can easily be registered for custom fields:

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})

Changelog

1.1.0 (2015-10-11)

  • Change file_callback signature to also include the key used in the dict. For dexterity content, the key is different than the fieldname because it is prefixed with the interface dottedname.

    • Old: file_callback(context, fieldname, data, filename, mimetype, jsondata)

    • New: file_callback(context, key, fieldname, data, filename, mimetype, jsondata)

    [jone]

  • Dexterity: support exporting RichTextValue objects. [jone]

1.0.0 (2015-05-05)

  • Initial implementation [maethu, jone]

Release files for ftw.jsondump 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ftw.jsondump 1.1.0
File Size Uploaded
ftw.jsondump-1.1.0.tar.gz 24.4 kB Details

Release files / ftw.jsondump-1.1.0.tar.gz

Download URL ftw.jsondump-1.1.0.tar.gz
Size 24.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9b66c3a98752620e8d9790c5a55d453eaa2b1f7c7f3150d3767b4e57017939fa
BLAKE2b-256 checksum
How to use checksums
054ba38f4f4d5eb9fcb88146338d0fd1b35168b64aba0ac53bf392f0d308c4b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.1.0 This release

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page