def test_foreign_json(self): """ Not all JSON that we decode will be encoded by this python thrift protocol implementation. E.g. this encode implementation stuffs raw unicode into the output, but we may use this implementation to decode JSON from other implementations, which escape unicode (sometimes incorrectly e.g. PHP). And we may use this implementation to decode JSON that was not encoded by thrift at all, which may contain nulls. """ s = "a fancy e looks like \u00e9" j = '{"aString": "a fancy e looks like \\u00e9", "anotherString": null, "anInteger": 10, "unknownField": null}' stuff = Stuff() Serializer.deserialize( TSimpleJSONProtocol.TSimpleJSONProtocolFactory(), j, stuff ) self.assertEqual(stuff.aString, s) def should_throw(): j = '{"aString": "foo", "anotherString": nullcorrupt}' stuff = Stuff() Serializer.deserialize( TSimpleJSONProtocol.TSimpleJSONProtocolFactory(), j, stuff ) self.assertRaises(TProtocol.TProtocolException, should_throw)
def test_deserializer(self): j = '{"aShort": 1, "anInteger": 2, "aLong": 3}' stuff = Stuff() Serializer.deserialize( TSimpleJSONProtocol.TSimpleJSONProtocolFactory(), j, stuff) self.assertEqual(stuff.aShort, 1) self.assertEqual(stuff.anInteger, 2) self.assertEqual(stuff.aLong, 3)
def parse_dir_inode_data(self, data: bytes) -> OverlayDir: from thrift.util import Serializer from thrift.protocol import TCompactProtocol tree_data = OverlayDir() protocol_factory = TCompactProtocol.TCompactProtocolFactory() Serializer.deserialize(protocol_factory, data, tree_data) return tree_data
def testNestedStructs(self): nested = NestedStructs(bonk=Bonk(), bools=Bools()) json.loads(self._serialize(nested)) # Old protocol should be able to deserialize both valid and invalid # JSON. protocol_factory = TJSONProtocol.TJSONProtocolFactory(validJSON=False) Serializer.deserialize(protocol_factory, '{"1":{"rec":{}}"2":{"rec":{}}}', NestedStructs()) Serializer.deserialize(protocol_factory, '{"1":{"rec":{}},"2":{"rec":{}}}', NestedStructs())
def parse_dir_inode_data(self, data: bytes) -> OverlayDir: from thrift.util import Serializer from thrift.protocol import TCompactProtocol # Initialize entries to the empty dictionary. # This value will be used if the serialized data does not have any value # for this field. tree_data = OverlayDir(entries={}) protocol_factory = TCompactProtocol.TCompactProtocolFactory() Serializer.deserialize(protocol_factory, data, tree_data) return tree_data
def deserialize_thrift_object(raw_data, thrift_type, proto_factory=Consts.PROTO_FACTORY): ''' Deserialize thrift data from binary blob :param raw_data string: the serialized thrift payload :param thrift_type: the thrift type :param proto_factory: protocol factory, set default as Compact Protocol :return: instance of thrift_type ''' resp = thrift_type() Serializer.deserialize(proto_factory(), raw_data, resp) return resp
def _load_overlay_tree(overlay_dir: str, inode_number: int) -> OverlayDir: from thrift.util import Serializer from thrift.protocol import TCompactProtocol dir_name = '{:02x}'.format(inode_number % 256) overlay_file_path = os.path.join(overlay_dir, dir_name, str(inode_number)) with open(overlay_file_path, 'rb') as f: data = f.read() assert data[0:4] == b'OVDR' tree_data = OverlayDir() protocol_factory = TCompactProtocol.TCompactProtocolFactory() Serializer.deserialize(protocol_factory, data[64:], tree_data) return tree_data
def test_roundtrip(self) -> None: INPUTS = { "empty": (Foo(), FooWithoutAdapters()), "default_values": ( Foo( structField={}, oStructField={}, mapField={}, ), FooWithoutAdapters( structField=Bar(), oStructField=Bar(), mapField={}, ), ), "basic": ( Foo( structField={"field": 42}, oStructField={"field": 43}, mapField={ 1: { "field": 44 }, 2: { "field": 45 }, }, ), FooWithoutAdapters( structField=Bar(field=42), oStructField=Bar(field=43), mapField={ 1: Bar(field=44), 2: Bar(field=45), }, ), ), } for protocol in PROTOCOLS: for (name, (foo, foo_without_adapters)) in INPUTS.items(): with self.subTest(case=name, protocol=type(protocol).__name__): serialized = Serializer.serialize(protocol, foo) deserialized = Serializer.deserialize( protocol, serialized, Foo()) self.assertEqual(deserialized, foo) no_adapter = Serializer.deserialize( protocol, serialized, FooWithoutAdapters()) self.assertEqual(no_adapter, foo_without_adapters)
def test_exception_safety(self) -> None: for protocol in PROTOCOLS: with self.subTest(protocol=type(protocol).__name__): foo = Foo(structField={}) with patch( "thrift.test.py.adapter_for_tests.AdapterTestStructToDict.to_thrift" ) as mock_to_thrift, self.assertRaises(RuntimeError): mock_to_thrift.side_effect = RuntimeError() Serializer.serialize(protocol, foo) serialized = Serializer.serialize( protocol, FooWithoutAdapters(structField=Bar()), ) with patch( "thrift.test.py.adapter_for_tests.AdapterTestStructToDict.from_thrift" ) as mock_from_thrift, self.assertRaises(RuntimeError): mock_from_thrift.side_effect = RuntimeError() Serializer.deserialize(protocol, serialized, Foo())
def test_foreign_json(self): """ Not all JSON that we decode will be encoded by this python thrift protocol implementation. E.g. this encode implementation stuffs raw unicode into the output, but we may use this implementation to decode JSON from other implementations, which escape unicode (sometimes incorrectly e.g. PHP). And we may use this implementation to decode JSON that was not encoded by thrift at all, which may contain nulls. """ s = "a fancy e looks like \u00e9" j = '{"aString": "a fancy e looks like \\u00e9", "anotherString": null, "anInteger": 10, "unknownField": null}' stuff = Stuff() Serializer.deserialize( TSimpleJSONProtocol.TSimpleJSONProtocolFactory(), j, stuff) self.assertEqual(stuff.aString, s) def should_throw(): j = '{"aString": "foo", "anotherString": nullcorrupt}' stuff = Stuff() Serializer.deserialize( TSimpleJSONProtocol.TSimpleJSONProtocolFactory(), j, stuff) self.assertRaises(TProtocol.TProtocolException, should_throw)
def _t_request(self): # pylint: disable=method-hidden # Importing the Thrift models inline so that building them is not a # hard, import-time dependency for tasks like building the docs. from .thrift.ttypes import Loid as TLoid from .thrift.ttypes import Request as TRequest from .thrift.ttypes import Session as TSession _t_request = TRequest() _t_request.loid = TLoid() _t_request.session = TSession() if self._header: try: Serializer.deserialize( self._HEADER_PROTOCOL_FACTORY, self._header, _t_request, ) except Exception: logger.debug( "Invalid Edge-Request header. %s", self._header, ) return _t_request
def _deserialize(self, objtype, data): return Serializer.deserialize(self.protocol_factory, data, objtype())
def should_throw(): j = '{"aString": "foo", "anotherString": nullcorrupt}' stuff = Stuff() Serializer.deserialize( TSimpleJSONProtocol.TSimpleJSONProtocolFactory(), j, stuff)
def should_throw(): j = '{"aString": "foo", "anotherString": nullcorrupt}' stuff = Stuff() Serializer.deserialize( TSimpleJSONProtocol.TSimpleJSONProtocolFactory(), j, stuff )
def fromJson(msg, spec): return Serializer.deserialize(TSimpleJSONProtocolFactory(), msg, spec)