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. * 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.stdio : writeln;
import std.conv : to; import std.conv : to;
void main() { void main() {
auto config = new JsonConfigFactory().loadFile("config.json"); auto config = loadJsonConfig("config.json");
auto serverConfig = config.getConfig("server"); auto serverConfig = config.getConfig("server");
auto databaseConfig = new JsonConfigFactory().parseConfig(" auto databaseConfig = parseJsonConfig("
{ {
\"host\": \"localhost\", \"host\": \"localhost\",
\"port\": 5432 \"port\": 5432

View file

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