1 module examples.json_reader;
2 
3 import cmdline;
4 
5 import std.stdio;
6 import std.file;
7 import std.path;
8 import std.json;
9 import std.format;
10 
11 // void main(in string[] argv) {
12 //     program.name("jread");
13 //     program.argument!string("<file-name>", "the file name you want to parse");
14 //     program.argument!string("[key]", "the key of the json object");
15 //     program.option("-p, --pretty", "set the output is human readable", true);
16 //     program.option("-P, --no-pretty", "disable pretty output");
17 
18 //     Option mopt = createOption!string("-m, --mode <mode>", "set the parsing mode");
19 //     mopt.choices("doNotEscapeSlashes", "escapeNonAsciiChars", "none",
20 //         "specialFloatLiterals", "strictParsing");
21 //     mopt.defaultVal("none");
22 
23 //     program.addOption(mopt);
24 
25 //     program.action((opts, fname, _key) {
26 //         string current_dir = getcwd();
27 //         string file_name = cast(string) fname;
28 //         bool is_pretty = opts("pretty").get!bool;
29 //         string mode = cast(string) opts("mode");
30 //         string key = _key.isValid ? _key.get!string : "";
31 //         string f = buildPath(current_dir, file_name);
32 
33 //         JSONValue jsconfig;
34 //         JSONOptions jmode;
35 
36 //         try {
37 //             string str = readText(f);
38 //             jsconfig = parseJSON(str);
39 //             switch (mode) {
40 //             case JSONOptions.doNotEscapeSlashes.stringof:
41 //                 jmode = JSONOptions.doNotEscapeSlashes;
42 //                 break;
43 //             case JSONOptions.escapeNonAsciiChars.stringof:
44 //                 jmode = JSONOptions.escapeNonAsciiChars;
45 //                 break;
46 //             case JSONOptions.none.stringof:
47 //                 jmode = JSONOptions.none;
48 //                 break;
49 //             case JSONOptions.specialFloatLiterals.stringof:
50 //                 jmode = JSONOptions.specialFloatLiterals;
51 //                 break;
52 //             default:
53 //                 jmode = JSONOptions.strictParsing;
54 //                 break;
55 //             }
56 //             if (key == "")
57 //                 writeln(toJSON(jsconfig, is_pretty, jmode));
58 //             else {
59 //                 if (jsconfig.type != JSONType.OBJECT) {
60 //                     throw new JReadError(
61 //                         "ERRROR: the target root json is not `" ~ JSONType.OBJECT.stringof ~ "`");
62 //                 }
63 //                 auto ptr = key in jsconfig;
64 //                 if (!ptr)
65 //                     throw new JReadError(
66 //                         format!"ERRROR: the key: `%s` not found in the target root json"(key));
67 //                 writeln(toJSON(*ptr, is_pretty, jmode));
68 //             }
69 //         }
70 //         catch (JReadError e) {
71 //             stderr.writeln(e.msg);
72 //             stderr.writeln("Here is the target root json: ");
73 //             stderr.writeln(toJSON(jsconfig, is_pretty, jmode));
74 //         }
75 //         catch (Exception e) {
76 //             stderr.writeln(e.msg);
77 //         }
78 //     });
79 
80 //     program.parse(argv);
81 // }
82 
83 // class JReadError : Error {
84 //     this(string msg, Throwable nextInChain = null) pure nothrow @nogc @safe {
85 //         super(msg, nextInChain);
86 //     }
87 // }
88 
89 struct JreadResult {
90     mixin BEGIN;
91     mixin DESC!"read the data from json file";
92     mixin VERSION!"0.0.1";
93 
94     mixin DEF_ARG!(
95         "fileName", string,
96         Desc_d!"the file name you want to parse"
97     );
98 
99     mixin DEF_ARG!(
100         "key", string,
101         Optional_d,
102         Desc_d!"the key of the json object"
103     );
104 
105     mixin DEF_OPT!(
106         "pretty", bool, "-p",
107         Desc_d!"set the output is human readable",
108         Negate_d!("-P", "disable pretty output")
109     );
110 
111     mixin DEF_OPT!(
112         "mode", string, "-m <mode>",
113         Desc_d!"set the parsing mode",
114         Choices_d!("doNotEscapeSlashes", "escapeNonAsciiChars", "none", "specialFloatLiterals", "strictParsing"),
115         Default_d!"none"
116     );
117 
118     mixin END;
119 
120     void action() {
121         auto cur_dir = getcwd();
122         auto file_name = fileName.get;
123         auto is_pretty = pretty.get;
124         auto mode_ = mode.get;
125         auto key_ = key ? key.get : "";
126         auto f = buildPath(cur_dir, file_name);
127 
128         JSONValue jsconfig;
129         JSONOptions jmode;
130 
131         try {
132             string str = readText(f);
133             jsconfig = parseJSON(str);
134             switch (mode_) {
135             case JSONOptions.doNotEscapeSlashes.stringof:
136                 jmode = JSONOptions.doNotEscapeSlashes;
137                 break;
138             case JSONOptions.escapeNonAsciiChars.stringof:
139                 jmode = JSONOptions.escapeNonAsciiChars;
140                 break;
141             case JSONOptions.none.stringof:
142                 jmode = JSONOptions.none;
143                 break;
144             case JSONOptions.specialFloatLiterals.stringof:
145                 jmode = JSONOptions.specialFloatLiterals;
146                 break;
147             default:
148                 jmode = JSONOptions.strictParsing;
149                 break;
150             }
151             if (key_ == "")
152                 writeln(toJSON(jsconfig, is_pretty, jmode));
153             else {
154                 if (jsconfig.type != JSONType.OBJECT) {
155                     throw new JReadError(
156                         "ERRROR: the target root json is not `" ~ JSONType.OBJECT.stringof ~ "`");
157                 }
158                 auto ptr = key_ in jsconfig;
159                 if (!ptr)
160                     throw new JReadError(
161                         format!"ERRROR: the key: `%s` not found in the target root json"(key_));
162                 writeln(toJSON(*ptr, is_pretty, jmode));
163             }
164         }
165         catch (JReadError e) {
166             stderr.writeln(e.msg);
167             stderr.writeln("Here is the target root json: ");
168             stderr.writeln(toJSON(jsconfig, is_pretty, jmode));
169         }
170         catch (Exception e) {
171             stderr.writeln(e.msg);
172         }
173     }
174 }
175 
176 class JReadError : Error {
177     this(string msg, Throwable nextInChain = null) pure nothrow @nogc @safe {
178         super(msg, nextInChain);
179     }
180 }
181 
182 void main(in string[] argv) {
183     argv.run!JreadResult;
184 }