-
Notifications
You must be signed in to change notification settings - Fork 5
/
prettyprintJSON.d
70 lines (66 loc) · 1.83 KB
/
prettyprintJSON.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
modified from code at http://dpaste.dzfl.pl/bcb14d6aang
*/
import std.json, std.stdio, std.range, std.file, std.string, std.algorithm;
void recurse( JSONValue j, string indent="", string name="") {
assert( indent.length <= 100 ); // recursion guard: >50 levels deep means there is danger
if( name.length ) {
write( indent, name, ": " );
}
else {
write(indent);
}
if( j.type == std.json.JSON_TYPE.STRING ) {
writeln( "(string): ", j.str );
}
else if( j.type == std.json.JSON_TYPE.INTEGER ) {
writeln( "(int): ", j.integer );
}
else if( j.type == std.json.JSON_TYPE.UINTEGER ) {
writeln( "(uint): ", j.uinteger );
}
else if( j.type == std.json.JSON_TYPE.FLOAT ) {
writeln( "(float): ", j.floating );
}
else if( j.type == std.json.JSON_TYPE.OBJECT ) {
writeln( "(object): {" );
indent = indent ~ " ";
foreach( key, val; j.object ) {
recurse(val, indent, key);
}
indent.popBackN( 2 );
writeln( indent, "}" );
}
else if( j.type == std.json.JSON_TYPE.ARRAY ) {
writeln( "(array): [" );
indent = indent ~ " ";
foreach( val; j.array ) {
recurse( val, indent );
}
indent.popBackN( 2 );
writeln( indent, "]" );
}
}
void usageMessage( string programName ) {
stderr.writeln( "Usage: " ~ programName ~ " somefile.json" );
stderr.writeln( " or" );
stderr.writeln( " " ~ programName ~ " < somefile.json" );
}
void main( string[] args ) {
if( args.length == 1 ) { // get from stdin
string bigassJsonString;
readf( "%s", &bigassJsonString );
recurse( parseJSON( bigassJsonString ) );
}
else if( args.length == 2 ) {
if( args[ 1 ].startsWith( "-" ) ) { // first check for dash argument
usageMessage( args[ 0 ] );
}
else { // else must be a filename
recurse( parseJSON( readText( args[ 1 ] ) ) );
}
}
else { // too many arguments
usageMessage( args[ 0 ] );
}
}