aboutsummaryrefslogtreecommitdiff
path: root/jim-json.c
diff options
context:
space:
mode:
authordbohdan <dbohdan@dbohdan.com>2019-11-09 19:54:10 +0000
committerdbohdan <dbohdan@dbohdan.com>2019-11-11 05:00:23 +0000
commitea69c35ddd6842bca86711e504968236b70cf426 (patch)
treeb610be02f6d893d6c10fe712a944165338b02b00 /jim-json.c
parent7bb4dffaa22b9d90982a561f464d54a7f20c2032 (diff)
downloadjimtcl-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.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/jim-json.c b/jim-json.c
index 0a47d4d..1b3869c 100644
--- a/jim-json.c
+++ b/jim-json.c
@@ -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;
}