Exemplo n.º 1
0
    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()))
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
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
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
def __thrift_to_json(x):
    trans = TTransport.TMemoryBuffer()
    proto = TSimpleJSONProtocol.TSimpleJSONProtocol(trans)
    x.write(proto)
    return json.loads(trans.getvalue())
Exemplo n.º 6
0
class SimpleJSONTest(AbstractTest):
    protocol_factory = TSimpleJSONProtocol.TSimpleJSONProtocolFactory()
Exemplo n.º 7
0
class TestForwardCompatibilityJSON(TestForwardCompatibilityAbstract,
                                   unittest.TestCase):
    protocol_factory = TSimpleJSONProtocol.TSimpleJSONProtocolFactory()
Exemplo n.º 8
0
def writeToJSON(obj):
    trans = TMemoryBuffer()
    proto = TSimpleJSONProtocol.TSimpleJSONProtocol(trans)
    obj.write(proto)
    return trans.getvalue()
Exemplo n.º 9
0
 def should_throw():
     j = '{"aString": "foo", "anotherString": nullcorrupt}'
     stuff = Stuff()
     Serializer.deserialize(
         TSimpleJSONProtocol.TSimpleJSONProtocolFactory(), j, stuff
     )
Exemplo n.º 10
0
def serialize_one(feature_parameters):
    trans = TMemoryBuffer()
    proto = TSimpleJSONProtocol.TSimpleJSONProtocol(trans)
    feature_parameters.write(proto)
    return trans.getvalue().decode("utf-8").replace("\n", "")
Exemplo n.º 11
0
    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(),