diff --git a/chrome/content/zotero/xpcom/data/collection.js b/chrome/content/zotero/xpcom/data/collection.js index 9e9fc1862..4dd6ba465 100644 --- a/chrome/content/zotero/xpcom/data/collection.js +++ b/chrome/content/zotero/xpcom/data/collection.js @@ -692,6 +692,20 @@ Zotero.Collection.prototype.serialize = function(nested) { } +Zotero.Collection.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) { + yield this.loadAllData(); + + if (!json.name) { + throw new Error("'name' property not provided for collection"); + } + this.name = json.name; + this.parentKey = json.parentCollection ? json.parentCollection : false; + + // TODO + //this.setRelations(json.relations); +}); + + Zotero.Collection.prototype.toJSON = function (options, patch) { var obj = {}; if (options && options.includeKey) { diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 0881f4f73..b91aa53e1 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -3988,26 +3988,44 @@ Zotero.Item.prototype.isCollection = function() { } -Zotero.Item.prototype.fromJSON = function (json) { - if (json.itemKey) this.key = json.itemKey; - if (json.itemType) this.setType(Zotero.ItemTypes.getID(json.itemType)); +Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) { + yield this.loadAllData(); - var changedFields = {}; + this.setType(Zotero.ItemTypes.getID(json.itemType)); + + var isValidForType = {}; + var setFields = {}; // Primary data for (var field in json) { let val = json[field]; switch (field) { - case 'itemKey': + case 'key': + case 'version': case 'itemType': case 'creators': - case 'deleted': + case 'note': + // Use? + case 'md5': + case 'mtime': break; case 'dateAdded': case 'dateModified': - this['_'+field] = val; + this['_' + field] = Zotero.Date.dateToSQL(Zotero.Date.isoToDate(val), true); + break; + + case 'parentItem': + this.parentKey = val; + break; + + case 'deleted': + this.deleted = !!val; + break; + + case 'creators': + this.setCreators(json.creators); break; case 'tags': @@ -4037,49 +4055,37 @@ Zotero.Item.prototype.fromJSON = function (json) { this.attachmentCharset = val; break; + case 'filename': + this.attachmentFilename = val; + break; + case 'path': this.attachmentPath = val; break; // Item fields default: - let changed = this.setField(field, json[field]); - if (changed) { - changedFields[field] = true; + isValidForType[field] = Zotero.ItemFields.isValidForType( + Zotero.ItemFields.getID(field), this.itemTypeID + ); + if (!isValidForType[field]) { + Zotero.logError("Discarding unknown JSON field " + field); + continue; } + this.setField(field, json[field]); + setFields[field] = true; } } // Clear existing fields not specified var previousFields = this.getUsedFields(true); - for each(let field in previousFields) { - if (!changedFields[field] && - // Invalid fields will already have been cleared by the type change - Zotero.ItemFields.isValidForType( - Zotero.ItemFields.getID(field), this.itemTypeID - ) - ) { + for (let field of previousFields) { + // Invalid fields will already have been cleared by the type change + if (!setFields[field] && isValidForType[field]) { this.setField(field, false); } } - // Deleted item flag - this.deleted = !!json.deleted; - - // Creators - let pos = 0; - if (json.creators) { - while (pos