Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/nbformat/v3/nbjson.py: 51%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Read and write notebooks in JSON format."""
3# Copyright (c) IPython Development Team.
4# Distributed under the terms of the Modified BSD License.
5from __future__ import annotations
7import copy
8import json
10from .nbbase import from_dict
11from .rwbase import NotebookReader, NotebookWriter, rejoin_lines, split_lines, strip_transient
14class BytesEncoder(json.JSONEncoder):
15 """A JSON encoder that accepts b64 (and other *ascii*) bytestrings."""
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)
24class JSONReader(NotebookReader):
25 """A JSON notebook reader."""
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
34 def to_notebook(self, d, **kwargs):
35 """Convert a dict to a notebook."""
36 return rejoin_lines(from_dict(d))
39class JSONWriter(NotebookWriter):
40 """A JSON notebook writer."""
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)
55_reader = JSONReader()
56_writer = JSONWriter()
58reads = _reader.reads
59read = _reader.read
60to_notebook = _reader.to_notebook
61write = _writer.write
62writes = _writer.writes