mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-25 20:28:27 +00:00
Merge pull request #2346 from cesanta/regex
Fix #2345 - json parsing of arrays of objects
This commit is contained in:
3 files changed
+30
-10
No files matched your search
+9
-5
@@ -2596,11 +2596,11 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
|
|||||||
if (i + 1 + n >= len) return MG_JSON_NOT_FOUND;
|
if (i + 1 + n >= len) return MG_JSON_NOT_FOUND;
|
||||||
if (depth < ed) return MG_JSON_NOT_FOUND;
|
if (depth < ed) return MG_JSON_NOT_FOUND;
|
||||||
if (depth == ed && path[pos - 1] != '.') return MG_JSON_NOT_FOUND;
|
if (depth == ed && path[pos - 1] != '.') return MG_JSON_NOT_FOUND;
|
||||||
// printf("K %s [%.*s] [%.*s] %d %d %d\n", path, pos, path, n,
|
// printf("K %s [%.*s] [%.*s] %d %d %d %d %d\n", path, pos, path, n,
|
||||||
// &s[i + 1], n, depth, ed);
|
// &s[i + 1], n, depth, ed, ci, ei);
|
||||||
// NOTE(cpq): in the check sequence below is important.
|
// NOTE(cpq): in the check sequence below is important.
|
||||||
// strncmp() must go first: it fails fast if the remaining length of
|
// strncmp() must go first: it fails fast if the remaining length of
|
||||||
// the path is smaller than `n`.
|
// the path is smaller than `n`.
|
||||||
if (depth == ed && path[pos - 1] == '.' &&
|
if (depth == ed && path[pos - 1] == '.' &&
|
||||||
strncmp(&s[i + 1], &path[pos], (size_t) n) == 0 &&
|
strncmp(&s[i + 1], &path[pos], (size_t) n) == 0 &&
|
||||||
(path[pos + n] == '\0' || path[pos + n] == '.' ||
|
(path[pos + n] == '\0' || path[pos + n] == '.' ||
|
||||||
@@ -2632,6 +2632,10 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
|
|||||||
} else if (c == ',') {
|
} else if (c == ',') {
|
||||||
expecting = (nesting[depth - 1] == '{') ? S_KEY : S_VALUE;
|
expecting = (nesting[depth - 1] == '{') ? S_KEY : S_VALUE;
|
||||||
} else if (c == ']' || c == '}') {
|
} else if (c == ']' || c == '}') {
|
||||||
|
if (depth == ed && c == '}' && path[pos - 1] == '.')
|
||||||
|
return MG_JSON_NOT_FOUND;
|
||||||
|
if (depth == ed && c == ']' && path[pos - 1] == ',')
|
||||||
|
return MG_JSON_NOT_FOUND;
|
||||||
MG_EOO('O');
|
MG_EOO('O');
|
||||||
if (depth == ed && ei >= 0) ci++;
|
if (depth == ed && ei >= 0) ci++;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+9
-5
@@ -166,11 +166,11 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
|
|||||||
if (i + 1 + n >= len) return MG_JSON_NOT_FOUND;
|
if (i + 1 + n >= len) return MG_JSON_NOT_FOUND;
|
||||||
if (depth < ed) return MG_JSON_NOT_FOUND;
|
if (depth < ed) return MG_JSON_NOT_FOUND;
|
||||||
if (depth == ed && path[pos - 1] != '.') return MG_JSON_NOT_FOUND;
|
if (depth == ed && path[pos - 1] != '.') return MG_JSON_NOT_FOUND;
|
||||||
// printf("K %s [%.*s] [%.*s] %d %d %d\n", path, pos, path, n,
|
// printf("K %s [%.*s] [%.*s] %d %d %d %d %d\n", path, pos, path, n,
|
||||||
// &s[i + 1], n, depth, ed);
|
// &s[i + 1], n, depth, ed, ci, ei);
|
||||||
// NOTE(cpq): in the check sequence below is important.
|
// NOTE(cpq): in the check sequence below is important.
|
||||||
// strncmp() must go first: it fails fast if the remaining length of
|
// strncmp() must go first: it fails fast if the remaining length of
|
||||||
// the path is smaller than `n`.
|
// the path is smaller than `n`.
|
||||||
if (depth == ed && path[pos - 1] == '.' &&
|
if (depth == ed && path[pos - 1] == '.' &&
|
||||||
strncmp(&s[i + 1], &path[pos], (size_t) n) == 0 &&
|
strncmp(&s[i + 1], &path[pos], (size_t) n) == 0 &&
|
||||||
(path[pos + n] == '\0' || path[pos + n] == '.' ||
|
(path[pos + n] == '\0' || path[pos + n] == '.' ||
|
||||||
@@ -202,6 +202,10 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
|
|||||||
} else if (c == ',') {
|
} else if (c == ',') {
|
||||||
expecting = (nesting[depth - 1] == '{') ? S_KEY : S_VALUE;
|
expecting = (nesting[depth - 1] == '{') ? S_KEY : S_VALUE;
|
||||||
} else if (c == ']' || c == '}') {
|
} else if (c == ']' || c == '}') {
|
||||||
|
if (depth == ed && c == '}' && path[pos - 1] == '.')
|
||||||
|
return MG_JSON_NOT_FOUND;
|
||||||
|
if (depth == ed && c == ']' && path[pos - 1] == ',')
|
||||||
|
return MG_JSON_NOT_FOUND;
|
||||||
MG_EOO('O');
|
MG_EOO('O');
|
||||||
if (depth == ed && ei >= 0) ci++;
|
if (depth == ed && ei >= 0) ci++;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -2939,6 +2939,18 @@ static void test_json(void) {
|
|||||||
ASSERT(mg_json_get_long(mg_str("[0, 42]"), "$[1]", 0) == 42);
|
ASSERT(mg_json_get_long(mg_str("[0, 42]"), "$[1]", 0) == 42);
|
||||||
ASSERT(mg_json_get_long(mg_str("[[], 42]"), "$[1]", 0) == 42);
|
ASSERT(mg_json_get_long(mg_str("[[], 42]"), "$[1]", 0) == 42);
|
||||||
ASSERT(mg_json_get_long(mg_str("[{}, 42]"), "$[1]", 0) == 42);
|
ASSERT(mg_json_get_long(mg_str("[{}, 42]"), "$[1]", 0) == 42);
|
||||||
|
|
||||||
|
json = mg_str("[{\"a\":1},{\"a\":2}]");
|
||||||
|
ASSERT(mg_json_get_long(json, "$[0]", -1) == -1);
|
||||||
|
ASSERT(mg_json_get_long(json, "$[0].a", -1) == 1);
|
||||||
|
ASSERT(mg_json_get_long(json, "$[1].a", -1) == 2);
|
||||||
|
ASSERT(mg_json_get_long(json, "$[2].a", -1) == -1);
|
||||||
|
|
||||||
|
json = mg_str("[{\"a1\":1},{\"a\":2}]");
|
||||||
|
ASSERT(mg_json_get_long(json, "$[0]", -1) == -1);
|
||||||
|
ASSERT(mg_json_get_long(json, "$[0].a", -1) == -1);
|
||||||
|
ASSERT(mg_json_get_long(json, "$[1].a", -1) == 2);
|
||||||
|
ASSERT(mg_json_get_long(json, "$[2].a", -1) == -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void resp_rpc(struct mg_rpc_req *r) {
|
static void resp_rpc(struct mg_rpc_req *r) {
|
||||||
|
|||||||
Reference in new issue
Block a user