public function toJSON($asArray=false, $requestParams=array(), $includeEmpty=false, $unformattedFields=false) { if ($this->id || $this->key) { if (!$this->loaded['primaryData']) { $this->loadPrimaryData(); } if (!$this->loaded['itemData']) { $this->loadItemData(); } } if (!isset($requestParams['v'])) { $requestParams['v'] = 3; } $regularItem = $this->isRegularItem(); $arr = array(); if ($requestParams['v'] >= 2) { if ($requestParams['v'] >= 3) { $arr['key'] = $this->key; $arr['version'] = $this->version; } else { $arr['itemKey'] = $this->key; $arr['itemVersion'] = $this->version; } $key = $this->getSourceKey(); if ($key) { $arr['parentItem'] = $key; } } $arr['itemType'] = Zotero_ItemTypes::getName($this->itemTypeID); if ($this->isAttachment()) { $val = $this->attachmentLinkMode; $arr['linkMode'] = strtolower(Zotero_Attachments::linkModeNumberToName($val)); } // For regular items, show title and creators first if ($regularItem) { // Get 'title' or the equivalent base-mapped field $titleFieldID = Zotero_ItemFields::getFieldIDFromTypeAndBase($this->itemTypeID, 'title'); $titleFieldName = Zotero_ItemFields::getName($titleFieldID); if ($includeEmpty || $this->itemData[$titleFieldID] !== false) { $arr[$titleFieldName] = $this->itemData[$titleFieldID] !== false ? $this->itemData[$titleFieldID] : ""; } // Creators $arr['creators'] = array(); $creators = $this->getCreators(); foreach ($creators as $creator) { $c = array(); $c['creatorType'] = Zotero_CreatorTypes::getName($creator['creatorTypeID']); // Single-field mode if ($creator['ref']->fieldMode == 1) { $c['name'] = $creator['ref']->lastName; } // Two-field mode else { $c['firstName'] = $creator['ref']->firstName; $c['lastName'] = $creator['ref']->lastName; } $arr['creators'][] = $c; } if (!$arr['creators'] && !$includeEmpty) { unset($arr['creators']); } } else { $titleFieldID = false; } // Item metadata $fields = array_keys($this->itemData); foreach ($fields as $field) { if ($field == $titleFieldID) { continue; } if ($unformattedFields) { $value = $this->itemData[$field]; } else { $value = $this->getField($field); } if (!$includeEmpty && ($value === false || $value === "")) { continue; } $fieldName = Zotero_ItemFields::getName($field); // TEMP if ($fieldName == 'versionNumber') { if ($requestParams['v'] < 3) { $fieldName = 'version'; } } else if ($fieldName == 'accessDate') { if ($requestParams['v'] >= 3 && $value !== false && $value !== "") { $value = Zotero_Date::sqlToISO8601($value); } } $arr[$fieldName] = ($value !== false && $value !== "") ? $value : ""; } if ($requestParams['v'] >= 3) { $arr['dateAdded'] = Zotero_Date::sqlToISO8601($this->dateAdded); $arr['dateModified'] = Zotero_Date::sqlToISO8601($this->dateModified); } // Embedded note for notes and attachments if (!$regularItem) { // Use sanitized version $arr['note'] = $this->getNote(true); } if ($this->isAttachment()) { $val = $this->attachmentLinkMode; $arr['linkMode'] = strtolower(Zotero_Attachments::linkModeNumberToName($val)); $val = $this->attachmentMIMEType; if ($includeEmpty || ($val !== false && $val !== "")) { $arr['contentType'] = $val; } $val = $this->attachmentCharset; if ($includeEmpty || ($val !== false && $val !== "")) { $arr['charset'] = $val; } if ($this->isImportedAttachment()) { $arr['filename'] = $this->attachmentFilename; $val = $this->attachmentStorageHash; if ($includeEmpty || $val) { $arr['md5'] = $val; } $val = $this->attachmentStorageModTime; if ($includeEmpty || $val) { $arr['mtime'] = $val; } } } if ($this->getDeleted()) { $arr['deleted'] = 1; } // Tags $arr['tags'] = array(); $tags = $this->getTags(); if ($tags) { foreach ($tags as $tag) { $t = array( 'tag' => $tag->name ); if ($tag->type != 0) { $t['type'] = $tag->type; } $arr['tags'][] = $t; } } if ($requestParams['v'] >= 2) { if ($this->isTopLevelItem()) { $collections = $this->getCollections(true); $arr['collections'] = $collections; } $arr['relations'] = $this->getRelations(); } if ($asArray) { return $arr; } // Before v3, additional characters were escaped in the JSON, for unclear reasons $escapeAll = $requestParams['v'] <= 2; return Zotero_Utilities::formatJSON($arr, $escapeAll); }
public function toJSON($asArray = false, $prettyPrint = false, $includeEmpty = false, $unformattedFields = false) { if ($this->id || $this->key) { if (!$this->loaded['primaryData']) { $this->loadPrimaryData(true); } if (!$this->loaded['itemData']) { $this->loadItemData(); } } $regularItem = $this->isRegularItem(); $arr = array(); $arr['itemType'] = Zotero_ItemTypes::getName($this->itemTypeID); if ($this->isAttachment()) { $val = $this->attachmentLinkMode; $arr['linkMode'] = strtolower(Zotero_Attachments::linkModeNumberToName($val)); } // For regular items, show title and creators first if ($regularItem) { // Get 'title' or the equivalent base-mapped field $titleFieldID = Zotero_ItemFields::getFieldIDFromTypeAndBase($this->itemTypeID, 'title'); $titleFieldName = Zotero_ItemFields::getName($titleFieldID); if ($includeEmpty || $this->itemData[$titleFieldID] !== false) { $arr[$titleFieldName] = $this->itemData[$titleFieldID] !== false ? $this->itemData[$titleFieldID] : ""; } // Creators $arr['creators'] = array(); $creators = $this->getCreators(); foreach ($creators as $creator) { $c = array(); $c['creatorType'] = Zotero_CreatorTypes::getName($creator['creatorTypeID']); // Single-field mode if ($creator['ref']->fieldMode == 1) { $c['name'] = $creator['ref']->lastName; } else { $c['firstName'] = $creator['ref']->firstName; $c['lastName'] = $creator['ref']->lastName; } $arr['creators'][] = $c; } if (!$arr['creators'] && !$includeEmpty) { unset($arr['creators']); } } else { $titleFieldID = false; } // Item metadata $fields = array_keys($this->itemData); foreach ($fields as $field) { if ($field == $titleFieldID) { continue; } if ($unformattedFields) { $value = $this->itemData[$field]; } else { $value = $this->getField($field); } if (!$includeEmpty && ($value === false || $value === "")) { continue; } $arr[Zotero_ItemFields::getName($field)] = $value ? $value : ""; } // Embedded note for notes and attachments if (!$regularItem) { // Use sanitized version $arr['note'] = $this->getNote(true); } if ($this->isAttachment()) { $val = $this->attachmentLinkMode; $arr['linkMode'] = strtolower(Zotero_Attachments::linkModeNumberToName($val)); $val = $this->attachmentMIMEType; if ($includeEmpty || $val !== false && $val !== "") { $arr['contentType'] = $val; } $val = $this->attachmentCharset; if ($includeEmpty || $val !== false && $val !== "") { $arr['charset'] = $val; } if ($this->isImportedAttachment()) { $arr['filename'] = $this->attachmentFilename; $val = $this->attachmentStorageHash; if ($includeEmpty || $val) { $arr['md5'] = $val; } $val = $this->attachmentStorageModTime; if ($includeEmpty || $val) { $arr['mtime'] = $val; } } } if ($this->getDeleted()) { $arr['deleted'] = 1; } // Tags $arr['tags'] = array(); $tags = $this->getTags(); if ($tags) { foreach ($tags as $tag) { $t = array('tag' => $tag->name); if ($tag->type != 0) { $t['type'] = $tag->type; } $arr['tags'][] = $t; } } if ($asArray) { return $arr; } $mask = JSON_HEX_TAG | JSON_HEX_AMP; if ($prettyPrint) { $json = Zotero_Utilities::json_encode_pretty($arr, $mask); } else { $json = json_encode($arr, $mask); } // Until JSON_UNESCAPED_SLASHES is available $json = str_replace('\\/', '/', $json); return $json; }