2
0
mirror of https://gitlab.com/ita1024/waf.git synced 2024-11-22 01:46:15 +01:00

Add Node.write_json()

Allows for a python object to be serialized to disk as JSON easily. By
default the function pretty prints the JSON which makes the file easily
human readable. A compact representation of the JSON can be done with
'pretty = False'
This commit is contained in:
Matt Clarkson 2015-11-20 13:52:26 +00:00
parent 2c617a42f8
commit 8b71e16989

View File

@ -183,6 +183,29 @@ class Node(object):
"""
Utils.writef(self.abspath(), data, flags, encoding)
def write_json(self, data, pretty=True):
"""
Writes a python object as JSON to disk. Files are always written as UTF8 as per the JSON standard::
def build(bld):
bld.path.find_node('xyz.json').write_json(199)
:type data: object
:param data: The data to write to disk
:type pretty: boolean
:param pretty: Determines if the JSON will be nicely space separated
"""
indent = 2
separators = (',', ': ')
sort_keys = pretty
newline = os.linesep
if not pretty:
indent = None
separators = (',', ':')
newline = ''
output = json.dumps(data, indent=indent, separators=separators, sort_keys=sort_keys) + newline
self.write(output, encoding='utf-8')
def chmod(self, val):
"""
Change file/dir permissions::