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-key falling back to id, 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 with full_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, as upgrade_literature does. Left out, the database assigns one.

Returns:

A saved Item instance with all related ItemName, ItemDate, and ItemIdentifier records created.

Raises:

ValidationError – data["type"] is missing or not a recognized CSL type, or both data["citation-key"] and data["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 via logger.warning() rather than raised.

Parameters:

data – A Python list of CSL JSON dicts (one per bibliographic item).

Returns:

The saved Item instances 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 Item model 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 by ItemName.order within each role, places known identifier types (DOI, ISBN, ISSN, PMID, PMCID, URL) as top-level keys and unknown types under custom, and exports string-or-number CSL fields always as strings.