Better json management
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user