| 1 |
# Work with PROV-JSON |
| 2 |
|
| 3 |
[PROV-JSON](https://openprovenance.org/prov-json/) is the default format used by |
| 4 |
{py:meth}`~prov.model.ProvDocument.serialize`/{py:meth}`~prov.model.ProvDocument.deserialize`. |
| 5 |
It needs no extra dependency — the serializer is always available. |
| 6 |
|
| 7 |
## Serialize to a file |
| 8 |
|
| 9 |
```python |
| 10 |
import prov.model as pm |
| 11 |
|
| 12 |
document = pm.ProvDocument() |
| 13 |
document.set_default_namespace("http://example.org/") |
| 14 |
document.entity("e1") |
| 15 |
|
| 16 |
document.serialize("document.json") # format="json" is the default |
| 17 |
``` |
| 18 |
|
| 19 |
## Serialize to a string |
| 20 |
|
| 21 |
Omit `destination` (or pass `None`) to get the serialization back as a string: |
| 22 |
|
| 23 |
```python |
| 24 |
json_str = document.serialize() |
| 25 |
print(json_str) |
| 26 |
``` |
| 27 |
|
| 28 |
## Deserialize from a file or stream |
| 29 |
|
| 30 |
```python |
| 31 |
loaded = pm.ProvDocument.deserialize("document.json") |
| 32 |
assert loaded == document |
| 33 |
``` |
| 34 |
|
| 35 |
`source` also accepts an open stream: |
| 36 |
|
| 37 |
```python |
| 38 |
with open("document.json") as f: |
| 39 |
loaded = pm.ProvDocument.deserialize(f) |
| 40 |
``` |
| 41 |
|
| 42 |
## Deserialize from a string |
| 43 |
|
| 44 |
Use the `content` keyword instead of `source`: |
| 45 |
|
| 46 |
```python |
| 47 |
loaded = pm.ProvDocument.deserialize(content=json_str, format="json") |
| 48 |
``` |
| 49 |
|
| 50 |
## Auto-detect the format with `prov.read()` |
| 51 |
|
| 52 |
{py:func}`prov.read` tries every registered deserializer in turn — PROV-JSON, then |
| 53 |
PROV-O/RDF, then PROV-N, then PROV-XML — until one both succeeds and produces a non-empty |
| 54 |
document, so it works without knowing the format up front. PROV-JSON is tried first, so |
| 55 |
valid PROV-JSON content always auto-detects correctly: |
| 56 |
|
| 57 |
```python |
| 58 |
import prov |
| 59 |
|
| 60 |
loaded = prov.read("document.json") |
| 61 |
assert loaded == document |
| 62 |
``` |
| 63 |
|
| 64 |
Passing `format="json"` explicitly skips the trial-and-error and gives a proper traceback |
| 65 |
if the content is not valid JSON. |
| 66 |
|
| 67 |
## Common errors |
| 68 |
|
| 69 |
Malformed JSON raises the standard library's decoder error, not a `prov`-specific |
| 70 |
exception: |
| 71 |
|
| 72 |
```python |
| 73 |
try: |
| 74 |
pm.ProvDocument.deserialize(content="not json", format="json") |
| 75 |
except Exception as e: |
| 76 |
print(f"{type(e).__name__}: {e}") |
| 77 |
``` |
| 78 |
|
| 79 |
```text |
| 80 |
JSONDecodeError: Expecting value: line 1 column 1 (char 0) |
| 81 |
``` |
| 82 |
|
| 83 |
Since 2.5.0, `prov.read()` swallows *every* candidate deserializer's failure during |
| 84 |
auto-detection — including a candidate that raises, and a candidate that parses |
| 85 |
successfully but yields an empty document (e.g. an empty file) — and always raises the |
| 86 |
fallback `TypeError` ("Could not read from the source...") once every registered format has |
| 87 |
been tried without success. Pass `format=` explicitly if you want a predictable, |
| 88 |
format-specific error instead of that generic `TypeError`. |