开发者

use AMF instead of JSON on iPhone? (for web services)

开发者 https://www.devze.com 2023-01-01 05:47 出处:网络
Although iPhone support JSON natively, AMF is a binary protocol and it supposes to use much less bandwidth.Do you think using AMF is a good idea?

Although iPhone support JSON natively, AMF is a binary protocol and it supposes to use much less bandwidth. Do you think using AMF is a good idea?

Just found this AMF library in cocoa (Objective-C): http://github.com/nesium/cocoa-amf/

Here's the famous benchmark that shows AMF is开发者_StackOverflow smaller and faster than JSON + gzip in Flex: http://www.jamesward.com/census/


I don't think AMF would be significantly smaller than JSON. In fact, it can be slightly larger in many cases. Let me show this in an example:

AMF stores the string "asdf" in the following binary format:

0x12            /* type = string */
0x00 0x04       /* length */
'a' 's' 'd' 'f'
/* total: strlen(s)+3 bytes */

while JSON stores the string "asdf" in strlen(s) + 2 bytes if there are no quotes in the string.

AMF stores the JSON object {"key1":"asdf","key2":"foo"} in the following binary format:

0x03             /* type = object */
0x00 0x04        /* length of key1 */
'k' 'e' 'y' '1'
0x02             /* value type = string */
0x00 0x04        /* length of value1 */
'a' 's' 'd' 'f'
0x00 0x04        /* length of key2 */
'k' 'e' 'y' '2'
0x02             /* type of value2 */
0x00 0x03        /* length of value2 */
'f' 'o' 'o'
0x00 0x00 0x09   /* end of object */
/* total: 30 bytes, while the JSON string is 28 bytes */

The above examples were in AMF0, but I don't think AMF3 would be much different.

The only feature in AMF0 that can significantly reduce the bandwidth is that it contains a reference type: if you send the same large object twice, the second object will be only a back-reference to the first instance. But it is a rare case IMHO (and it works only for objects, not for strings).

So I would recommend JSON (if you really want to spare on bytes, you can compress it with zlib or anything): it's much simpler to read, there are much more implementations, and the specification is clear (while the Flash implementation is sometimes different from the specification - we all like Adobe ;))


Gym said :

The above examples were in AMF0, but I don't think AMF3 would be much different.

This is SO untrue. AMF3 can result in data as much as 5 to 8 times less than AMF / JSON. AMF3 achieves this by referencing every single item that has been used once. Not only strings. Any object, including keys, is referenced (with an offset) as soon as it has been used once.

On large datasets, it makes a huge difference.


You might take a look at Hessian or Google protocol buffers if you want to use a binary protocol. I know for a fact hessian provides very good performance on the iPhone.

http://code.google.com/p/protobuf/

http://hessian.caucho.com/


Actually it's a pretty good question and I have it too. I attended a session today at WWDC talking about client/server with iPhone. And they kept telling us binary plist were far more efficient than JSON and XML, especially when it comes to parsing time. But the problem is that I'm still tryin to find any server-side implementation of plist as a remoting protocol, whereas AMF has plenty of great implementations on the server-side: WebORB, ZendAMF, BlazeDS, etc. So integrating AMF on the server side is a breeze. Unfortunately, on the client side, the only option I found was Nesium's Cocoa AMF, but unfortunately it doesn't support channel set authentication and it misses a client-side stub generator. I would look into it but since this is no small task and I'm sure plenty of iPhone developers have already faced that issue, I want to make sure there really are no other options.

Up until now, I've been using Hessian with HessianKit, but it doesn't support authentication either and it's starting to be a limitation. Apple can say all they want about Flash, but at least they make it very easy to connect to a remote server.


You could also try plist, a native binary format. Any format, include AMF or even XML plist, could be reduced by zip. zlib is part of iPhone.

0

精彩评论

暂无评论...
验证码 换一张
取 消