mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-25 20:28:27 +00:00
Fix #1690 - mg_json_get for keys with the same prefix
This commit is contained in:
1 parent
1c16e95b3a
commit
0661f5921a
3 files changed
+11
-2
No files matched your search
+3
-1
@@ -2565,8 +2565,10 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
|
|||||||
int n = mg_pass_string(&s[i + 1], len - i - 1);
|
int n = mg_pass_string(&s[i + 1], len - i - 1);
|
||||||
if (n < 0) return n;
|
if (n < 0) return n;
|
||||||
if (i + 1 + n >= len) return MG_JSON_NOT_FOUND;
|
if (i + 1 + n >= len) return MG_JSON_NOT_FOUND;
|
||||||
// printf("K[%.*s] %d %d\n", n, &s[i + 1], depth, ed);
|
// printf("K[%.*s] %d %d %d\n", n, &s[i + 1], n, depth, ed);
|
||||||
if (depth == ed && path[pos - 1] == '.' &&
|
if (depth == ed && path[pos - 1] == '.' &&
|
||||||
|
(path[pos + n] == '\0' || path[pos + n] == '.' ||
|
||||||
|
path[pos + n] == '[') &&
|
||||||
strncmp(&s[i + 1], &path[pos], (size_t) n) == 0) {
|
strncmp(&s[i + 1], &path[pos], (size_t) n) == 0) {
|
||||||
pos += n;
|
pos += n;
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -169,8 +169,10 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
|
|||||||
int n = mg_pass_string(&s[i + 1], len - i - 1);
|
int n = mg_pass_string(&s[i + 1], len - i - 1);
|
||||||
if (n < 0) return n;
|
if (n < 0) return n;
|
||||||
if (i + 1 + n >= len) return MG_JSON_NOT_FOUND;
|
if (i + 1 + n >= len) return MG_JSON_NOT_FOUND;
|
||||||
// printf("K[%.*s] %d %d\n", n, &s[i + 1], depth, ed);
|
// printf("K[%.*s] %d %d %d\n", n, &s[i + 1], n, depth, ed);
|
||||||
if (depth == ed && path[pos - 1] == '.' &&
|
if (depth == ed && path[pos - 1] == '.' &&
|
||||||
|
(path[pos + n] == '\0' || path[pos + n] == '.' ||
|
||||||
|
path[pos + n] == '[') &&
|
||||||
strncmp(&s[i + 1], &path[pos], (size_t) n) == 0) {
|
strncmp(&s[i + 1], &path[pos], (size_t) n) == 0) {
|
||||||
pos += n;
|
pos += n;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2490,6 +2490,11 @@ static void test_json(void) {
|
|||||||
ASSERT((str = mg_json_get_hex(json, "$[1]", &len)) != NULL);
|
ASSERT((str = mg_json_get_hex(json, "$[1]", &len)) != NULL);
|
||||||
ASSERT(len == 5 && memcmp(str, "\x01\x00\x02hi", (size_t) len) == 0);
|
ASSERT(len == 5 && memcmp(str, "\x01\x00\x02hi", (size_t) len) == 0);
|
||||||
free(str);
|
free(str);
|
||||||
|
|
||||||
|
json = mg_str("{\"a\":[1,2,3], \"ab\": 2}");
|
||||||
|
ASSERT(mg_json_get_long(json, "$.a[0]", -42) == 1);
|
||||||
|
ASSERT(mg_json_get_long(json, "$.ab", -42) == 2);
|
||||||
|
ASSERT(mg_json_get_long(json, "$.ac", -42) == -42);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user