Conversion Functions

By default, this library uses the default pickle and json modules of the standard Python installation to perform conversion to and from those formats.

Sometimes there may be a wish to use a different implementation of those functions (for example, cPickle or a faster JSON encoder/ decoder).

There are two functions available to change the default Pickle and JSON converters.

Note that these immediately affect all Connection objects. If you wish to have a finer grained control over which object uses which converters, you may wish to consider writing your own Transcoder implementation instead.

couchbase.set_json_converters(encode, decode)

Modify the default JSON conversion functions. This affects all Connection instances.

These functions will called instead of the default ones (json.dumps and json.loads) to encode and decode JSON (when FMT_JSON is used).

Parameters:
  • encode (callable) – Callable to invoke when encoding an object to JSON. This should have the same prototype as json.dumps, with the exception that it is only ever passed a single argument.
  • decode (callable) – Callable to invoke when decoding an object to JSON. This should have the same prototype and behavior as json.loads with the exception that it is only ever passed a single argument.
Returns:

A tuple of (old encoder, old decoder)

No exceptions are raised, and it is the responsibility of the caller to ensure that the provided functions operate correctly, otherwise exceptions may be thrown randomly when encoding and decoding values

couchbase.set_pickle_converters(encode, decode)

Modify the default Pickle conversion functions. This affects all Connection instances.

These functions will be called instead of the default ones (pickle.dumps and pickle.loads) to encode and decode values to and from the Pickle format (when FMT_PICKLE is used).

Parameters:
  • encode (callable) – Callable to invoke when encoding an object to Pickle. This should have the same prototype as pickle.dumps with the exception that it is only ever called with a single argument
  • decode (callable) – Callable to invoke when decoding a Pickle encoded object to a Python object. Should have the same prototype as pickle.loads with the exception that it is only ever passed a single argument
Returns:

A tuple of (old encoder, old decoder)

No exceptions are raised and it is the responsibility of the caller to ensure that the provided functions operate correctly.

Previous topic

Using With and Without Threads

Next topic

Item Objects

This Page