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

5

5

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 smaller and faster than JSON + gzip in Flex: http://www.jamesward.com/census/

Winslow answered 29/5, 2010 at 1:47 Comment(2)
It certainly depends on the data you are transferring. I need to create a mini-Census app that does smaller data sets. :)Berceuse
Make sure you read @user562816 answer, AMF3 should be a lot smaller.Exequies
B
5

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.

Brazenfaced answered 4/1, 2011 at 16:36 Comment(1)
very important difference here. AM3 should be marked as the correct answer here... hmm wonder if I can edit answer?Exequies
G
6

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 ;))

Gebler answered 29/5, 2010 at 9:23 Comment(4)
but isn't transporting binary over HTTP more efficient somehow than text?Winslow
Not at all! Maybe what you're referring to is that the textual data of a HTML form is URL encoded by the browser (it escapes space character to %20, for example), which increases length. But it is not needed to send data this way over HTTP. And anyway, textual characters are the subset of "binary characters" (numbers between 0-255), so if you send a text as binary data, it cannot cost more than a "real" binary data! (actually, only the contrary can happen: some protocols require that the data is sent in ASCII characters, so the binary data must be unpacked to the 7-bit range...)Gebler
Just a point, as this answer might be misleading - you've probably shown on of very few cases JSON will be more efficient than AMF,namely sending strings (and it's only sometimes better as AMF3 uses references to identical strings). On big sets of objects, AMF3 sends the class structure at the beggining, and than just the data in binary format, while JSON resends the field names again, and again as strings. Even in simple, common cases like sending numbers, AMF wins by far, as for example "1234567890" would be written as a string in JSON but would be 80 bits instead of 32bits for an AMF int.Toadeater
Robert, thanks for the correction. I only knew AMF0 (on the byte level) when I wrote this answer.Gebler
B
5

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.

Brazenfaced answered 4/1, 2011 at 16:36 Comment(1)
very important difference here. AM3 should be marked as the correct answer here... hmm wonder if I can edit answer?Exequies
C
2

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/

Chalmer answered 29/5, 2010 at 18:47 Comment(0)
I
1

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.

Inflight answered 10/6, 2010 at 1:42 Comment(1)
no Java implementation of plist yet? maybe it's time to start an open source project. :)Winslow
K
0

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.

Kattie answered 29/5, 2010 at 1:54 Comment(2)
thx, but plist doesn't work well with most web servers though.Winslow
You should add enough details to your question to make your requirements clear. Vague questions get vague answers. Initially, your only reason for using AMF was to reduce bandwidth, in which case plist with zip is a better choice.Kattie

© 2022 - 2024 — McMap. All rights reserved.