Thrift无法从json反序列化为java对象
我从以下thrift对象生成了一个java对象:
struct Account { 1: required string accountType, 2: bool accountActive, }
我编写了一个java代码,试图将java对象序列化为json字符串,然后将json字符串反序列化为java对象。 我可以成功序列化但无法反序列化。
TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory()); TDeserializer deserializer = new TDeserializer(new TSimpleJSONProtocol.Factory()); Account a1 = new Account(); a1.setAccountType("P"); a1.setAccountActive(true); String json = serializer.toString(a1); System.out.println(json); Account a2 = new Account(); deserializer.deserialize(a2, json, "UTF-8"); System.out.println(a2); System.out.println(a2.getAccountType());
它不断抛出以下exception:
Exception in thread "main" org.apache.thrift.protocol.TProtocolException: Required field 'accountType' was not present! Struct: Account(accountType:null, accountActive:false)
任何人都可以帮我弄清问题是什么? 提前致谢!
SimpleJSONProtocol
从未打算过反序列化。 请改用TJSONProtocol
。
来自http://wiki.apache.org/thrift/ThriftUsageJava :
序列化为“简单”JSON
TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory()); String json = serializer.toString(work);
“简单”JSON协议产生适合AJAX或脚本语言的输出。 它不保留Thrift的字段标记,也不能被Thrift读回。
(强调我的)