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 nbformat.notebooknode import from_dict
11
12from .rwbase import NotebookReader, NotebookWriter, rejoin_lines, split_lines, strip_transient
13
14
15class BytesEncoder(json.JSONEncoder):
16 """A JSON encoder that accepts b64 (and other *ascii*) bytestrings."""
17
18 def default(self, obj):
19 """Get the default value of an object."""
20 if isinstance(obj, bytes):
21 return obj.decode("ascii")
22 return json.JSONEncoder.default(self, obj)
23
24
25class JSONReader(NotebookReader):
26 """A JSON notebook reader."""
27
28 def reads(self, s, **kwargs):
29 """Read a JSON string into a Notebook object"""
30 nb = json.loads(s, **kwargs)
31 nb = self.to_notebook(nb, **kwargs)
32 return nb # noqa: RET504
33
34 def to_notebook(self, d, **kwargs):
35 """Convert a disk-format notebook dict to in-memory NotebookNode
36
37 handles multi-line values as strings, scrubbing of transient values, etc.
38 """
39 nb = from_dict(d)
40 nb = rejoin_lines(nb)
41 nb = strip_transient(nb)
42 return nb # noqa: RET504
43
44
45class JSONWriter(NotebookWriter):
46 """A JSON notebook writer."""
47
48 def writes(self, nb, **kwargs):
49 """Serialize a NotebookNode object as a JSON string"""
50 kwargs["cls"] = BytesEncoder
51 kwargs["indent"] = 1
52 kwargs["sort_keys"] = True
53 kwargs["separators"] = (",", ": ")
54 kwargs.setdefault("ensure_ascii", False)
55 # don't modify in-memory dict
56 nb = copy.deepcopy(nb)
57 if kwargs.pop("split_lines", True):
58 nb = split_lines(nb)
59 nb = strip_transient(nb)
60 return json.dumps(nb, **kwargs)
61
62
63_reader = JSONReader()
64_writer = JSONWriter()
65
66reads = _reader.reads
67read = _reader.read
68to_notebook = _reader.to_notebook
69write = _writer.write
70writes = _writer.writes