Better json management

This commit is contained in:
2024-02-21 13:49:13 -03:00
parent f9dedca988
commit 9c5eb45d95
4 changed files with 48 additions and 42 deletions

View File

@@ -3,18 +3,24 @@ module util;
import std.format;
import std.json;
JSONValue requiredKey(string key)(JSONValue val) {
const(JSONValue)* j = key in val;
void optional(T)(ref JSONValue val, string key, ref T receiver) {
const(JSONValue)* p = key in val;
if (j)
return *j;
if (p == null)
return;
throw new Exception(format("Key %s not found in json", key));
static if (is(JSONValue == T))
receiver = *p;
else
receiver = (*p).get!T;
}
T checkKey(T)(JSONValue val, string key) {
if (JSONValue* j = key in val)
return (*j).get!T;
return T.init;
T optional(T)(ref JSONValue val, string key) {
T t;
optional(val, key, t);
return t;
}
void required(T)(ref JSONValue val, string key, ref T receiver) {
receiver = val[key].get!T;
}