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

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


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/