utility for deserializing LLSD data
The deserialization component is defined as a utility because the input data can be a string or a file. It might be possible to define this as an adapter on a string but a string is too generic for this. So that’s why it is a utility.
You can use it like this:
>>> s='<?xml version="1.0" ?><llsd><map><key>test</key><integer>1234</integer><key>foo</key><string>bar</string></map></llsd>'
We use queryUtility because this returns None instead of an exception when a utility is not registered. We use the content type we received as the name of the utility. Another option would of course be to subclas string to some LLSDString class and use an adapter. We then would need some factory for generating the LLSDString class from whatever came back from the HTTP call.
So here is how you use that utility: >>> deserializer = LLSDDeserializer() >>> llsd = deserializer.deserialize(s) >>> llsd {‘test’: 1234, ‘foo’: ‘bar’}
We can also test this with some non-LLSD string:
>>> llsd = deserializer.deserialize_string('mumpitz') # this is not LLSD
...
DeserializationFailed: deserialization failed for 'mumpitz', reason: 'invalid token at index 0: 109'
>>> llsd = deserializer.deserialize_string('barfoo')
...
DeserializationFailed: deserialization failed for 'barfoo', reason: 'binary notation not yet supported'
adapter for serializing a list to LLSD
An example: >>> d=[‘ChatSessionRequest’, ‘CopyInventoryFromNotecard’] >>> serializer = ListLLSDSerializer(d) >>> serializer.serialize() ‘<?xml version=”1.0” ?><llsd><array><string>ChatSessionRequest</string><string>CopyInventoryFromNotecard</string></array></llsd>’ >>> serializer.content_type ‘application/llsd+xml’
contains useful helper functions
adapter for serializing a dictionary to LLSD
An example: >>> d={‘foo’:’bar’, ‘test’:1234} >>> serializer = DictLLSDSerializer(d) >>> serializer.serialize() ‘<?xml version=”1.0” ?><llsd><map><key>test</key><integer>1234</integer><key>foo</key><string>bar</string></map></llsd>’ >>> serializer.content_type ‘application/llsd+xml’