##// END OF EJS Templates
shorten json indent to 1sp
MinRK -
Show More
@@ -1,69 +1,69 b''
1 """Read and write notebooks in JSON format.
1 """Read and write notebooks in JSON format.
2
2
3 Authors:
3 Authors:
4
4
5 * Brian Granger
5 * Brian Granger
6 """
6 """
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2008-2011 The IPython Development Team
9 # Copyright (C) 2008-2011 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 import copy
19 import copy
20 import json
20 import json
21
21
22 from .nbbase import from_dict
22 from .nbbase import from_dict
23 from .rwbase import (
23 from .rwbase import (
24 NotebookReader, NotebookWriter, restore_bytes, rejoin_lines, split_lines
24 NotebookReader, NotebookWriter, restore_bytes, rejoin_lines, split_lines
25 )
25 )
26
26
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 # Code
28 # Code
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30
30
31 class BytesEncoder(json.JSONEncoder):
31 class BytesEncoder(json.JSONEncoder):
32 """A JSON encoder that accepts b64 (and other *ascii*) bytestrings."""
32 """A JSON encoder that accepts b64 (and other *ascii*) bytestrings."""
33 def default(self, obj):
33 def default(self, obj):
34 if isinstance(obj, bytes):
34 if isinstance(obj, bytes):
35 return obj.decode('ascii')
35 return obj.decode('ascii')
36 return json.JSONEncoder.default(self, obj)
36 return json.JSONEncoder.default(self, obj)
37
37
38
38
39 class JSONReader(NotebookReader):
39 class JSONReader(NotebookReader):
40
40
41 def reads(self, s, **kwargs):
41 def reads(self, s, **kwargs):
42 nb = json.loads(s, **kwargs)
42 nb = json.loads(s, **kwargs)
43 nb = self.to_notebook(nb, **kwargs)
43 nb = self.to_notebook(nb, **kwargs)
44 return nb
44 return nb
45
45
46 def to_notebook(self, d, **kwargs):
46 def to_notebook(self, d, **kwargs):
47 return restore_bytes(rejoin_lines(from_dict(d)))
47 return restore_bytes(rejoin_lines(from_dict(d)))
48
48
49
49
50 class JSONWriter(NotebookWriter):
50 class JSONWriter(NotebookWriter):
51
51
52 def writes(self, nb, **kwargs):
52 def writes(self, nb, **kwargs):
53 kwargs['cls'] = BytesEncoder
53 kwargs['cls'] = BytesEncoder
54 kwargs['indent'] = 4
54 kwargs['indent'] = 1
55 kwargs['sort_keys'] = True
55 kwargs['sort_keys'] = True
56 if kwargs.pop('split_lines', True):
56 if kwargs.pop('split_lines', True):
57 nb = split_lines(copy.deepcopy(nb))
57 nb = split_lines(copy.deepcopy(nb))
58 return json.dumps(nb, **kwargs)
58 return json.dumps(nb, **kwargs)
59
59
60
60
61 _reader = JSONReader()
61 _reader = JSONReader()
62 _writer = JSONWriter()
62 _writer = JSONWriter()
63
63
64 reads = _reader.reads
64 reads = _reader.reads
65 read = _reader.read
65 read = _reader.read
66 to_notebook = _reader.to_notebook
66 to_notebook = _reader.to_notebook
67 write = _writer.write
67 write = _writer.write
68 writes = _writer.writes
68 writes = _writer.writes
69
69
General Comments 0
You need to be logged in to leave comments. Login now