Add JsonGet(), JsonSet(), and JsonCreate() for convenience.

This commit is contained in:
Jordan Bancino 2023-02-16 18:49:19 +00:00
parent d255ce1a21
commit 72405a94f6
3 changed files with 144 additions and 0 deletions

View file

@ -14,6 +14,7 @@ Milestone: v0.2.0
[ ] Abstract /email/requestToken and /msidsn/requestToken [ ] Abstract /email/requestToken and /msidsn/requestToken
[ ] User login [ ] User login
[ ] User manipulation functions (so we don't use the DB directly)
[ ] Refresh tokens [ ] Refresh tokens
[ ] Logout [ ] Logout
[ ] Logout all [ ] Logout all
@ -30,6 +31,7 @@ Milestone: v0.2.0
[x] Document User [x] Document User
[x] Document Str and remove old functions from Util docs. [x] Document Str and remove old functions from Util docs.
[x] Move docs from Matrix to User for UserValidate [x] Move docs from Matrix to User for UserValidate
[ ] Document new JSON functions
[ ] Document UserInteractiveAuth (move docs from Matrix) [ ] Document UserInteractiveAuth (move docs from Matrix)
[ ] Clean up scripts (make tools/lib/matrix.sh) [ ] Clean up scripts (make tools/lib/matrix.sh)

View file

@ -1080,3 +1080,136 @@ JsonDecode(FILE * stream)
return result; 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;
}

View file

@ -104,4 +104,13 @@ extern int
extern HashMap * extern HashMap *
JsonDecode(FILE *); 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 */ #endif /* TELODENDRIA_JSON_H */