1 module reggae.dub.json; 2 3 import reggae.dub.info; 4 import reggae.build; 5 import std.json; 6 import std.algorithm: map, filter; 7 8 9 DubInfo getDubInfo(string jsonString) @trusted { 10 auto json = parseJSON(jsonString); 11 auto packages = json.byKey("packages").array; 12 return DubInfo(packages.map!(a => DubPackage(a.byKey("name").str, 13 a.byKey("path").str, 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) @trusted { 28 return files.array. 29 filter!(a => a.byKey("type").str == "source"). 30 filter!(a => a.byOptionalKey("active", true)). //true for backward compatibility 31 map!(a => a.byKey("path").str). 32 array; 33 } 34 35 private string[] jsonValueToStrings(JSONValue json) @trusted { 36 return json.array.map!(a => a.str).array; 37 } 38 39 40 private auto byKey(JSONValue json, in string key) @trusted { 41 return json.object[key]; 42 } 43 44 private auto byOptionalKey(JSONValue json, in string key, bool def) { 45 import std.conv: to; 46 auto value = json.object; 47 return key in value ? value[key].boolean : def; 48 } 49 50 //std.json has no conversion to bool 51 private bool boolean(JSONValue json) @trusted { 52 import std.exception: enforce; 53 enforce!JSONException(json.type == JSON_TYPE.TRUE || json.type == JSON_TYPE.FALSE, 54 "JSONValue is not a boolean"); 55 return json.type == JSON_TYPE.TRUE; 56 } 57 58 private string getOptional(JSONValue json, in string key) @trusted { 59 auto aa = json.object; 60 return key in aa ? aa[key].str : ""; 61 } 62 63 private string[] getOptionalList(JSONValue json, in string key) @trusted { 64 auto aa = json.object; 65 return key in aa ? aa[key].jsonValueToStrings : []; 66 }