![]() There are other variations of the timezone encoding in the ISO 8601 spec, but the –10:00 format is the only TZ format that current JSON parsers support. ISO 8601 also supports time zones by replacing the Z with + or – value for the timezone offset: The date is represented in a standard and sortable format that represents a UTC time (indicated by the Z). You can serialize to this format, but there’s no direct deserialization back to a date from it. Dates are encoded as ISO 8601 strings and then treated just like a regular string when the JSON is serialized and deserialized. There’s no ‘official’ standard for what the date format should look like, although it’s been more or less agreed upon that the JSON Date encoding format should be ISO 8601 as that’s what all the major browser native JSON parsers use. So in order to represent dates in JavaScript, JSON uses a specific string format – ISO 8601 – to encode dates as string. Which seems like a rather major omission given that dates are pretty important in computing and business environments. You can represent strings, numbers, Booleans and even objects, arrays and RegEx expressions with language specific literals, but there’s no equivalent literal representation for dates. ![]() The problem with dates in JSON – and really JavaScript in general – is that JavaScript doesn’t have a date literal. JSON Dates are not dates – they are Strings I find this functionality indispensable and use as part of my own libraries in just about every client-side application that uses AJAX. The code and small support library discussed in this post is up on GitHub in my json.date-extensions library, so you can download and play around with this yourself. In this post I’ll discuss the problem and a few workarounds as well as small JSON extension library that you can use to globally parse JSON with proper date conversion with minimal fuss. I’ve talked about this topic a few times in the past, but I thought I’d revisit because it’s one that comes up quite frequently, and because in the interceding years I’ve started using a more flexible solution than what I previously discussed. Or more precisely – it serializes dates just fine, it just doesn’t deserialize them. But yet there’s at least one very annoying problem with JSON: It doesn’t serialize dates. It’s used for transport serialization for just about anything REST related these days, for configuration management, for storage and data representation in many database formats (NoSql). For Web based applications one could say it’s become the serialization format. JSON has become a rather prevalent serialization format in recent years.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |