From 8bd3fa3d06e14e3dda8bc01a9e6fbb0daed0a0e3 Mon Sep 17 00:00:00 2001 From: maralla Date: Fri, 21 Nov 2014 19:51:08 +0800 Subject: [PATCH] fix json protocol unicode error --- tests/test_json_protocol.py | 22 +++++++++++++++++++++- thriftpy/protocol/json.py | 7 ++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/tests/test_json_protocol.py b/tests/test_json_protocol.py index 6021561..120c50e 100644 --- a/tests/test_json_protocol.py +++ b/tests/test_json_protocol.py @@ -3,6 +3,7 @@ from thriftpy.protocol import TJSONProtocol from thriftpy.thrift import TPayload, TType from thriftpy.transport import TMemoryBuffer +from thriftpy._compat import u import thriftpy.protocol.json as proto @@ -28,7 +29,7 @@ def test_map_to_json(): spec = [TType.STRING, TType.DOUBLE] json = proto.map_to_json(obj, spec) - assert [{"key": "ratio", "value": "0.618"}] == json + assert [{"key": "ratio", "value": 0.618}] == json def test_list_to_obj(): @@ -92,3 +93,22 @@ def test_json_proto_api_read(): obj2 = p.read_struct(obj2) assert obj.id == 13 and obj.phones == ["5234", "12346456"] + + +def test_unicode_string(): + class Foo(TPayload): + thrift_spec = { + 1: (TType.STRING, "name") + } + default_spec = [("name", None)] + + trans = TMemoryBuffer() + p = TJSONProtocol(trans) + + foo = Foo(name=u('pão de açúcar')) + foo.write(p) + + foo2 = Foo() + foo2.read(p) + + assert foo == foo2 diff --git a/thriftpy/protocol/json.py b/thriftpy/protocol/json.py index fcc62cc..590488c 100644 --- a/thriftpy/protocol/json.py +++ b/thriftpy/protocol/json.py @@ -16,11 +16,8 @@ def json_value(ttype, val, spec=None): - if ttype in INTEGER: - return int(val) - - if ttype in FLOAT or ttype == TType.STRING: - return str(val) + if ttype in INTEGER or ttype in FLOAT or ttype == TType.STRING: + return val if ttype == TType.BOOL: return True if val else False