1 module reggae.dub_json;
2 
3 import reggae.dub;
4 import reggae.build;
5 import stdx.data.json;
6 import std.algorithm: map, filter;
7 
8 
9 DubInfo getDubInfo(string jsonString) @safe {
10     auto json = parseJSONValue(jsonString);
11     auto packages = json.byKey("packages").get!(JSONValue[]);
12     return DubInfo(packages.map!(a => DubPackage(a.byKey("name").get!string,
13                                                  a.byKey("path").get!string,
14                                                  a.getOptional("mainSourceFile"),
15                                                  a.getOptional("targetFileName"),
16                                                  a.byKey("dflags").jsonValueToStrings,
17                                                  a.byKey("importPaths").jsonValueToStrings,
18                                                  a.byKey("stringImportPaths").jsonValueToStrings,
19                                                  a.byKey("files").jsonValueToFiles,
20                                                  a.getOptional("targetType"),
21                                                  a.getOptionalList("versions"),
22                                                  a.getOptionalList("dependencies"),
23                                                  a.getOptionalList("libs"))).array);
24 }
25 
26 
27 private string[] jsonValueToFiles(JSONValue files) @safe {
28     return files.get!(JSONValue[]).
29         filter!(a => a.byKey("type") == "source").
30         filter!(a => a.byOptionalKey("active", true)). //true for backward compatibility
31         map!(a => a.byKey("path").get!string).
32         array;
33 }
34 
35 private string[] jsonValueToStrings(JSONValue json) @safe {
36     return json.get!(JSONValue[]).map!(a => a.get!string).array;
37 }
38 
39 
40 private auto byKey(JSONValue json, in string key) @safe {
41     return json.get!(JSONValue[string])[key];
42 }
43 
44 private auto byOptionalKey(T)(JSONValue json, in string key, T def) {
45     auto value = json.get!(JSONValue[string]);
46     return key in value ? value[key].get!T : def;
47 }
48 
49 
50 private string getOptional(JSONValue json, in string key) @safe {
51     auto aa = json.get!(JSONValue[string]);
52     return key in aa ? aa[key].get!string : "";
53 }
54 
55 private string[] getOptionalList(JSONValue json, in string key) @safe {
56     auto aa = json.get!(JSONValue[string]);
57     return key in aa ? aa[key].jsonValueToStrings : [];
58 }