--- libelwix/example/test_json.c 2021/03/21 01:32:04 1.4 +++ libelwix/example/test_json.c 2024/12/04 17:46:50 1.4.38.1 @@ -11,7 +11,7 @@ int run_simple(json_t *json) { static const char *teststr = "{\"user\": \"johndoe\", { \"meow\", \"\", \"\aaa\"}, \"admin\": false, \"uid\": 1000, \"objects\" : { \"a\":\"1\", \"b\" : \"2\" ,\"c\" : \"3\", , \"oho\"},\n" - "\"groups\": [\"users\", \"\", \"wheel\", \"audio\", \"video\"], \"testend\":{\"x\" : \"8\", \"y\":\"7\", \"z\" : \"9\"}}"; + "\"groups\": [\"users\", \"\", \"wheel\", \"audio\", \"video\"], \"testend\":{\"x\" : \"8\", \"y\":\"7\", \"z\" : \"9\"}}"; // static const char *teststr = "{}\n[]\n\"sdfsdf\""; int i, ret; jtok_t *tok, toks[36]; @@ -262,7 +262,7 @@ run_stdin(json_t *json) { char szJSON[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; jtok_t *tok, toks[1024]; - int ret; + int ret, scope; char *str; printf("Input JSON> "); @@ -287,6 +287,38 @@ run_stdin(json_t *json) json_freestr(str); } } + + printf("Default scope=%ld, scope for \"zzz\"=%ld\n", + json_objscope(NULL, szJSON, toks, ret), + (scope = json_objscope("zzz", szJSON, toks, ret))); + + tok = json_findbykeyatscope(0, szJSON, "t1", J_UNDEF, toks, ret); + if (tok) { + str = json_token2str(szJSON, tok); + if (str) { + printf("default scope t1=%s\n", str); + json_freestr(str); + } + } + tok = json_findbykeyatscope(scope, szJSON, "t1", J_UNDEF, toks, ret); + if (tok) { + str = json_token2str(szJSON, tok); + if (str) { + printf("scope=%ld object zzz t1=%s\n", scope, str); + json_freestr(str); + } + } + + scope = json_objscope("z", szJSON, toks, ret); + tok = json_findbykeyatscope(scope, szJSON, "t1", J_UNDEF, toks, ret); + if (tok) { + str = json_token2str(szJSON, tok); + if (str) { + printf("scope=%ld object z t1=%s\n", scope, str); + json_freestr(str); + } + } + return 0; }