From 6c485625f4f95c4c890381b2a3ff80d616f7f614 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Fri, 20 Nov 2015 13:53:39 +0000 Subject: [PATCH] Added a JSON example to the playground The example reads a file as JSON, adds a key, then writes the new structure to a JSON file in the build folder. The example accepts a '--pretty' argument to output human readable JSON to the file. --- playground/json/test.json | 9 +++++++++ playground/json/wscript | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 playground/json/test.json create mode 100644 playground/json/wscript diff --git a/playground/json/test.json b/playground/json/test.json new file mode 100644 index 00000000..778faebe --- /dev/null +++ b/playground/json/test.json @@ -0,0 +1,9 @@ +{ + "array": [1, 2, "abc", 4.8, null], + "dict": { + "integer": 1, + "float": 4.8, + "string": "Hello, world!" + }, + "boolean": true +} diff --git a/playground/json/wscript b/playground/json/wscript new file mode 100644 index 00000000..48f819b4 --- /dev/null +++ b/playground/json/wscript @@ -0,0 +1,32 @@ +#! /usr/bin/env python +# encoding: utf-8 +# Matt Clarkson, 2015 (ita) + +VERSION='0.0.1' +APPNAME='json_test' + +top = '.' + +import sys +import waflib.Configure +waflib.Configure.autoconfig = True + +def options(opt): + opt.add_option( + '--pretty', + action = 'store_true', + help = 'pretty prints the writing of the JSON') + +def configure(conf): + pass + +def build(bld): + node = bld.srcnode.make_node('test.json') + json = node.read_json() + print('Read', json) + json['new_key'] = { + 'number': 199 + } + output = bld.bldnode.make_node('output.json') + output.write_json(json, pretty=bld.options.pretty) + print('Wrote', output.read())