This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!
Project Description

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 History

Release History

1.1.0

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.0.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
ftw.jsondump-1.1.0.tar.gz (24.4 kB) Copy SHA256 Checksum SHA256 Source Oct 11, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting