Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

fix json protocol unicode error #65

Merged
merged 1 commit into from
Nov 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion tests/test_json_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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():
Expand Down Expand Up @@ -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
7 changes: 2 additions & 5 deletions thriftpy/protocol/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down