From 2c617a42f82f01c62c28931a15b71c70d42ea93c Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Fri, 20 Nov 2015 13:51:11 +0000 Subject: [PATCH] Add a Node.read_json() function This allows an easy way to read a JSON file into a python object. It, by default, on Python2 converts all strings to normal strings. This is the common use case for simple JSON files. Files that have UTF8 codepoints over the normal ASCII range can use the 'convert = False' argument. --- waflib/Node.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/waflib/Node.py b/waflib/Node.py index 5943d1dd..005f80e5 100644 --- a/waflib/Node.py +++ b/waflib/Node.py @@ -20,7 +20,7 @@ Node: filesystem structure, contains lists of nodes (:py:class:`waflib.Node.Nod3`, see the :py:class:`waflib.Context.Context` initializer). A reference to the context owning a node is held as self.ctx """ -import os, re, sys, shutil +import os, re, sys, shutil, json from waflib import Utils, Errors exclude_regs = ''' @@ -135,6 +135,40 @@ class Node(object): """ return Utils.readf(self.abspath(), flags, encoding) + def read_json(self, convert=True, encoding='utf-8'): + """ + Read and parse the contents of this node as JSON:: + + def build(bld): + bld.path.find_node('abc.json').read_json() + + Note that this by default automatically decodes unicode strings on Python2, unlike what the Python JSON module does. + + :type convert: boolean + :param convert: Prevents decoding of unicode strings on Python2 + :type encoding: string + :param encoding: The encoding of the file to read. This default to UTF8 as per the JSON standard + :rtype: object + :return: Parsed file contents + """ + object_pairs_hook = None + + if convert and sys.hexversion < 0x3000000: + def convert(value): + if isinstance(value, list): + return [convert(element) for element in value] + elif isinstance(value, unicode): + return str(value) + else: + return value + + def object_pairs(pairs): + return dict((str(pair[0]), convert(pair[1])) for pair in pairs) + + object_pairs_hook = object_pairs + + return json.loads(self.read(encoding=encoding), object_pairs_hook=object_pairs_hook) + def write(self, data, flags='w', encoding='ISO8859-1'): """ Write some text to the physical file represented by this node::