节俭 - 从简单的JSON转换 [英] Thrift - converting from simple JSON

查看:194
本文介绍了节俭 - 从简单的JSON转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下Thrift对象:

I created the following Thrift Object:

struct Student{
        1: string id;
        2: string firstName;
        3: string lastName
}

现在我想读这个对象来自JSON。根据这个发布,这是可能的

Now I would like to read this object from JSON. According to this post this is possible

所以我编写了以下代码:

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);

我得到的是以下异常:

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)

我错过了什么吗?

推荐答案

你错过了Thrift的JSON与你的不同的事实。不写入字段名称,而是写入(和预期)指定的字段ID号。以下是Thrift的JSON协议示例:

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:

[1,"MyService",2,1,{"1":{"rec":{"1":{"str":"Error: Process() failed"}}}}]

换句话说,Thrift不打算解析任何类型的JSON。它支持非常特定的JSON格式作为可能的传输之一。

In other words, Thrift is not intended to parse any kind of JSON. It supports a very specific JSON format as one of the possible transports.

但是,根据您的JSON数据的来源,Thrift可能仍然可以帮助您,如果你能够在双方使用它。在这种情况下,编写一个IDL来描述数据结构,将其提供给Thrift编译器,并将生成的代码和库的必要部分与您的项目集成。

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的来源超出您的范围,或者由于某种原因无法更改JSON格式,您需要找到另一种方式。

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.

格式和语义是不同的野兽

在某种程度上,整个问题可以与XML:有一种通用的XML语法,告诉我们如何填充事物,因此任何符合标准的XML处理器都可以读取它们。

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解析器可以成功读取文件,因为它是格式良好的XML,我们需要知道数据的语义才能真正利用该文件中的内容:它是客户数据记录?或者它是 SOAP信封?也许配置文件

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?

这就是DTD或XML Schema发挥作用的地方,它们用于描述XML数据的内容。在不知道您丢失的逻辑结构的情况下,因为有无数种可能的方式来表达XML中的内容。 JSON完全相同,只是 JSON架构描述不太常用。

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 descriptions are less commonly used.

你的意思是,我们只需要告诉Thrift如何组织JSON?

"So you mean, we need just a way to tell Thrift how the JSON is organized?"

不,因为Thrift背后的目的和理念是拥有一个框架来尽可能高效地解决/序列化事物和/或实现RPC服务器和客户端。它不打算具有通用文件解析器。相反,Thrift只读取和说出自己的一组格式,这些格式是作为协议插入体系结构:Thrift Binary, Thrift JSON,Thrift Compact等等。

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协议实现,以支持您选择的特定JSON格式。这并不难,值得一试。

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.

这篇关于节俭 - 从简单的JSON转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆