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.
This commit is contained in:
Matt Clarkson 2015-11-20 13:53:39 +00:00
parent 8b71e16989
commit 6c485625f4
2 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,9 @@
{
"array": [1, 2, "abc", 4.8, null],
"dict": {
"integer": 1,
"float": 4.8,
"string": "Hello, world!"
},
"boolean": true
}

32
playground/json/wscript Normal file
View File

@ -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())