iPhone & JSON: Encode dates in JSON objects for use in iPhone applications
Taking into consideration performance and readability, how should dates be encoded in JSON for use in iPhone applications? The ideal encoding would wor开发者_如何学Pythonk with NSDate easily, but also be future proofed for use in a web based back end (i.e. Not using an API exclusive to the iPhone, something crossplatform). Here are some encodings I have been considering:
# UNIX Epoc based integer encoding
{"Date":123456789}
# ISO 8601 String format
{"Date":"2011-03-25T20:00Z"}
Will the parsing of the ISO 8601 string impact performance too much when thousands of dates are processed? Is the performance hit too low to matter when compared to Epoc? Is there an encoding missing that accomplishes the mentioned requirements?
I've always used Unix epoch based encoding when I have control of the server stack. The pros outweigh the cons for my use cases
epoch pros
- faster (may be an issue if dealing with large volume of dates.)
- needs less code
- supported by just about every date/time library/language
epoch cons
- not human readable
- bogus input (as long as it's a number) will likely still give you a valid date
ISO pros
- very readable
- bogus input (malformed) will likely not give you a valid date
ISO cons
- takes more bytes
- need to write/find your own Cocoa implementation (let me know if you can't find any, I have a category lying around somewhere)
ISO parsing is not hard, but you must check yourself on your own implementation. Cocoa doesn't have the fastest string parser. I have to believe that it will be noticeably slower (likely more than 2x).
精彩评论