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

Commit

Permalink
Merge pull request #65 from maralla/json_unicode
Browse files Browse the repository at this point in the history
fix json protocol unicode error
  • Loading branch information
lxyu committed Nov 21, 2014
2 parents 12564da + 8bd3fa3 commit b6b2e4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
22 changes: 21 additions & 1 deletion tests/test_json_protocol.py
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
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

0 comments on commit b6b2e4c

Please sign in to comment.