JSON Interface

muspy.load_json(path, compressed=None)[source]

Load a JSON file into a Music object.

Parameters
  • path (str, Path or TextIO) – Path to the file or the file to load.

  • compressed (bool, optional) – Whether the file is a compressed JSON file (.json.gz). Has no effect when path is a file object. Defaults to infer from the extension (.gz).

Returns

Loaded Music object.

Return type

muspy.Music

Notes

When a path is given, assume UTF-8 encoding and gzip compression if compressed=True.

muspy.save_json(path, music, skip_missing=True, ensure_ascii=False, compressed=None, **kwargs)[source]

Save a Music object to a JSON file.

Parameters
  • path (str, Path or TextIO) – Path or file to save the JSON data.

  • music (muspy.Music) – Music object to save.

  • skip_missing (bool, default: True) – Whether to skip attributes with value None or those that are empty lists.

  • ensure_ascii (bool, default: False) – Whether to escape non-ASCII characters. Will be passed to PyYAML’s yaml.dump.

  • compressed (bool, optional) – Whether to save as a compressed JSON file (.json.gz). Has no effect when path is a file object. Defaults to infer from the extension (.gz).

  • **kwargs – Keyword arguments to pass to json.dumps().

Notes

When a path is given, use UTF-8 encoding and gzip compression if compressed=True.

Note

A JSON schema is available for validating a JSON fiule against MusPy’s format.