我从以下节俭对象生成了一个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 in thread "main" org.apache.thrift.protocol.TProtocolException: Required field 'accountType' was not present! Struct: Account(accountType:null, accountActive:false)有人能帮我找出什么问题吗?提前感谢!
发布于 2014-06-20 10:11:29
SimpleJSONProtocol从来没有打算被反序列化。使用TJSONProtocol代替。
来自http://wiki.apache.org/thrift/ThriftUsageJava
序列化为“简单”JSON TSerializer序列化程序=新TSerializer(新TSimpleJSONProtocol.Factory());字符串json = serializer.toString(work); 这个“简单”的JSON协议产生了适合于AJAX或脚本语言的输出。它不保存Thrift的字段标记,并且不能被读回。
(强调地雷)
https://stackoverflow.com/questions/24319325
复制相似问题