diff --git a/docs/README.md b/docs/README.md index c7cd32a5..4f5e39f2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2820,6 +2820,29 @@ mg_json_get_bool(mg_str("[123]", "$[0]", &b)); // Error. b remains to be false mg_json_get_bool(mg_str("[true]", "$[0]", &b)); // b is true ``` +### mg\_json\_get\_long() + +```c +long mg_json_get_bool(struct mg_str json, const char *path, long default_val); +``` + +Fetch integer numeric (long) value from the json string `json` at JSON path +`path`. Return it if found, or `default_val` if not found. + +Parameters: +- `json` - a string containing valid JSON +- `path` - a JSON path. Must start with `$` +- `default_val` - a default value for the failure case + +Return value: found value, or `default_val` value + +Usage example: + +```c +long a = mg_json_get_bool(mg_str("[123]", "$a", -1)); // a = -1 +long b = mg_json_get_bool(mg_str("[123]", "$[0]", -1)); // b = 123 +``` + ### mg\_json\_get\_str() ```c diff --git a/mongoose.c b/mongoose.c index 8bd317b0..ada49653 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2695,6 +2695,13 @@ char *mg_json_get_hex(struct mg_str json, const char *path, int *len) { return result; } +long mg_json_get_long(struct mg_str json, const char *path, long dflt) { + double dv; + long result = dflt; + if (mg_json_get_num(json, path, &dv)) result = (long) dv; + return result; +} + #ifdef MG_ENABLE_LINES #line 1 "src/log.c" #endif diff --git a/mongoose.h b/mongoose.h index a0759dad..9c0382d7 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1320,6 +1320,7 @@ int mg_json_get(const char *buf, int len, const char *path, int *toklen); 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); char *mg_json_get_str(struct mg_str json, const char *path); char *mg_json_get_hex(struct mg_str json, const char *path, int *len); char *mg_json_get_b64(struct mg_str json, const char *path, int *len); diff --git a/src/json.c b/src/json.c index d6b736a7..4c960647 100644 --- a/src/json.c +++ b/src/json.c @@ -245,3 +245,10 @@ char *mg_json_get_hex(struct mg_str json, const char *path, int *len) { } return result; } + +long mg_json_get_long(struct mg_str json, const char *path, long dflt) { + double dv; + long result = dflt; + if (mg_json_get_num(json, path, &dv)) result = (long) dv; + return result; +} diff --git a/src/json.h b/src/json.h index 45c9b415..b5d1161e 100644 --- a/src/json.h +++ b/src/json.h @@ -13,6 +13,7 @@ int mg_json_get(const char *buf, int len, const char *path, int *toklen); 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); char *mg_json_get_str(struct mg_str json, const char *path); char *mg_json_get_hex(struct mg_str json, const char *path, int *len); char *mg_json_get_b64(struct mg_str json, const char *path, int *len); diff --git a/test/unit_test.c b/test/unit_test.c index 7a2cd3af..9a4b2b03 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -2373,6 +2373,9 @@ static void test_json(void) { ASSERT(strcmp(str, "hi\nthere") == 0); free(str); + ASSERT(mg_json_get_long(mg_str(json), "$.foo", -42) == -42); + ASSERT(mg_json_get_long(mg_str(json), "$.b[0]", -42) == 12345); + ASSERT(mg_json_get_num(mg_str(json), "$.a", &d) == false); ASSERT(mg_json_get_num(mg_str(json), "$.c", &d) == false); ASSERT(mg_json_get_num(mg_str(json), "$.b[0]", &d) == true);