diff --git a/source/mirage/json.d b/source/mirage/json.d index 5088c56..a4b2cf7 100644 --- a/source/mirage/json.d +++ b/source/mirage/json.d @@ -9,7 +9,7 @@ module mirage.json; -import std.json : JSONValue, JSONType; +import std.json : JSONValue, JSONType, parseJSON; import std.conv : to; import mirage.config : ConfigFactory, ConfigDictionary, ConfigNode, ValueNode, ObjectNode, ArrayNode, ConfigCreationException; @@ -20,7 +20,7 @@ class JsonConfigFactory : ConfigFactory { } ConfigDictionary parseConfig(string contents) { - throw new Exception("not yet implemented"); + return parseJson(parseJSON(contents)); } ConfigDictionary parseJson(JSONValue json) { @@ -73,7 +73,8 @@ class JsonConfigFactory : ConfigFactory { } version (unittest) { - @("Parse JSON") unittest { + @("Parse JSON") + unittest { JSONValue serverJson = ["hostname": "hosty.com", "port": "1234"]; JSONValue nullJson = ["isNull": null]; JSONValue socketsJson = [ @@ -99,7 +100,8 @@ version (unittest) { assert(config.get("decimalas[2]") == "6.7"); } - @("Parse JSON root values") unittest { + @("Parse JSON root values") + unittest { auto loader = new JsonConfigFactory(); assert(loader.parseJson(JSONValue("hi")).get(".") == "hi"); @@ -108,4 +110,24 @@ version (unittest) { assert(loader.parseJson(JSONValue(1.8)).get(".") == "1.8"); assert(loader.parseJson(JSONValue([1, 2, 3])).get("[2]") == "3"); } + + @("Parse JSON string") + unittest { + string json = " + { + \"name\": \"Groot\", + \"traits\": [\"groot\", \"tree\"], + \"age\": 8728, + \"taxNumber\": null + } + "; + + auto loader = new JsonConfigFactory(); + auto config = loader.parseJson(json); + + assert(config.get("name") == "Groot"); + assert(config.get("traits[1]") == "tree"); + assert(config.get("age") == "8728"); + assert(config.get("taxNumber") == null); + } } diff --git a/testfiles/groot.json b/testfiles/groot.json new file mode 100644 index 0000000..7dc52f1 --- /dev/null +++ b/testfiles/groot.json @@ -0,0 +1,6 @@ +{ + "name": "Groot", + "traits": ["groot", "tree"], + "age": 8728, + "taxNumber": null +}