Jump to >

This documentation covers the in-development release of Djblets. You can see the latest stable docs or all previous versions.

djblets.webapi.encoders

class WebAPIEncoder[source]

Encodes an object into a dictionary of fields and values.

This object is used for both JSON and XML API formats.

Projects can subclass this to provide representations of their objects. To make use of a encoder, add the path to the encoder class to the project’s settings.WEB_API_ENCODERS list.

For example:

WEB_API_ENCODERS = (
    'myproject.webapi.MyEncoder',
)
encode(o, *args, **kwargs)[source]

Encodes an object.

This is expected to return either a dictionary or a list. If the object being encoded is not supported, return None, or call the superclass’s encode method.

class BasicAPIEncoder[source]

A basic encoder that encodes standard types.

This supports encoding of dates, times, QuerySets, Users, and Groups.

encode(o, *args, **kwargs)[source]

Encodes an object.

This is expected to return either a dictionary or a list. If the object being encoded is not supported, return None, or call the superclass’s encode method.

class ResourceAPIEncoder[source]

An encoder that encodes objects based on registered resources.

encode(o, *args, **kwargs)[source]

Encodes an object.

This is expected to return either a dictionary or a list. If the object being encoded is not supported, return None, or call the superclass’s encode method.

class JSONEncoderAdapter(encoder, *args, **kwargs)[source]

Adapts a WebAPIEncoder to be used with json.

This takes an existing encoder and makes it available to use as a json.JSONEncoder. This is used internally when generating JSON from a WebAPIEncoder, but can be used in other projects for more specific purposes as well.

encode(o, *args, **kwargs)[source]

Return a JSON string representation of a Python data structure.

>>> from json.encoder import JSONEncoder
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})
'{"foo": ["bar", "baz"]}'
default(o)[source]

Encodes an object using the supplied WebAPIEncoder.

If the encoder is unable to encode this object, a TypeError is raised.

class XMLEncoderAdapter(encoder, *args, **kwargs)[source]

Adapts a WebAPIEncoder to output XML.

This takes an existing encoder and adapts it to output a simple XML format.

get_registered_encoders()[source]

Returns a list of registered Web API encoders.