1"""Read and write notebooks in JSON format."""
2
3# Copyright (c) IPython Development Team.
4# Distributed under the terms of the Modified BSD License.
5from __future__ import annotations
6
7import copy
8import json
9
10from .nbbase import from_dict
11from .rwbase import NotebookReader, NotebookWriter, rejoin_lines, split_lines, strip_transient
12
13
14class BytesEncoder(json.JSONEncoder):
15 """A JSON encoder that accepts b64 (and other *ascii*) bytestrings."""
16
17 def default(self, obj):
18 """Get the default value of an object."""
19 if isinstance(obj, bytes):
20 return obj.decode("ascii")
21 return json.JSONEncoder.default(self, obj)
22
23
24class JSONReader(NotebookReader):
25 """A JSON notebook reader."""
26
27 def reads(self, s, **kwargs):
28 """Convert a string to a notebook."""
29 nb = json.loads(s, **kwargs)
30 nb = self.to_notebook(nb, **kwargs)
31 nb = strip_transient(nb)
32 return nb # noqa: RET504
33
34 def to_notebook(self, d, **kwargs):
35 """Convert a dict to a notebook."""
36 return rejoin_lines(from_dict(d))
37
38
39class JSONWriter(NotebookWriter):
40 """A JSON notebook writer."""
41
42 def writes(self, nb, **kwargs):
43 """Convert a notebook to a string."""
44 kwargs["cls"] = BytesEncoder
45 kwargs["indent"] = 1
46 kwargs["sort_keys"] = True
47 kwargs["separators"] = (",", ": ")
48 nb = copy.deepcopy(nb)
49 nb = strip_transient(nb)
50 if kwargs.pop("split_lines", True):
51 nb = split_lines(nb)
52 return json.dumps(nb, **kwargs)
53
54
55_reader = JSONReader()
56_writer = JSONWriter()
57
58reads = _reader.reads
59read = _reader.read
60to_notebook = _reader.to_notebook
61write = _writer.write
62writes = _writer.writes