- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我创建了以下 Thrift 对象:
struct Student{
1: string id;
2: string firstName;
3: string lastName
}
现在我想从 JSON 中读取这个对象。根据这个post这是可能的
所以我写了下面的代码:
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 协议(protocol)示例:
[1,"MyService",2,1,{"1":{"rec":{"1":{"str":"Error: Process() failed"}}}}]
换句话说,Thrift 不打算解析任何 类型的 JSON。它支持一种非常具体的 JSON 格式作为可能的传输方式之一。
但是,根据您的 JSON 数据的来源,如果您能够在双方都使用 Thrift,Thrift 可能仍然可以帮助您。在这种情况下,编写一个 IDL 来描述数据结构,将其提供给 Thrift 编译器,并将生成的代码和库的必要部分与您的项目集成。
如果 JSON 的来源超出您的范围,或者如果由于某种原因无法更改 JSON 格式,您需要寻找其他方法。
格式和语义是不同的野兽
在某种程度上,整个问题可以与 XML 进行比较:有一种通用的 XML 语法,它告诉我们如何格式化事物,以便任何符合标准的 XML 处理器都可以读取它们。
但是如果我们从某人那里得到某个 XML 文件,那么了解 XML 的规则只是答案的一半。即使我们的 XML 解析器可以成功读取文件,因为它是格式良好的 XML,我们需要知道数据的语义才能真正利用该文件中的内容:它是 customer data record 吗? ?还是 SOAP envelope ?也许是 configuration file ?
这就是 DTD 或 XML 模式发挥作用的地方,它们的存在是为了描述 XML 数据的内容。如果不知道逻辑结构,您就会迷路,因为在 XML 中有无数种可能的表达方式。与 JSON 完全相同,除了 JSON schema descriptions。不太常用。
“所以你的意思是,我们只需要一种方法来告诉 Thrift JSON 是如何组织的?”
不,因为 Thrift 背后的目的和想法是拥有一个框架来尽可能高效地反序列化事物和/或实现 RPC 服务器和客户端。它不打算有一个通用的文件解析器。相反,Thrift 只读和说它自己的一组格式,即 plugged into the architecture as protocols :Thrift Binary、Thrift JSON、Thrift Compact 等等。
您可以做什么:除了我在回答的第一部分中所说的以外,您还可以考虑编写自己的自定义 Thrift 协议(protocol)实现以支持您选择的特定 JSON 格式。这并不难,值得一试。
关于java - Thrift - 从简单的 JSON 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20261519/
我正在使用 Ubuntu 10.04 LTS 机器,我在该机器上克隆了 Thrift 的 git 存储库并从源代码本地构建它。但是最新版本的 API 有一些变化,这导致应用程序的构建过程失败。 由于仅
尽管 Thrift 格式使用相对广泛(并且项目仍然存在),但我无法找到底层数据格式的规范。或格式,因为显然至少有两种格式;标准之一,紧凑的格式。 鉴于它的广泛使用,我希望它只是我可怜的 google-
每个参数(字段标识符)之前的数字的用途是什么?为什么从 5 跳到 16? struct Tweet { 1: required i32 userId; 2: required stri
我已经在 mac 中安装了 thrift。 从终端看起来 thrift 已成功安装但是当我尝试运行 thrift 命令时,我得到以下信息 Thrift: command not found 我从终端使
我在 c++ 中有一个结构,它存储这样的字节: struct RemoteData { /// some other fields here unsigned char* buf;
我想在 IDL 文件中定义几个结构。然后在服务中返回该结构类型的对象。为此,我必须导入该结构。如何在 IDL 中导入它们。 namespace java abc.xyz struct struct_{
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我使用thrift编译器(0.9.3)生成gen-go文件夹,同时编译器生成一个函数thrift.PrependError,在thrift 包(git.apache.org/thrift.git/li
假设我在包含两个字段的 Apache Thrift IDL 文件中定义了一个结构。例如: struct Thing { 1: optional string name, 2: optional
是否有可能使用 Thrift 来表示诸如 Java 类型 Map 之类的东西,其中 Map 中的值是已知的,例如是字符串还是整数? 换句话说,是否有任何方法来表示实际基类型仅在运行时已知但保证是一组预
我需要使用 Ruby 实现 Thrift API。 在生产环境中运行服务器有哪些选项? 最好的选择是什么? 目前我只是实例化一个 SimpleServer 并在其上调用 #serve。这显然是一个蹩脚
我正在查看 thirft 代码生成器的文档。它是这样开始的: Usage: thrift [options] file Options: -version Print the compile
所有教程和文档都建议每个 Thrift 服务器可以提供一项服务(1 个处理程序 1 个处理器 1 个服务器,全部在构造函数等中给出)。 从我的观点(优雅的设计)来看,如果许多或所有服务定义可以独立会更
我正在寻找 C++ 异步客户端和非阻塞 C++ 服务器实现。我在 apache 中看到一些邮件文件,但该事件是 2009 年末的。想知道最新的 thrift 是否支持它。我正在对 C++ 代码使用 c
我一直在研究使用 Apache Thrift,但发现文档有些缺乏。现在我有兴趣看到支持的目标语言以及每种语言支持的协议(protocol),是否有这样的矩阵? 最佳答案 试试这个: Thrift La
使用 C++ Apache Thrift 库,是否可以仅使用序列化/反序列化而不使用 RPC 服务? 据我了解 this页面,可以使用 Java 库来完成。但是,我找不到 C++ 库的类似类。 最佳答
Thrift 解析器有一个“注解”的定义: https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob;f=compiler/cpp/src
我的组织设置了一个 Spark Thrift 服务器,该服务器配置为使用 HTTP 上的 SSL。目的是使 Power BI 能够通过 Spark 安全地检索数据。然而,仅仅检索架构信息可能需要长达
我正在我的 Mac 上运行一对使用 Apache Thrift 进行通信的客户端和服务器程序。在我们的生产系统中,我们可能最终会遇到客户端使用TJSONProtocol,服务端使用TBinaryPro
来自 Thrift website 的示例 int multiply(1:int n1, 2:int n2); 为什么我们必须使用标签 1:和 2:在变量名之前,这个标签的目的是什么? 最佳答案 这些
我是一名优秀的程序员,十分优秀!