Devin Jeanpierre | 19fc55f | 2017-04-24 10:49:00 -0700 | [diff] [blame] | 1 | # Copyright 2007 The JsonCpp Authors |
Sam Clegg | 6386061 | 2015-04-09 18:01:33 -0700 | [diff] [blame] | 2 | # Distributed under MIT license, or public domain if desired and |
| 3 | # recognized in your jurisdiction. |
| 4 | # See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE |
| 5 | |
| 6 | """Simple implementation of a json test runner to run the test against |
| 7 | json-py.""" |
| 8 | |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 9 | from __future__ import print_function |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 10 | import sys |
| 11 | import os.path |
| 12 | import json |
| 13 | import types |
| 14 | |
| 15 | if len(sys.argv) != 2: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 16 | print("Usage: %s input-json-file", sys.argv[0]) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 17 | sys.exit(3) |
| 18 | |
| 19 | input_path = sys.argv[1] |
| 20 | base_path = os.path.splitext(input_path)[0] |
| 21 | actual_path = base_path + '.actual' |
| 22 | rewrite_path = base_path + '.rewrite' |
| 23 | rewrite_actual_path = base_path + '.actual-rewrite' |
| 24 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 25 | def valueTreeToString(fout, value, path = '.'): |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 26 | ty = type(value) |
| 27 | if ty is types.DictType: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 28 | fout.write('%s={}\n' % path) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 29 | suffix = path[-1] != '.' and '.' or '' |
| 30 | names = value.keys() |
| 31 | names.sort() |
| 32 | for name in names: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 33 | valueTreeToString(fout, value[name], path + suffix + name) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 34 | elif ty is types.ListType: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 35 | fout.write('%s=[]\n' % path) |
| 36 | for index, childValue in zip(xrange(0,len(value)), value): |
| 37 | valueTreeToString(fout, childValue, path + '[%d]' % index) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 38 | elif ty is types.StringType: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 39 | fout.write('%s="%s"\n' % (path,value)) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 40 | elif ty is types.IntType: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 41 | fout.write('%s=%d\n' % (path,value)) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 42 | elif ty is types.FloatType: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 43 | fout.write('%s=%.16g\n' % (path,value)) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 44 | elif value is True: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 45 | fout.write('%s=true\n' % path) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 46 | elif value is False: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 47 | fout.write('%s=false\n' % path) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 48 | elif value is None: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 49 | fout.write('%s=null\n' % path) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 50 | else: |
| 51 | assert False and "Unexpected value type" |
| 52 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 53 | def parseAndSaveValueTree(input, actual_path): |
| 54 | root = json.loads(input) |
| 55 | fout = file(actual_path, 'wt') |
| 56 | valueTreeToString(fout, root) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 57 | fout.close() |
| 58 | return root |
| 59 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 60 | def rewriteValueTree(value, rewrite_path): |
| 61 | rewrite = json.dumps(value) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 62 | #rewrite = rewrite[1:-1] # Somehow the string is quoted ! jsonpy bug ? |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 63 | file(rewrite_path, 'wt').write(rewrite + '\n') |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 64 | return rewrite |
| 65 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 66 | input = file(input_path, 'rt').read() |
| 67 | root = parseAndSaveValueTree(input, actual_path) |
| 68 | rewrite = rewriteValueTree(json.write(root), rewrite_path) |
| 69 | rewrite_root = parseAndSaveValueTree(rewrite, rewrite_actual_path) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 70 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 71 | sys.exit(0) |