diff options
author | Bjørn Mork <bjorn@mork.no> | 2019-05-25 18:20:35 +0200 |
---|---|---|
committer | Bjørn Mork <bjorn@mork.no> | 2019-05-25 18:20:35 +0200 |
commit | 099797fc735bf784a8f8dc82407c9b786594c895 (patch) | |
tree | 90334232d0e4b7f7f41c59c786545c939756fb20 | |
parent | 230a6c779a9193e3d3a63eb6020808d3965d7709 (diff) |
simplify array handling
Signed-off-by: Bjørn Mork <bjorn@mork.no>
-rw-r--r-- | obinsectd.c | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/obinsectd.c b/obinsectd.c index 2288e3c..c785c29 100644 --- a/obinsectd.c +++ b/obinsectd.c @@ -1418,7 +1418,7 @@ static void set_publish(json_object *pub, const char *key, json_object *arraynam static void process_cfg(json_object *cfg, char *buf, size_t bufsize) { - json_object *topics, *array, *publish, *arrayname; + json_object *topics, *publish, *arrayname; int i, idx = 0; const char *name; @@ -1429,32 +1429,29 @@ static void process_cfg(json_object *cfg, char *buf, size_t bufsize) /* unconditionally add parent nodes */ publish = json_object_new_object(); json_object_object_add(cfg, "publish", publish); - array = json_object_new_object(); - json_object_object_add(cfg, "arrays", array); /* create a flat set of all keys to be published */ json_object_object_foreach(topics, t, tmp) { /* tmp is one of three different types: OBIS code, symbolic alias, or array */ + debug("looking at topic '%s'\n", t); if (json_object_is_type(tmp, json_type_array)) { /* 1. move to arrays */ idx++; snprintf(buf, bufsize, "__array_%u", idx); - json_object_object_add(array, buf, json_object_get(tmp)); arrayname = json_object_new_string(buf); /* 2. add keys to array */ for (i = 0; i < json_object_array_length(tmp); i++) { name = json_object_get_string(json_object_array_get_idx(tmp, i)); + /* prevent morons from nesting */ + if (!strncmp(name, "__array",7)) + continue; set_publish(publish, name, arrayname); } /* 3. replace the original topic value with the array name */ - name = strdup(t); - json_object_object_del(topics, t); - json_object_object_add(topics, name, arrayname); - free((char *)name); - + json_object_object_add(topics, t, arrayname); } else { name = json_object_get_string(tmp); set_publish(publish, name, NULL); |