mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-23 20:11:49 +00:00
Merge pull request #1698 from oakfire/master
Fix mg_json_get for the array element result not as expected.
This commit is contained in:
2 files changed
+5
No files matched your search
@@ -2602,6 +2602,7 @@ 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 && ei >= 0) ci--; // Array ends, as ci is pre-added by 1, needs to be decremented before MG_EOO
|
||||||
MG_EOO('O');
|
MG_EOO('O');
|
||||||
if (depth == ed && ei >= 0) ci++;
|
if (depth == ed && ei >= 0) ci++;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -2508,6 +2508,10 @@ static void test_json(void) {
|
|||||||
ASSERT(mg_json_get_long(json, "$[1].b", -42) == 4);
|
ASSERT(mg_json_get_long(json, "$[1].b", -42) == 4);
|
||||||
ASSERT(mg_json_get_long(json, "$[2].a", -42) == -42);
|
ASSERT(mg_json_get_long(json, "$[2].a", -42) == -42);
|
||||||
|
|
||||||
|
json = mg_str("{\"a\":[1],\"b\":[2,3]}");
|
||||||
|
ASSERT(mg_json_get_long(json, "$.a[0]", -42) == 1);
|
||||||
|
ASSERT(mg_json_get_long(json, "$.a[1]", -42) == -42);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user