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. 13 map!(a => DubPackage(a.byKey("name").str, 14 a.byKey("path").str, 15 a.getOptional("mainSourceFile"), 16 a.getOptional("targetFileName"), 17 a.byKey("dflags").jsonValueToStrings, 18 a.byKey("importPaths").jsonValueToStrings, 19 a.byKey("stringImportPaths").jsonValueToStrings, 20 a.byKey("files").jsonValueToFiles, 21 a.getOptional("targetType"), 22 a.getOptionalList("versions"), 23 a.getOptionalList("dependencies"), 24 a.getOptionalList("libs"), 25 a.byOptionalKey("active", true), //true for backwards compatibility 26 )). 27 filter!(a => a.active). 28 array); 29 } 30 31 32 private string[] jsonValueToFiles(JSONValue files) @trusted { 33 return files.array. 34 filter!(a => a.byKey("type").str == "source"). 35 map!(a => a.byKey("path").str). 36 array; 37 } 38 39 private string[] jsonValueToStrings(JSONValue json) @trusted { 40 return json.array.map!(a => a.str).array; 41 } 42 43 44 private auto byKey(JSONValue json, in string key) @trusted { 45 return json.object[key]; 46 } 47 48 private auto byOptionalKey(JSONValue json, in string key, bool def) { 49 import std.conv: to; 50 auto value = json.object; 51 return key in value ? value[key].boolean : def; 52 } 53 54 //std.json has no conversion to bool 55 private bool boolean(JSONValue json) @trusted { 56 import std.exception: enforce; 57 enforce!JSONException(json.type == JSON_TYPE.TRUE || json.type == JSON_TYPE.FALSE, 58 "JSONValue is not a boolean"); 59 return json.type == JSON_TYPE.TRUE; 60 } 61 62 private string getOptional(JSONValue json, in string key) @trusted { 63 auto aa = json.object; 64 return key in aa ? aa[key].str : ""; 65 } 66 67 private string[] getOptionalList(JSONValue json, in string key) @trusted { 68 auto aa = json.object; 69 return key in aa ? aa[key].jsonValueToStrings : []; 70 }