YAML/JSON/XML:在 iPhone 上选择哪一种通过 RESTFul 协议进行通信?
我正在编写一个与外部服务器通信的简单应用程序。 服务器目前支持yaml、xml和json。
- iPhone 上哪种编码速度最快?
- 哪个有更好的支持?
- 您建议使用哪些库?
I'm writing a simple application that communicates with an external server.
The server currently supports yaml, xml and json.
- Which encoding is fastest on IPhone?
- Which has better support?
- What libraries do you suggest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我参与的一个项目将运行 J2ME 的摩托罗拉手机与网络中的语音服务器连接起来。我们发现总带宽值得优化(这是在 2004 年的 2.5G 网络上)。因此,我建议您测量每种序列化格式需要多少字节,并选择较小的序列化格式(JSON 或 YAML)。您甚至可以考虑使用二进制协议,例如 Hessian 或 Google 的 Protocol Buffers。
我们还发现,最大限度地减少消息数量可以减少延迟,因此请寻找以更少、更大的块将数据发送到 iPhone 的方法,在手机上使用 HTTP 缓存,使用 HTTP 实体标签和 If-Modified 标头,等等。由于您使用的是 REST,因此您可以很好地利用 HTTP 的所有这些优秀功能。
当然,这很容易成为过早优化,因此尽可能以最简单的方式进行编码并首先进行测量。
I worked on a project that connected Motorola handsets running J2ME with a speech server in the network. We found that total bandwidth was worth optimizing (this was on a 2.5G network in 2004). So I'd suggest you measure how many bytes each serialization format takes and go with the smaller one (which will be JSON or YAML). You might even consider using a binary protocol like Hessian or Google's Protocol Buffers.
We also discovered that minimizing the number of messages decreased latency, so be on the lookout for ways to send data to the iPhone in fewer, larger chunks, use an HTTP cache on your phone, use HTTP entity tags and If-Modified headers, and so on. Since you're using REST, you're well positioned to leverage all these nice features of HTTP.
Of course this can all very easily be premature optimization, so code it up the easiest way possible and measure first.
我为 iPhone OS 3.0 创建了一个开源应用程序,展示了如何使用 REST 和 REST。 iPhone 应用程序中的 SOAP 服务,使用 XML(使用 8 个不同的 iPhone 库)、SOAP、JSON(使用 SBJSON 和 TouchJSON)、YAML、Protocol Buffers(Google 序列化格式),甚至来自 PHP 示例应用程序的 CSV(包含在项目中)。
http://github.com/akosma/iPhoneWebServicesClient
该项目足够模块化,可以支持许多其他格式未来的图书馆。
SlideShare 中的以下演示显示了我在性能、易于实施和有效负载特征方面的发现:
http://www.slideshare.net/akosma/web-services-3439269
基本上,在我的测试中,我发现 Binary Plists + REST + JSON 和 XML + TBXML 库是“最佳”选择(含义:易于实施+反序列化速度+最低有效负载大小)。
在 Github 项目中,有一个“结果”文件夹,其中有一个总结调查结果的 Excel 工作表(以及所有原始数据)。您也可以通过 3G 或 WiFi 自行启动测试,然后将结果邮寄给自己进行比较和研究。
希望有帮助!
I've created an open source application for iPhone OS 3.0 that shows how to use REST & SOAP services in iPhone application, using XML (using 8 different iPhone libraries), SOAP, JSON (using SBJSON and TouchJSON), YAML, Protocol Buffers (Google serialization format) and even CSV from a PHP sample app (included in the project).
http://github.com/akosma/iPhoneWebServicesClient
The project is modular enough to support many other formats and libraries in the future.
The following presentation in SlideShare shows my findings in terms of performance, ease of implementation and payload characteristics:
http://www.slideshare.net/akosma/web-services-3439269
Basically I've found, in my tests, that Binary Plists + REST + JSON and XML + the TBXML library are the "best" options (meaning: ease of implementation + speed of deserialization + lowest payload size).
In the Github project there's a "results" folder, with an Excel sheet summarizing the findings (and with all the raw data, too). You can launch the tests yourself, too, in 3G or wifi, and then have the results mailed to yourself for comparison and study.
Hope it helps!
使用 json-framework,使您的类与 JSON 进行互操作非常简单。
Using json-framework, making your classes interoperate with JSON is ridiculously easy.
好吧,如果您想使用 XML,请使用 plist,因为 iPhone 本身就支持它。
JSON 不是,但有一些很好的 c 库可用。我的应用程序支持 JSON 和 XML。
与 XML 相同 - 有很多 - 只需搜索一下即可。
它还取决于您的服务器支持的媒体类型 - 顺便说一句,REST 并不是真正的协议。
Well, if you want to use XML, use a plist, since its supported natively on the iphone.
JSON is not, but there are some good c libs available. I support JSON and XML in my app.
Same with XML - there's a bunch - just search around.
It also depends what media-types your server supports - btw, REST isn't really a protocol.
我有一些基准比较此处提供的不同序列化器的性能和有效负载大小:
http://www.servicestack.net/benchmarks/NorthwindDatabaseRowsSerialization.1000000-times.2010-02-06.html
基本上,如果您使用 .NET Web 服务进行开发,那么您将除非您使用另一个序列化器,否则要在有效负载大小与性能之间做出妥协。
Marc 的 protobuf-net 显示了最小且最快的实现:
尽管是二进制协议,但调试起来可能会比较困难。
如果您使用 MonoTouch(即 iPhone 的 C#/Mono)进行开发并希望使用基于文本的格式,那么您可能会对我的 类似 Javascript 的类型序列化器,已针对大小和速度进行了优化,它还比可用的 XML 和 JSON 选项显示出适度的增益,即:速度
以下是 MonoTouch 教程,展示了如何从 iPhone 调用 Web 服务:
http://www.servicestack.net/monotouch/remote-info/
I have some benchmarks comparing the performance and payload sizes of the different serializers available here:
http://www.servicestack.net/benchmarks/NorthwindDatabaseRowsSerialization.1000000-times.2010-02-06.html
Basically, if you're developing with .NET web services than you are going to be making a compromise on payload size vs performance, unless you go with another serializer.
Marc's protobuf-net shows the smallest and fastest implementation:
Although being a binary protocol, it may be harder to debug.
If your developing with MonoTouch (i.e. C#/Mono for the iPhone) and want to use a text-based format, than you may be interested in my Javascript-like type serializer that has been optimized for size and speed, it also shows modest gains over the available XML and JSON options, namely:
Here's a MonoTouch tutorial showing how to call web services from an iPhone:
http://www.servicestack.net/monotouch/remote-info/
如果您可以在 iPhone 项目中使用 c++ 库,那么请查看 yaml-cpp:
http://code.yaml-cpp google.com/p/yaml-cpp/
IF you are ok with a c++ library in your iPhone projects, then please have a look at yaml-cpp:
http://code.google.com/p/yaml-cpp/
这实际上取决于您的需求以及您要在服务器和应用程序之间传输哪种数据。如果您想执行查询来选择某些数据,您应该选择 XML,因为 XQuery 语言支持。据我所知,不支持 JSON。关于 YAML 我无话可说。
It really depends on your need and what kind of data you are going to transfer between server and application. If you want to execute queries to select some piece of data you should choose XML because of XQuery language support. JSON is not supported, as I know. I can nothing to say about YAML.
我还没有看到任何 YAML 库(尽管这并不意味着没有)。我知道 TouchJSON 工作得很好,而且至少还有其他一种。
JSON 比 XML 或 PLIST feed 占用的空间更少,但需要提前多思考才能获得正确的结构。
pList 的一个优点是您可以将日期作为对象返回而无需解析。如果您在 JSON 中传递日期,请决定一种格式并在各处使用相同的格式。 NSDateFormatter 不是线程安全的,因此如果您想使用单个日期格式化程序来节省资源,则必须为每个线程创建一个实例。
I have not seen any YAML libraries (though it does not mean there are none). I know TouchJSON works pretty well, and there is at least one other.
JSON takes less space than an XML or PLIST feed, BUT needs a little more thinking ahead of time to get the structure right.
One nice aspect of pLists is that you get back dates as objects without parsing. If you pass dates in a JSON decide on a format and use the same format everywhere. NSDateFormatter is not thread safe so you have to make an instance per thread, if you want to use a single date formatter to save resources.
Objective-C 中支持所有三种格式。只需谷歌搜索即可找到他们。我建议编写一个支持所有这三个的序列化器。只需让 URL 资源请求以 .xml|.json|.yaml 结尾,并让服务器根据该扩展名决定要序列化的内容。然后您实际上不必决定可以切换到您想要的任何内容。在大多数服务器端实现中,使序列化器可插入非常容易。
there is support for all three formats in Objective-C. Just google for them. I would suggest writing a serializer that supports all three. Just make your URL resource request end with .xml|.json|.yaml and have the server decided what to serialize to based on that extension. Then you don't actually have to decide you can switch to whatever you want. Making the serializer pluggable is really easy in most server side implementations.