Android和AppEngine Web服务:Json ... RPC,REST ...协议缓冲区?

时间:2022-01-30 19:34:46

I'm working on a web service that will be accessed from an Android app. After doing some research on what's the best technology, I'm left somewhat confused and dazed by the options.

我正在开发一个可以从Android应用程序访问的Web服务。在对什么是最好的技术进行一些研究之后,我对选项感到有些困惑和茫然。

Obviously on the Android end I want it to be as lightweight as possible. I also would prefer to share the common code since both are java, although that's less important. My primary concern is having it be efficient, and after that, simple and elegant code.

显然在Android端我希望它尽可能轻量级。我也更愿意分享公共代码,因为它们都是java,尽管这不太重要。我主要担心的是它是否有效,之后是简单而优雅的代码。

I've tried gson on the Android end, and it works nicely. But then I read about protocol buffers, and that seems even more efficient, I'm not sure if it's a significant difference. Also I'm not sure whether to go for RPC or REST.

我在Android端尝试过gson,效果很好。但后来我读到了协议缓冲区,这似乎更有效,我不确定它是否是一个显着的差异。此外,我不确定是否要使用RPC或REST。

1 个解决方案

#1


5  

On the efficiency front, Protocol Buffers will probably be more efficient than any JSON implementation, thought not necessarily by as much as you think. GSON is not particularly fast, but the Jackson library can almost compete with most binary serializers (Jackson is 2-4x faster than GSON in most situations and 10-20x faster on UTF-8 because it has special code for UTF-8).

在效率方面,Protocol Buffers可能比任何JSON实现更有效,不一定想象的那么多。 GSON并不是特别快,但杰克逊库几乎可以与大多数二进制序列化器竞争(在大多数情况下,Jackson比GSON快2-4倍,在UTF-8上快10-20倍,因为它具有UTF-8的特殊代码)。

But I'd still take Protocol Buffers over any JSON library because of the programming model. With most JSON libraries, your have to check the structure of a message manually. With Protocol Buffers, you specify message structures declaratively and the library will take care of the structural validation for you (though there will still be things that you need to validate manually).

但由于编程模型,我仍然会在任何JSON库上使用Protocol Buffers。对于大多数JSON库,您必须手动检查消息的结构。使用Protocol Buffers,您可以声明性地指定消息结构,并且库将为您处理结构验证(尽管仍然需要手动验证)。

Other libraries like Protocol Buffers: Apache Avro, Apache Thrift.

其他库如Protocol Buffers:Apache Avro,Apache Thrift。

The Protostuff library uses the Protocol Buffers data model (so you get structural validation for free) but support serializing to JSON and YAML in addition to other formats. This can be useful if you want your service to be consumed by Javascript code, where JSON is often the easiest thing to deal with.

Protostuff库使用Protocol Buffers数据模型(因此您可以免费获得结构验证),但除了其他格式外,还支持序列化为JSON和YAML。如果您希望Javascript代码使用您的服务,这可能很有用,其中JSON通常是最容易处理的事情。

#1


5  

On the efficiency front, Protocol Buffers will probably be more efficient than any JSON implementation, thought not necessarily by as much as you think. GSON is not particularly fast, but the Jackson library can almost compete with most binary serializers (Jackson is 2-4x faster than GSON in most situations and 10-20x faster on UTF-8 because it has special code for UTF-8).

在效率方面,Protocol Buffers可能比任何JSON实现更有效,不一定想象的那么多。 GSON并不是特别快,但杰克逊库几乎可以与大多数二进制序列化器竞争(在大多数情况下,Jackson比GSON快2-4倍,在UTF-8上快10-20倍,因为它具有UTF-8的特殊代码)。

But I'd still take Protocol Buffers over any JSON library because of the programming model. With most JSON libraries, your have to check the structure of a message manually. With Protocol Buffers, you specify message structures declaratively and the library will take care of the structural validation for you (though there will still be things that you need to validate manually).

但由于编程模型,我仍然会在任何JSON库上使用Protocol Buffers。对于大多数JSON库,您必须手动检查消息的结构。使用Protocol Buffers,您可以声明性地指定消息结构,并且库将为您处理结构验证(尽管仍然需要手动验证)。

Other libraries like Protocol Buffers: Apache Avro, Apache Thrift.

其他库如Protocol Buffers:Apache Avro,Apache Thrift。

The Protostuff library uses the Protocol Buffers data model (so you get structural validation for free) but support serializing to JSON and YAML in addition to other formats. This can be useful if you want your service to be consumed by Javascript code, where JSON is often the easiest thing to deal with.

Protostuff库使用Protocol Buffers数据模型(因此您可以免费获得结构验证),但除了其他格式外,还支持序列化为JSON和YAML。如果您希望Javascript代码使用您的服务,这可能很有用,其中JSON通常是最容易处理的事情。