Java Thrift - 从简单的 JSON 转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20261519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thrift - converting from simple JSON
提问by danny.lesnik
I created the following Thrift Object:
我创建了以下 Thrift 对象:
struct Student{
1: string id;
2: string firstName;
3: string lastName
}
Now I would like to read this object from JSON. According to this postthis is possible
现在我想从 JSON 读取这个对象。根据这篇文章,这是可能的
So I wrote the following code:
所以我写了下面的代码:
String json = "{\"id\":\"aaa\",\"firstName\":\"Danny\",\"lastName\":\"Lesnik\"}";
StudentThriftObject s = new StudentThriftObject();
byte[] jsonAsByte = json.getBytes("UTF-8");
TMemoryBuffer memBuffer = new TMemoryBuffer(jsonAsByte.length);
memBuffer.write(jsonAsByte);
TProtocol proto = new TJSONProtocol(memBuffer);
s.read(proto);
What I'm getting is the following exception:
我得到的是以下异常:
Exception in thread "main" org.apache.thrift.protocol.TProtocolException: Unexpected character:i
at org.apache.thrift.protocol.TJSONProtocol.readJSONSyntaxChar(TJSONProtocol.java:322)
at org.apache.thrift.protocol.TJSONProtocol.readJSONInteger(TJSONProtocol.java:698)
at org.apache.thrift.protocol.TJSONProtocol.readFieldBegin(TJSONProtocol.java:837)
at com.vanilla.thrift.example.entities.StudentThriftObject$StudentThriftObjectStandardScheme.read(StudentThriftObject.java:486)
at com.vanilla.thrift.example.entities.StudentThriftObject$StudentThriftObjectStandardScheme.read(StudentThriftObject.java:479)
at com.vanilla.thrift.example.entities.StudentThriftObject.read(StudentThriftObject.java:413)
at com.vanilla.thrift.controller.Main.main(Main.java:24)
Am I missing something?
我错过了什么吗?
采纳答案by JensG
You are missing the fact, that Thrift's JSON is different from yours. The field names are not written, instead the assigned field ID numbers are written (and expected). Here's an example for Thrift's JSON protocol:
您忽略了一个事实,即 Thrift 的 JSON 与您的不同。不写入字段名称,而是写入(和预期)分配的字段 ID 号。这是 Thrift 的 JSON 协议的示例:
[1,"MyService",2,1,{"1":{"rec":{"1":{"str":"Error: Process() failed"}}}}]
In other words, Thrift is not intended to parse anykind of JSON. It supports a very specific JSON format as one of the possible transports.
换句话说,Thrift 不打算解析任何类型的 JSON。它支持一种非常特定的 JSON 格式作为可能的传输之一。
However, depending on what the origin of your JSON data is, Thrift can possibly still help you out, if you are able to use it on both sides. In that case, write an IDL to describe the data structures, feed it to the Thrift compiler and integrate both the generated code and the neccessary parts of the library with your projects.
但是,根据您的 JSON 数据的来源,Thrift 可能仍然可以帮助您,如果您能够在双方使用它。在这种情况下,编写一个 IDL 来描述数据结构,将其提供给 Thrift 编译器并将生成的代码和库的必要部分与您的项目集成。
If the origin of the JSON lies outside of your reach, or if the JSON format cannot be changed for some reason, you need to find another way.
如果 JSON 的来源在您的范围之外,或者由于某种原因无法更改 JSON 格式,您需要寻找其他方法。
Format and semantics are different beasts
格式和语义是不同的野兽
To some extent, the whole issue can be compared with XML: There is one general XML syntax, which tells us how we have to fomat things so any standard conformant XML processor can read them.
在某种程度上,整个问题可以与 XML 进行比较:有一种通用的 XML 语法,它告诉我们必须如何对事物进行格式化,以便任何符合标准的 XML 处理器都可以读取它们。
But knowing the rules of XML is only half the answer, if we get a certain XML file from someone. Even if our XML parser can read the file successfully, because it is well-formed XML, we need to know the semantics of the data to really make use of what's within that file: Is it a customer data record? Or is it a SOAP envelope? Maybe a configuration file?
但是,如果我们从某人那里获得某个 XML 文件,那么了解 XML 规则只是答案的一半。即使我们的 XML 解析器可以成功读取文件,因为它是格式良好的 XML,我们需要知道数据的语义才能真正利用该文件中的内容:它是客户数据记录吗?或者它是一个SOAP 信封?也许是配置文件?
That is where DTDs or XML Schema come into play, they exist to describe the contents of the XML data. Without knowing the logical structure you are lost, because there are myriads of possible ways to express things in XML. And exactly the same is true with JSON, except that JSON schema descriptionsare less commonly used.
这就是 DTD 或 XML 模式发挥作用的地方,它们的存在是为了描述 XML 数据的内容。如果不知道逻辑结构,您就会迷失方向,因为在 XML 中有无数种可能的方式来表达事物。与 JSON 完全相同,只是JSON 模式描述不太常用。
"So you mean, we need just a way to tell Thrift how the JSON is organized?"
“所以你的意思是,我们只需要一种方法来告诉 Thrift JSON 是如何组织的?”
No, because the purpose and idea behind Thrift is to have a framework to de/serialize things and/or implement RPC servers and clients as efficiently as possible. It is not intended to have a general purpose file parser. Instead, Thrift reads and speaks only its own set of formats, which are plugged into the architecture as protocols: Thrift Binary, Thrift JSON, Thrift Compact, and a few more.
不,因为 Thrift 背后的目的和想法是拥有一个框架来尽可能高效地反/序列化事物和/或实现 RPC 服务器和客户端。它不打算具有通用文件解析器。相反,Thrift 只读取和说出它自己的一组格式,这些格式作为协议插入到架构中:Thrift Binary、Thrift JSON、Thrift Compact 等等。
What you could do:In addition to what I said at in the first section of my answer, you may consider writing your own custom Thrift protocol implementation to support your particular JSON format of choice. It is not that hard, and worth a try.
您可以做什么:除了我在回答的第一部分所说的内容之外,您还可以考虑编写自己的自定义 Thrift 协议实现来支持您选择的特定 JSON 格式。这并不难,值得一试。