literature.converters#
CSL JSON serialization and deserialization between Item instances and CSL JSON 1.0.2 dicts.
- from_csl_json(data: dict, *, pk: int | None = None) Any[source]#
Deserialize a CSL JSON dict into a new saved Item with all related records.
Validates
type, reads the citation key (citation-keyfalling back toid, stored exactly as given; a key the store already holds is not rewritten, per docs/adr/0023-citation-key-uniqueness-is-the-readers-concern.md), maps scalar fields, and creates the related Name/ItemName, ItemDate and ItemIdentifier records. Every model instance is validated withfull_clean()before saving.- Parameters:
data – A Python dict containing CSL JSON data for a single bibliographic item.
pk – The primary key to give the new
Item, when it has to keep one it already had elsewhere, asupgrade_literaturedoes. Left out, the database assigns one.
- Returns:
A saved
Iteminstance with all relatedItemName,ItemDate, andItemIdentifierrecords created.- Raises:
ValidationError –
data["type"]is missing or not a recognized CSL type, or bothdata["citation-key"]anddata["id"]are absent.
- from_csl_json_list(data: list[dict]) list[Any][source]#
Import a list of CSL JSON dicts, skipping invalid items with warnings.
Calls
from_csl_json()for each item in the list. Items that fail validation are skipped and their errors are logged vialogger.warning()rather than raised.- Parameters:
data – A Python list of CSL JSON dicts (one per bibliographic item).
- Returns:
The saved
Iteminstances for all successfully imported items, in input order.
- to_csl_json(item: Any) dict[str, Any][source]#
Serialize a saved Item instance to a CSL JSON 1.0.2 compatible dict.
- Parameters:
item – A saved
Itemmodel instance (must have a primary key).- Returns:
A dict conforming to the CSL JSON 1.0.2 schema. Always contains
"id"(=item.citation_key) and"type", omits blank/null optional fields, orders name arrays byItemName.orderwithin each role, places known identifier types (DOI, ISBN, ISSN, PMID, PMCID, URL) as top-level keys and unknown types undercustom, and exports string-or-number CSL fields always as strings.