在 Android 应用程序中使用 Protocol Buffers 而不是 JSON 来传输数据是个好主意吗?
嘿,有人建议我使用 Protocol Buffers,在 Android 应用程序中使用 Protocol Buffers 而不是 JSON 来传输数据是个好主意吗?
我正在考虑性能、库大小以及将 JSON 格式转换为 PB 是否容易。
Hey, some guy suggest me to use Protocol Buffers, is this a good idea to use Protocol Buffers instead of JSON to transfer data in android app?
I am considering performance, library size and if is this easy to transform JSON format to PB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
理论上,协议缓冲区的重量应该要轻一些。只有通过测试您的具体情况才能告诉您是否值得。 Protocol Buffers 和 JSON 都很容易实现,我建议尝试这两种方法,看看你更喜欢哪一种。
我相信 Protocol Buffers 还构建了一些确保向后兼容反序列化的支持。您必须使用 JSON 自行完成此操作。
Theoretically, Protocol Buffers should be quite a bit lighter weight. Only by testing your specific situation will tell you if it's worth it or not though. Protocol Buffers and JSON are so easy to implement, that I'd suggest trying it both ways, and see which one you prefer.
I believe that Protocol Buffers also builds in some support for ensuring backward compatible deserialization. You have to do that on your own with JSON.
我同意嘎嘎努的观点。我只是想补充我的两分钱。
如果您已经在使用 JSON,请问问自己:
性能是现在的问题还是未来可预见的问题?
如果是这样,那么就大胆地考虑PB。我见过的所有基准测试都显示出从稍微好到好得多的性能,具体取决于确切的标准和上下文。
对于大小:我认为 PB 应该小于等效的 JSON
易于转换:PB 需要使用一些您可能必须学习使用的工具以及一些(相对较小的)库来添加到您的应用程序中,但是 PB 和 JSON 都是意味着可用。转变会有多困难;这确实取决于你自己的知识。我认为 PB 通常需要更少的实际代码行才能使其正常工作。
如果性能目前不是问题,我建议使用 PB 制作一个测试应用程序,看看它是否是您将来想要使用的东西。学习使用它当然不会有什么坏处。
I agree with squaknull. I just wanted to add my two cents.
If you are already using JSON, ask yourself:
Is performance a problem now or a foreseeable problem in the future?
If so, than defiantly consider PB. All benchmarks I have seen show anywhere from slightly better to much better performance depending on the exact criteria and context.
for size: I think PB should be smaller than equivalent JSON
ease of conversion: PB requires use of some tools that you might have to learn to use and some (relatively small) libraries to add to your app, but both PB and JSON are meant to be usable. How hard it will be to convert; that really depends on your own Knowledge. I think PB in general requires a bit less actual lines of code to get it working though.
if performance is not currently a problem, I would suggest making a test app with PB to see if its something you want to use in the future. It certainly won't hurt to learn to use it.