diff --git a/source/mirage/config.d b/source/mirage/config.d index 24ac843..9aec79a 100644 --- a/source/mirage/config.d +++ b/source/mirage/config.d @@ -12,6 +12,7 @@ module mirage.config; import std.exception : enforce; import std.string : split, startsWith, endsWith, join, lastIndexOf; import std.conv : to, ConvException; +import std.file : readText; class ConfigReadException : Exception { this(string msg, string file = __FILE__, size_t line = __LINE__) { @@ -265,8 +266,12 @@ class ConfigDictionary { } } -interface ConfigFactory { - ConfigDictionary loadFile(string path); +abstract class ConfigFactory { + ConfigDictionary loadFile(string path) { + auto json = readText(path); + return parseConfig(json); + } + ConfigDictionary parseConfig(string contents); } diff --git a/source/mirage/json.d b/source/mirage/json.d index a4b2cf7..1ca0cd4 100644 --- a/source/mirage/json.d +++ b/source/mirage/json.d @@ -15,11 +15,7 @@ import std.conv : to; import mirage.config : ConfigFactory, ConfigDictionary, ConfigNode, ValueNode, ObjectNode, ArrayNode, ConfigCreationException; class JsonConfigFactory : ConfigFactory { - ConfigDictionary loadFile(string path) { - throw new Exception("not yet implemented"); - } - - ConfigDictionary parseConfig(string contents) { + override ConfigDictionary parseConfig(string contents) { return parseJson(parseJSON(contents)); } @@ -130,4 +126,15 @@ version (unittest) { assert(config.get("age") == "8728"); assert(config.get("taxNumber") == null); } + + @("Load JSON file") + unittest { + auto loader = new JsonConfigFactory(); + auto config = loader.loadFile("testfiles/groot.json"); + + assert(config.get("name") == "Groot"); + assert(config.get("traits[1]") == "tree"); + assert(config.get("age") == "8728"); + assert(config.get("taxNumber") == null); + } }