diff options
author | dbohdan <dbohdan@dbohdan.com> | 2019-11-09 19:54:10 +0000 |
---|---|---|
committer | dbohdan <dbohdan@dbohdan.com> | 2019-11-11 05:00:23 +0000 |
commit | ea69c35ddd6842bca86711e504968236b70cf426 (patch) | |
tree | b610be02f6d893d6c10fe712a944165338b02b00 /jim-json.c | |
parent | 7bb4dffaa22b9d90982a561f464d54a7f20c2032 (diff) | |
download | jimtcl-ea69c35ddd6842bca86711e504968236b70cf426.zip jimtcl-ea69c35ddd6842bca86711e504968236b70cf426.tar.gz jimtcl-ea69c35ddd6842bca86711e504968236b70cf426.tar.bz2 |
json: implement -index decode option
Diffstat (limited to 'jim-json.c')
-rw-r--r-- | jim-json.c | 15 |
1 files changed, 12 insertions, 3 deletions
@@ -44,6 +44,7 @@ struct json_state { int need_subst; /* The following are used for -schema */ int enable_schema; + int enable_index; Jim_Obj *schemaObj; Jim_Obj *schemaTypeObj[JSON_MAX_TYPE]; }; @@ -183,6 +184,10 @@ json_decode_dump_container(Jim_Interp *interp, struct json_state *state) json_decode_dump_value(interp, state, list); } + if (state->enable_index && type == JSMN_ARRAY) { + Jim_ListAppendElement(interp, list, Jim_NewIntObj(interp, i)); + } + if (state->schemaObj && container_type != JSON_LIST) { if (state->tok->type == JSMN_STRING || state->tok->type == JSMN_PRIMITIVE) { json_decode_add_schema_type(interp, state, json_decode_get_type(state->tok, state->json)); @@ -245,8 +250,8 @@ json_decode_dump_value(Jim_Interp *interp, struct json_state *state, Jim_Obj *li */ static int parse_json_decode_options(Jim_Interp *interp, int argc, Jim_Obj *const argv[], struct json_state *state) { - static const char * const options[] = { "-null", "-schema", NULL }; - enum { OPT_NULL, OPT_SCHEMA, }; + static const char * const options[] = { "-index", "-null", "-schema", NULL }; + enum { OPT_INDEX, OPT_NULL, OPT_SCHEMA, }; int i; for (i = 1; i < argc - 1; i++) { @@ -255,6 +260,10 @@ static int parse_json_decode_options(Jim_Interp *interp, int argc, Jim_Obj *cons return JIM_ERR; } switch (option) { + case OPT_INDEX: + state->enable_index = 1; + break; + case OPT_NULL: i++; Jim_IncrRefCount(argv[i]); @@ -270,7 +279,7 @@ static int parse_json_decode_options(Jim_Interp *interp, int argc, Jim_Obj *cons if (i != argc - 1) { Jim_WrongNumArgs(interp, 1, argv, - "?-null nullvalue? ?-schema? json"); + "?-index? ?-null nullvalue? ?-schema? json"); return JIM_ERR; } |