Add helper function mg_json_get_tok

This commit is contained in:
Sergey Lyubka committed 2024-02-01 12:05:49 +00:00
1 parent ebfa5f0ba7
commit f231760395
4 files changed
+300 -285

No files matched your search

+7 -1
View File
@@ -111,7 +111,7 @@ size_t mg_json_next(struct mg_str obj, size_t ofs, struct mg_str *key,
}
}
}
//MG_INFO(("SUB ofs %u %.*s", ofs, sub.len, sub.ptr));
// MG_INFO(("SUB ofs %u %.*s", ofs, sub.len, sub.ptr));
while (ofs && ofs < obj.len &&
(obj.ptr[ofs] == ' ' || obj.ptr[ofs] == '\t' ||
obj.ptr[ofs] == '\n' || obj.ptr[ofs] == '\r')) {
@@ -266,6 +266,12 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
return MG_JSON_NOT_FOUND;
}
struct mg_str mg_json_get_tok(struct mg_str json, const char *path) {
int len = 0, ofs = mg_json_get(json, path, &len);
return mg_str_n(ofs < 0 ? NULL : json.ptr + ofs,
(size_t) (len < 0 ? 0 : len));
}
bool mg_json_get_num(struct mg_str json, const char *path, double *v) {
int n, toklen, found = 0;
if ((n = mg_json_get(json, path, &toklen)) >= 0 &&
+1
View File
@@ -11,6 +11,7 @@
enum { MG_JSON_TOO_DEEP = -1, MG_JSON_INVALID = -2, MG_JSON_NOT_FOUND = -3 };
int mg_json_get(struct mg_str json, const char *path, int *toklen);
struct mg_str mg_json_get_tok(struct mg_str json, const char *path);
bool mg_json_get_num(struct mg_str json, const char *path, double *v);
bool mg_json_get_bool(struct mg_str json, const char *path, bool *v);
long mg_json_get_long(struct mg_str json, const char *path, long dflt);