From 72405a94f686936038a136ddb2b7258008a85fc0 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Thu, 16 Feb 2023 18:49:19 +0000 Subject: [PATCH] Add JsonGet(), JsonSet(), and JsonCreate() for convenience. --- TODO.txt | 2 + src/Json.c | 133 +++++++++++++++++++++++++++++++++++++++++++++ src/include/Json.h | 9 +++ 3 files changed, 144 insertions(+) diff --git a/TODO.txt b/TODO.txt index aa0375b..85a1dbe 100644 --- a/TODO.txt +++ b/TODO.txt @@ -14,6 +14,7 @@ Milestone: v0.2.0 [ ] Abstract /email/requestToken and /msidsn/requestToken [ ] User login + [ ] User manipulation functions (so we don't use the DB directly) [ ] Refresh tokens [ ] Logout [ ] Logout all @@ -30,6 +31,7 @@ Milestone: v0.2.0 [x] Document User [x] Document Str and remove old functions from Util docs. [x] Move docs from Matrix to User for UserValidate +[ ] Document new JSON functions [ ] Document UserInteractiveAuth (move docs from Matrix) [ ] Clean up scripts (make tools/lib/matrix.sh) diff --git a/src/Json.c b/src/Json.c index 8f08347..d692666 100644 --- a/src/Json.c +++ b/src/Json.c @@ -1080,3 +1080,136 @@ JsonDecode(FILE * stream) return result; } + +JsonValue * +JsonGet(HashMap *json, size_t nArgs, ...) +{ + va_list argp; + + HashMap *tmp = json; + JsonValue *val = NULL; + size_t i; + + if (!json || ! nArgs) + { + return NULL; + } + + va_start(argp, nArgs); + for (i = 0; i < nArgs - 1; i++) + { + char *key = va_arg(argp, char *); + + val = HashMapGet(tmp, key); + if (!val) + { + goto finish; + } + + if (JsonValueType(val) != JSON_OBJECT) + { + val = NULL; + goto finish; + } + + tmp = JsonValueAsObject(val); + } + + val = HashMapGet(tmp, va_arg(argp, char *)); + +finish: + va_end(argp); + return val; +} + +JsonValue * +JsonSet(HashMap *json, JsonValue *newVal, size_t nArgs, ...) +{ + HashMap *tmp = json; + JsonValue *val = NULL; + size_t i; + + va_list argp; + + if (!json || !newVal || !nArgs) + { + return NULL; + } + + va_start(argp, nArgs); + + for (i = 0; i < nArgs - 1; i++) + { + char *key = va_arg(argp, char *); + + val = HashMapGet(tmp, key); + if (!val) + { + goto finish; + } + + if (JsonValueType(val) != JSON_OBJECT) + { + val = NULL; + goto finish; + } + + tmp = JsonValueAsObject(val); + } + + val = HashMapSet(tmp, va_arg(argp, char *), newVal); + +finish: + va_end(argp); + return val; +} + +int +JsonCreate(HashMap *json, JsonValue *newVal, size_t nArgs, ...) +{ + HashMap *tmp = json; + JsonValue *val = NULL; + size_t i; + char *key; + int ret = 0; + + va_list argp; + + if (!json || !newVal || !nArgs) + { + return 0; + } + + va_start(argp, nArgs); + + for (i = 0; i < nArgs - 1; i++) + { + key = va_arg(argp, char *); + + val = HashMapGet(tmp, key); + if (!val) + { + val = JsonValueObject(HashMapCreate()); + HashMapSet(tmp, key, val); + } + else if (JsonValueType(val) != JSON_OBJECT) + { + goto finish; + } + + tmp = JsonValueAsObject(val); + } + + key = va_arg(argp, char *); + if (HashMapGet(tmp, key)) + { + goto finish; + } + + HashMapSet(tmp, key, newVal); + ret = 1; + +finish: + va_end(argp); + return ret; +} diff --git a/src/include/Json.h b/src/include/Json.h index 4397935..509d93d 100644 --- a/src/include/Json.h +++ b/src/include/Json.h @@ -104,4 +104,13 @@ extern int extern HashMap * JsonDecode(FILE *); +extern JsonValue * + JsonGet(HashMap *, size_t, ...); + +extern JsonValue * + JsonSet(HashMap *, JsonValue *, size_t, ...); + +extern int + JsonCreate(HashMap *, JsonValue *, size_t, ...); + #endif /* TELODENDRIA_JSON_H */