Skip to content

Commit 0435223

Browse files
committed
get_strlen: Add APIs to get the length of a JSON string value
json_obj_get_strlen and json_arr_get_strlen added Signed-off-by: Piyush Shah <[email protected]>
1 parent 4c8d60f commit 0435223

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

include/json_parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ int json_obj_get_int(jparse_ctx_t *jctx, char *name, int *val);
3131
int json_obj_get_int64(jparse_ctx_t *jctx, char *name, int64_t *val);
3232
int json_obj_get_float(jparse_ctx_t *jctx, char *name, float *val);
3333
int json_obj_get_string(jparse_ctx_t *jctx, char *name, char *val, int size);
34+
int json_obj_get_strlen(jparse_ctx_t *jctx, char *name, int *strlen);
3435

3536
int json_arr_get_array(jparse_ctx_t *jctx, uint32_t index);
3637
int json_arr_leave_array(jparse_ctx_t *jctx);
@@ -41,5 +42,6 @@ int json_arr_get_int(jparse_ctx_t *jctx, uint32_t index, int *val);
4142
int json_arr_get_int64(jparse_ctx_t *jctx, uint32_t index, int64_t *val);
4243
int json_arr_get_float(jparse_ctx_t *jctx, uint32_t index, float *val);
4344
int json_arr_get_string(jparse_ctx_t *jctx, uint32_t index, char *val, int size);
45+
int json_arr_get_strlen(jparse_ctx_t *jctx, uint32_t index, int *strlen);
4446

4547
#endif /* _JSON_PARSER_H_ */

src/json_parser.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ int json_obj_get_string(jparse_ctx_t *jctx, char *name, char *val, int size)
199199
return json_tok_to_string(jctx, tok, val, size);
200200
}
201201

202+
int json_obj_get_strlen(jparse_ctx_t *jctx, char *name, int *strlen)
203+
{
204+
json_tok_t *tok = json_obj_get_val_tok(jctx, name, JSMN_STRING);
205+
if (!tok)
206+
return -OS_FAIL;
207+
*strlen = tok->end - tok->start + 1;
208+
return OS_SUCCESS;
209+
}
210+
202211
static json_tok_t *json_arr_search(jparse_ctx_t *ctx, uint32_t index)
203212
{
204213
json_tok_t *tok = ctx->cur;
@@ -298,6 +307,15 @@ int json_arr_get_string(jparse_ctx_t *jctx, uint32_t index, char *val, int size)
298307
return json_tok_to_string(jctx, tok, val, size);
299308
}
300309

310+
int json_arr_get_strlen(jparse_ctx_t *jctx, uint32_t index, int *strlen)
311+
{
312+
json_tok_t *tok = json_arr_get_val_tok(jctx, index, JSMN_STRING);
313+
if (!tok)
314+
return -OS_FAIL;
315+
*strlen = tok->end - tok->start + 1;
316+
return OS_SUCCESS;
317+
}
318+
301319
int json_parse_start(jparse_ctx_t *jctx, char *js, int len)
302320
{
303321
memset(jctx, 0, sizeof(jparse_ctx_t));

0 commit comments

Comments
 (0)