fix duplicate objects after restoring data from localStorage

Attempt to fix #2134 (and possibly also #2091 and #1249).

This is not yet 100% finished, but can already be reviewed. Todos:

* [ ] fix failing `restores from v2 JSON (modification)` test
* [ ] add tests for `restores from v3 JSON`