From 3e272f9e5a59b239c58401c8124e116d4c747a81 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Sun, 25 Sep 2022 19:09:59 +0300 Subject: [PATCH] Add imperative convenience functions for loading JSON --- examples/json/app.d | 6 ++--- source/mirage/json.d | 58 ++++++++++++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/examples/json/app.d b/examples/json/app.d index 6e56c0d..a542a8f 100644 --- a/examples/json/app.d +++ b/examples/json/app.d @@ -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 diff --git a/source/mirage/json.d b/source/mirage/json.d index 246fced..c00affe 100644 --- a/source/mirage/json.d +++ b/source/mirage/json.d @@ -21,7 +21,7 @@ import mirage.config : ConfigFactory, ConfigDictionary, ConfigNode, ValueNode, O */ class JsonConfigFactory : ConfigFactory { - /** + /** * Parse configuration from the given JSON string. * * Params: @@ -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");