msl.io.writers.json_ module

Writer for a JSON file format. The corresponding Reader is JSONReader.

class msl.io.writers.json_.JSONWriter(file=None, **metadata)[source]

Bases: Writer

Create a JSON writer.

You can use JSONWriter as a context manager. For example,

>>> with JSONWriter('example.json') as root:
...     dset = root.create_dataset('dset', data=[1, 2, 3])
...     root.update_context_kwargs(indent=4)

This will automatically write root to the specified file using indent=4 as a keyword argument to the write() method when the with block exits.

Parameters:
  • file (path-like or file-like, optional) – The file to write the data to. Can also be specified in the write() method.

  • **metadata – Key-value pairs that are used as Metadata of the Root.

write(file=None, root=None, **kwargs)[source]

Write to a JSON file.

The first line in the output file contains a description that the file was created by the JSONWriter. It begins with a # and contains a version number.

Version 1.0 specifications

  • Use the 'dtype' and 'data' keys to uniquely identify a JSON object as a Dataset.

  • If a Metadata key has a value that is a Metadata object then the key becomes the name of a Group and the value becomes Metadata of that Group.

Parameters:
  • file (path-like or file-like, optional) – The file to write the root to. If None then uses the value of file that was specified when JSONWriter was instantiated.

  • root (Root, optional) – Write root in JSON format. If None then write the Groups and Datasets in this JSONWriter.

  • **kwargs – Accepts mode, encoding and errors keyword arguments which are passed to open(). The default encoding value is 'utf-8' and the default errors value is 'strict'. All additional keyword arguments are passed to json.dump. The default indentation is 2.