Add imperative convenience functions for loading JSON

This commit is contained in:
Mike Bierlee 2022-09-25 19:09:59 +03:00
parent 94ae45a408
commit 3e272f9e5a
2 changed files with 46 additions and 18 deletions

View file

@ -7,15 +7,15 @@
* The full terms of the license can be found in the LICENSE file.
*/
import mirage.json : JsonConfigFactory;
import mirage.json : loadJsonConfig, parseJsonConfig;
import std.stdio : writeln;
import std.conv : to;
void main() {
auto config = new JsonConfigFactory().loadFile("config.json");
auto config = loadJsonConfig("config.json");
auto serverConfig = config.getConfig("server");
auto databaseConfig = new JsonConfigFactory().parseConfig("
auto databaseConfig = parseJsonConfig("
{
\"host\": \"localhost\",
\"port\": 5432

View file

@ -36,7 +36,7 @@ class JsonConfigFactory : ConfigFactory {
* Parse configuration from a JSONValue tree.
*
* Params:
* contents = Text contents of the config to be parsed.
* contents = JSONValue config to be parsed.
* Returns: The parsed configuration.
*/
ConfigDictionary parseJson(JSONValue json) {
@ -96,6 +96,39 @@ class JsonConfigFactory : ConfigFactory {
}
}
/**
* Parse JSON config from the given JSON string.
* Params:
* json = Text contents of the config to be parsed.
* Returns: The parsed configuration.
*/
ConfigDictionary parseJsonConfig(string json) {
return new JsonConfigFactory().parseConfig(json);
}
/**
* Parse JSON config from the given JSONValue.
*
* Params:
* contents = JSONValue config to be parsed.
* Returns: The parsed configuration.
*/
ConfigDictionary parseJsonConfig(JSONValue json) {
return new JsonConfigFactory().parseJson(json);
}
/**
* Load a JSON configuration file from disk.
*
* Params:
* filePath = Path to the JSON configuration file.
* Returns: The loaded configuration.
*/
ConfigDictionary loadJsonConfig(string filePath) {
return new JsonConfigFactory().loadFile(filePath);
}
version (unittest) {
@("Parse JSON")
unittest {
@ -111,8 +144,7 @@ version (unittest) {
"numberos": numbersJson, "decimalas": decimalsJson
];
auto loader = new JsonConfigFactory();
auto config = loader.parseJson(jsonConfig);
auto config = parseJsonConfig(jsonConfig);
assert(config.get("server.hostname") == "hosty.com");
assert(config.get("server.port") == "1234");
@ -126,13 +158,11 @@ version (unittest) {
@("Parse JSON root values")
unittest {
auto loader = new JsonConfigFactory();
assert(loader.parseJson(JSONValue("hi")).get(".") == "hi");
assert(loader.parseJson(JSONValue(1)).get(".") == "1");
assert(loader.parseJson(JSONValue(null)).get(".") == null);
assert(loader.parseJson(JSONValue(1.8)).get(".") == "1.8");
assert(loader.parseJson(JSONValue([1, 2, 3])).get("[2]") == "3");
assert(parseJsonConfig(JSONValue("hi")).get(".") == "hi");
assert(parseJsonConfig(JSONValue(1)).get(".") == "1");
assert(parseJsonConfig(JSONValue(null)).get(".") == null);
assert(parseJsonConfig(JSONValue(1.8)).get(".") == "1.8");
assert(parseJsonConfig(JSONValue([1, 2, 3])).get("[2]") == "3");
}
@("Parse JSON string")
@ -146,8 +176,7 @@ version (unittest) {
}
";
auto loader = new JsonConfigFactory();
auto config = loader.parseJson(json);
auto config = parseJsonConfig(json);
assert(config.get("name") == "Groot");
assert(config.get("traits[1]") == "tree");
@ -157,8 +186,7 @@ version (unittest) {
@("Load JSON file")
unittest {
auto loader = new JsonConfigFactory();
auto config = loader.loadFile("testfiles/groot.json");
auto config = loadJsonConfig("testfiles/groot.json");
assert(config.get("name") == "Groot");
assert(config.get("traits[1]") == "tree");