MCPcopy Create free account
hub / github.com/MariaDB/server / json_locate_key

Function json_locate_key

strings/json_lib.c:2315–2385  ·  view source on GitHub ↗

Expects the JSON object as an js argument, and the key name. Looks for this key in the object and returns the location of all the text related to it. The text includes the comma, separating this key. comma_pos - the hint where the comma is. It is important if you plan to replace the key rather than just cut. 1 - comma is on the left 2 - comma is on the right. 0 - no

Source from the content-addressed store, hash-verified

2313 if no such key found *key_start is set to NULL.
2314*/
2315int json_locate_key(json_engine_t *je, const char *js, const char *js_end,
2316 const char *kname,
2317 const char **key_start, const char **key_end,
2318 int *comma_pos)
2319{
2320 const char *kname_end= kname + strlen(kname);
2321 json_string_t key_name;
2322 int t_next, c_len, match_result;
2323
2324 json_string_set_cs(&key_name, &my_charset_utf8mb4_bin);
2325
2326 json_scan_start(je, &my_charset_utf8mb4_bin,(const uchar *) js,
2327 (const uchar *) js_end);
2328
2329 if (json_read_value(je) ||
2330 je->value_type != JSON_VALUE_OBJECT)
2331 goto err_return;
2332
2333 *key_start= (const char *) je->s.c_str;
2334 *comma_pos= 0;
2335
2336 while (!json_scan_next(je))
2337 {
2338 switch (je->state)
2339 {
2340 case JST_KEY:
2341 json_string_set_str(&key_name, (const uchar *) kname,
2342 (const uchar *) kname_end);
2343 match_result= json_key_matches(je, &key_name);
2344 if (json_skip_key(je))
2345 goto err_return;
2346 get_first_nonspace(&je->s, &t_next, &c_len);
2347 je->s.c_str-= c_len;
2348
2349 if (match_result)
2350 {
2351 *key_end= (const char *) je->s.c_str;
2352
2353 if (*comma_pos == 1)
2354 {
2355 return 0;
2356 }
2357
2358 DBUG_ASSERT(*comma_pos == 0);
2359
2360 if (t_next == C_COMMA)
2361 {
2362 *key_end+= c_len;
2363 *comma_pos= 2;
2364 }
2365 else if (t_next == C_RCURB)
2366 *comma_pos= 0;
2367 else
2368 goto err_return;
2369 return 0;
2370 }
2371
2372 *key_start= (const char *) je->s.c_str;

Callers 1

do_json_locateFunction · 0.85

Calls 8

json_string_set_csFunction · 0.85
json_scan_startFunction · 0.85
json_read_valueFunction · 0.85
json_scan_nextFunction · 0.85
json_string_set_strFunction · 0.85
json_key_matchesFunction · 0.85
json_skip_keyFunction · 0.85
get_first_nonspaceFunction · 0.85

Tested by

no test coverage detected