I'm programming a Javascript application which saves "documents" in localStorage. As the app evolves, naturally there are changes in the document format.

I've tried searching but not found anything – probably (at least partly) because of search term ambiguities.

What is a good practice in managing document format versions/upgrades with local storage implementation approach?

1 Answer 1


One answer I see used in various languages is to version each stable format. Later, when you change to a new format you write a translator that reads the older format and converts it to the new when loaded.

The translator is not only responsible for repackaging the data into the new format but also providing sensible defaults for new properties or merging extinct properties into existing ones if their meaning isn't meant to be lost.

You can keep creating new versions as long as you keep writing matching translators, the conversions will then happen serially from the first match up to your most recent (eg. v1 -> v2 -> v3 -> v4).

When the new data is saved out it automatically is set to the latest version. In this way writing a utility to bulk-translate all old data (for major releases) is as simple as a loop:

while files
    read file
    write file
end while
  • Sounds sensible, and was along the lines I were thinking. I guess what I wasn't quite sure about was how to deal with upgrading the format – but serial conversion through all versions sounds like the easiest way to go. Sep 23, 2012 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.