Jump to >

djblets.webapi.encoders

class WebAPIEncoder[source]

Bases: object

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]

Bases: djblets.webapi.encoders.WebAPIEncoder

A basic encoder that encodes standard types.

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

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

Bases: djblets.webapi.encoders.WebAPIEncoder

An encoder that encodes objects based on registered resources.

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

Bases: json.encoder.JSONEncoder

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.

__init__(encoder, *args, **kwargs)[source]
encode(o, *args, **kwargs)[source]
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]

Bases: object

Adapts a WebAPIEncoder to output XML.

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

__init__(encoder, *args, **kwargs)[source]
encode(o, *args, **kwargs)[source]
startElement(name, attrs={})[source]
endElement(name)[source]
text(value)[source]
addIndent()[source]
get_registered_encoders()[source]

Returns a list of registered Web API encoders.