1
# Simple implementation of a json test runner to run the test against json-py.
9
print "Usage: %s input-json-file", sys.argv[0]
12
input_path = sys.argv[1]
13
base_path = os.path.splitext(input_path)[0]
14
actual_path = base_path + '.actual'
15
rewrite_path = base_path + '.rewrite'
16
rewrite_actual_path = base_path + '.actual-rewrite'
18
def valueTreeToString( fout, value, path = '.' ):
20
if ty is types.DictType:
21
fout.write( '%s={}\n' % path )
22
suffix = path[-1] != '.' and '.' or ''
26
valueTreeToString( fout, value[name], path + suffix + name )
27
elif ty is types.ListType:
28
fout.write( '%s=[]\n' % path )
29
for index, childValue in zip( xrange(0,len(value)), value ):
30
valueTreeToString( fout, childValue, path + '[%d]' % index )
31
elif ty is types.StringType:
32
fout.write( '%s="%s"\n' % (path,value) )
33
elif ty is types.IntType:
34
fout.write( '%s=%d\n' % (path,value) )
35
elif ty is types.FloatType:
36
fout.write( '%s=%.16g\n' % (path,value) )
38
fout.write( '%s=true\n' % path )
40
fout.write( '%s=false\n' % path )
42
fout.write( '%s=null\n' % path )
44
assert False and "Unexpected value type"
46
def parseAndSaveValueTree( input, actual_path ):
47
root = json.loads( input )
48
fout = file( actual_path, 'wt' )
49
valueTreeToString( fout, root )
53
def rewriteValueTree( value, rewrite_path ):
54
rewrite = json.dumps( value )
55
#rewrite = rewrite[1:-1] # Somehow the string is quoted ! jsonpy bug ?
56
file( rewrite_path, 'wt').write( rewrite + '\n' )
59
input = file( input_path, 'rt' ).read()
60
root = parseAndSaveValueTree( input, actual_path )
61
rewrite = rewriteValueTree( json.write( root ), rewrite_path )
62
rewrite_root = parseAndSaveValueTree( rewrite, rewrite_actual_path )