Table Of Contents

Previous topic

Dictionary Document

Next topic

MessagePack

This Page

Json

The spyne.protocol.json package contains the Json-related protocols. Currently, only spyne.protocol.json.JsonDocument is supported.

Initially released in 2.8.0-rc.

This module is EXPERIMENTAL. You may not recognize the code here next time you look at it.

Missing Types

The JSON standard does not define every type that Spyne supports. These include Date/Time types as well as arbitrary-length integers and arbitrary-precision decimals. Integers are parsed to ints or longs seamlessly but Decimals are only parsed correctly when they come off as strings.

While it’s possible to e.g. (de)serialize floats to Decimals by adding hooks to parse_float [1] (and convert later as necessary), such customizations apply to the whole incoming document which pretty much messes up AnyDict serialization and deserialization.

It also wasn’t possible to work with object_pairs_hook as Spyne’s parsing is always “from outside to inside” whereas object_pairs_hook is passed dicts basically in any order “from inside to outside”.

[1]http://docs.python.org/2/library/json.html#json.loads
class spyne.protocol.json.JsonDocument(app=None, validator=None, mime_type=None, ignore_uncap=False, ignore_wrappers=True, complex_as=<type 'dict'>, ordered=False, default_string_encoding=None, **kwargs)[source]

Bases: spyne.protocol.dictdoc.HierDictDocument

An implementation of the json protocol that uses simplejson package when available, json package otherwise.

Parameters:
  • ignore_wrappers – Does not serialize wrapper objects.
  • complex_as – One of (list, dict). When list, the complex objects are serialized to a list of values instead of a dict of key/value pairs.
create_in_document(ctx, in_string_encoding=None)[source]

Sets ctx.in_document using ctx.in_string.

create_out_string(ctx, out_string_encoding='utf8')[source]

Sets ctx.out_string using ctx.out_document.

class spyne.protocol.json.JsonP(callback_name, *args, **kwargs)[source]

Bases: spyne.protocol.json.JsonDocument

The JsonP protocol puts the reponse document inside a designated javascript function call. The input protocol is identical to the JsonDocument protocol.

Parameters:callback_name – The name of the function call that will wrapp all response documents.

For other arguents, see spyne.protocol.json.JsonDocument.