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

UnicodeEncodeError using TJSONProtocol with unicode string (python2.7) #64

Closed
gustavofonseca opened this issue Nov 20, 2014 · 1 comment

Comments

@gustavofonseca
Copy link

Hi all! This is the traceback:

Traceback (most recent call last):
  File "foo.py", line 14, in <module>
    foo.write(proto.TJSONProtocol(buff))
  File "/Users/gustavofonseca/prj/envs/thrift/lib/python2.7/site-packages/thriftpy/thrift.py", line 93, in write
    oprot.write_struct(self)
  File "/Users/gustavofonseca/prj/envs/thrift/lib/python2.7/site-packages/thriftpy/protocol/json.py", line 206, in write_struct
    "payload": struct_to_json(obj)
  File "/Users/gustavofonseca/prj/envs/thrift/lib/python2.7/site-packages/thriftpy/protocol/json.py", line 129, in struct_to_json
    outobj[field_name] = json_value(field_type, v, field_type_spec)
  File "/Users/gustavofonseca/prj/envs/thrift/lib/python2.7/site-packages/thriftpy/protocol/json.py", line 23, in json_value
    return str(val)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe3' in position 1: ordinal not in range(128)

foo.thrift:

struct Foo {
    1: optional string name;
}

foo.py:

# coding: utf-8
import io

import thriftpy
import thriftpy.protocol.json as proto

thrift_spec = thriftpy.load('foo.thrift', module_name='foo_thrift')

foo = thrift_spec.Foo(name=u'pão de açúcar')
#print proto.struct_to_json(foo) 


buff = io.BytesIO()
foo.write(proto.TJSONProtocol(buff))

print buff.getvalue()
@lxyu
Copy link
Contributor

lxyu commented Nov 21, 2014

should be fixed in #65

@lxyu lxyu closed this as completed Nov 21, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants