mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-14 01:38:01 +08:00
Add mg_json_get_long
This commit is contained in:
parent
86e57d243d
commit
c5751b0bd3
@ -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
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user