diff --git a/source/dub/compilers/buildsettings.d b/source/dub/compilers/buildsettings.d index 9d5652c..1966802 100644 --- a/source/dub/compilers/buildsettings.d +++ b/source/dub/compilers/buildsettings.d @@ -405,6 +405,7 @@ struct Flags (T) { import dub.internal.vibecompat.data.serialization : ignore; + import dub.internal.vibecompat.data.json : Json; @ignore BitFlags!T values; @@ -420,6 +421,39 @@ alias values this; + public Json toJson() const + { + import std.conv : to; + import std.traits : EnumMembers; + + auto json = Json.emptyArray; + + static foreach (em; EnumMembers!T) { + static if (em != 0) { + if (values & em) { + json ~= em.to!string; + } + } + } + + return json; + } + + public static Flags!T fromJson(Json json) + { + import std.conv : to; + import std.exception : enforce; + + BitFlags!T flags; + + enforce(json.type == Json.Type.array, "Should be an array"); + foreach (jval; json) { + flags |= jval.get!string.to!T; + } + + return Flags!T(flags); + } + /** * Reads a list of flags from a JSON/YAML document and converts them * to our internal representation. @@ -444,6 +478,16 @@ unittest { + import dub.internal.vibecompat.data.json; + + auto opts = Flags!BuildOption(BuildOption.debugMode | BuildOption.debugInfo | BuildOption.warningsAsErrors); + const str = serializeToJsonString(opts); + assert(str == `["debugMode","debugInfo","warningsAsErrors"]`); + assert(deserializeJson!(typeof(opts))(str) == opts); +} + +unittest +{ import dub.internal.configy.Read; static struct Config