Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to change the default indentation of the produced JSON. #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions jams/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ def search(self, **kwargs):

return self.annotations.search(**kwargs)

def save(self, path_or_file, strict=True, fmt='auto'):
def save(self, path_or_file, strict=True, fmt='auto', indent=2):
"""Serialize annotation as a JSON formatted stream to file.

Parameters
Expand All @@ -1762,6 +1762,14 @@ def save(self, path_or_file, strict=True, fmt='auto'):
If the input is an open file handle, `jams` encoding
is used.

indent : int
If a non-negative integer, then JSON array elements and
object members will be pretty-printed with that indent level. An indent
level of 0 will only insert newlines. `None` is the most compact
representation.

By default, 2 is used.


Raises
------
Expand All @@ -1777,7 +1785,7 @@ def save(self, path_or_file, strict=True, fmt='auto'):
self.validate(strict=strict)

with _open(path_or_file, mode='w', fmt=fmt) as fdesc:
json.dump(self.__json__, fdesc, indent=2)
json.dump(self.__json__, fdesc, indent=indent)

def validate(self, strict=True):
'''Validate a JAMS object against the schema.
Expand Down
5 changes: 3 additions & 2 deletions tests/test_jams.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,10 @@ def input_jam():
return jams.load('tests/fixtures/valid.jams')


def test_jams_save(input_jam, output_path):
@parametrize('indent', [None, 0, 1, 2])
def test_jams_save(input_jam, output_path, indent):

input_jam.save(output_path)
input_jam.save(output_path, indent=indent)
reload_jam = jams.load(output_path)
assert input_jam == reload_jam

Expand Down