Message Encoding – JSON¶
fedmsg messages are encoded as JSON.
Use the functions fedmsg.encoding.loads(), fedmsg.encoding.dumps(),
and fedmsg.encoding.pretty_dumps() to encode/decode.
When serializing objects (usually python dicts) with
fedmsg.encoding.dumps() and fedmsg.encoding.pretty_dumps(), the
following exceptions to normal JSON serialization are observed.
datetime.datetimeobjects are correctly converted to seconds since the epoch.- For objects that are not JSON serializable, if they have a
.__json__()method, that will be used instead.- SQLAlchemy models that do not specify a
.__json__()method will be run throughfedmsg.encoding.sqla.to_json()which recursively produces a dict of all attributes and relations of the object(!) Be careful using this, as you might expose information to the bus that you do not want to. See Cryptography and Message Signing for considerations.
-
fedmsg.encoding.loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)¶ Deserialize
s(astrorunicodeinstance containing a JSON document) to a Python object.If
sis astrinstance and is encoded with an ASCII based encoding other than utf-8 (e.g. latin-1) then an appropriateencodingname must be specified. Encodings that are not ASCII based (such as UCS-2) are not allowed and should be decoded tounicodefirst.object_hookis an optional function that will be called with the result of any object literal decode (adict). The return value ofobject_hookwill be used instead of thedict. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).object_pairs_hookis an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value ofobject_pairs_hookwill be used instead of thedict. This feature can be used to implement custom decoders that rely on the order that the key and value pairs are decoded (for example, collections.OrderedDict will remember the order of insertion). Ifobject_hookis also defined, theobject_pairs_hooktakes priority.parse_float, if specified, will be called with the string of every JSON float to be decoded. By default this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal).parse_int, if specified, will be called with the string of every JSON int to be decoded. By default this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float).parse_constant, if specified, will be called with one of the following strings: -Infinity, Infinity, NaN, null, true, false. This can be used to raise an exception if invalid JSON numbers are encountered.To use a custom
JSONDecodersubclass, specify it with theclskwarg; otherwiseJSONDecoderis used.
-
fedmsg.encoding.dumps(self, o)¶ Return a JSON string representation of a Python data structure.
>>> JSONEncoder().encode({"foo": ["bar", "baz"]}) '{"foo": ["bar", "baz"]}'
-
fedmsg.encoding.pretty_dumps(self, o)¶ Return a JSON string representation of a Python data structure.
>>> JSONEncoder().encode({"foo": ["bar", "baz"]}) '{"foo": ["bar", "baz"]}'
SQLAlchemy Utilities¶
fedmsg.encoding.sqla houses utility functions for JSONifying
sqlalchemy models that do not define their own .__json__() methods.
Use at your own risk. fedmsg.encoding.sqla.to_json() will expose all
attributes and relations of your sqlalchemy object and may expose information
you not want it to. See Cryptography and Message Signing for considerations.
-
fedmsg.encoding.sqla.expand(obj, relation, seen)¶ Return the to_json or id of a sqlalchemy relationship.
-
fedmsg.encoding.sqla.to_json(obj, seen=None)¶ Returns a dict representation of the object.
Recursively evaluates to_json(...) on its relationships.