def _test_json_output(self, u, j): protocol_factory = TSimpleJSONProtocol.TSimpleJSONProtocolFactory() databuf = TTransport.TMemoryBuffer() prot = protocol_factory.getProtocol(databuf) u.write(prot) self.assertEquals(j, json.loads(databuf.getvalue().decode()))
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 readStuffFromJSON(jstr, struct_type=Stuff): stuff = struct_type() trans = TMemoryBuffer(jstr) proto = TSimpleJSONProtocol.TSimpleJSONProtocol(trans, struct_type.thrift_spec) stuff.read(proto) return stuff
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 __thrift_to_json(x): trans = TTransport.TMemoryBuffer() proto = TSimpleJSONProtocol.TSimpleJSONProtocol(trans) x.write(proto) return json.loads(trans.getvalue())
class SimpleJSONTest(AbstractTest): protocol_factory = TSimpleJSONProtocol.TSimpleJSONProtocolFactory()
class TestForwardCompatibilityJSON(TestForwardCompatibilityAbstract, unittest.TestCase): protocol_factory = TSimpleJSONProtocol.TSimpleJSONProtocolFactory()
def writeToJSON(obj): trans = TMemoryBuffer() proto = TSimpleJSONProtocol.TSimpleJSONProtocol(trans) obj.write(proto) return trans.getvalue()
def should_throw(): j = '{"aString": "foo", "anotherString": nullcorrupt}' stuff = Stuff() Serializer.deserialize( TSimpleJSONProtocol.TSimpleJSONProtocolFactory(), j, stuff )
def serialize_one(feature_parameters): trans = TMemoryBuffer() proto = TSimpleJSONProtocol.TSimpleJSONProtocol(trans) feature_parameters.write(proto) return trans.getvalue().decode("utf-8").replace("\n", "")
TSimpleJSONProtocol, ) from thrift.protocol import fastproto # type: ignore # noqa: F401 from thrift.util import Serializer from .adapter.ttypes import Foo, FooWithoutAdapters from .adapter_bar.ttypes import Bar PROTOCOLS = [ TBinaryProtocol.TBinaryProtocolFactory(), TBinaryProtocol.TBinaryProtocolAcceleratedFactory(), TCompactProtocol.TCompactProtocolFactory(), TCompactProtocol.TCompactProtocolAcceleratedFactory(), TJSONProtocol.TJSONProtocolFactory(), TSimpleJSONProtocol.TSimpleJSONProtocolFactory(), ] class AdapterTest(unittest.TestCase): def test_roundtrip(self) -> None: INPUTS = { "empty": (Foo(), FooWithoutAdapters()), "default_values": ( Foo( structField={}, oStructField={}, mapField={}, ), FooWithoutAdapters( structField=Bar(),