X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcore%2Fstore_json.c;h=a772dc39c8f67549c8205a0f63527672b6edf473;hb=1426e3fcf927d6321fd2cf8595394f817bb1a0e2;hp=b4b7b6a8af4b1becfb02431e43188325355f29b4;hpb=fa16153fd58a0a5c36037c81119072f1a0623e48;p=sysdb.git diff --git a/src/core/store_json.c b/src/core/store_json.c index b4b7b6a..a772dc3 100644 --- a/src/core/store_json.c +++ b/src/core/store_json.c @@ -39,7 +39,9 @@ #include +#include #include +#include /* * private data types @@ -53,6 +55,10 @@ struct sdb_store_json_formatter { int context[8]; size_t current; + /* The current host context when processing non-host objects */ + sdb_store_obj_t *current_host; + + int type; int flags; }; @@ -60,22 +66,77 @@ struct sdb_store_json_formatter { * private helper functions */ +static void +escape_string(const char *src, char *dest) +{ + size_t i = 1; + dest[0] = '"'; + for ( ; *src; ++src) { + char c = *src; + if ((c == '"') || (c == '\\') || iscntrl((int)c)) { + dest[i] = '\\'; + ++i; + } + switch (c) { + case '\a': dest[i] = 'a'; break; + case '\b': dest[i] = 'b'; break; + case '\t': dest[i] = 't'; break; + case '\n': dest[i] = 'n'; break; + case '\v': dest[i] = 'v'; break; + case '\f': dest[i] = 'f'; break; + case '\r': dest[i] = 'r'; break; + default: dest[i] = c; break; + } + ++i; + } + dest[i] = '"'; + dest[i + 1] = '\0'; +} /* escape_string */ + static int json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) { char time_str[64]; char interval_str[64]; + char name[2 * strlen(SDB_OBJ(obj)->name) + 3]; size_t i; assert(f && obj); - sdb_strbuf_append(f->buf, "{\"name\": \"%s\", ", SDB_OBJ(obj)->name); + if ((f->type != SDB_HOST) && (f->type == obj->type)) { + /* create the host for the current entry first */ + assert(obj->parent && (obj->parent->type == SDB_HOST)); + if (f->current_host != obj->parent) { + json_emit(f, obj->parent); + sdb_strbuf_append(f->buf, ", \"%ss\": [", + SDB_STORE_TYPE_TO_NAME(obj->type)); + f->current_host = obj->parent; + } + } + + escape_string(SDB_OBJ(obj)->name, name); + sdb_strbuf_append(f->buf, "{\"name\": %s, ", name); if (obj->type == SDB_ATTRIBUTE) { char tmp[sdb_data_strlen(&ATTR(obj)->value) + 1]; - if (sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp), - SDB_DOUBLE_QUOTED) < 0) + char val[2 * sizeof(tmp) + 3]; + if (! sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp), + SDB_DOUBLE_QUOTED)) snprintf(tmp, sizeof(tmp), ""); - sdb_strbuf_append(f->buf, "\"value\": %s, ", tmp); + + if (tmp[0] == '"') { + /* a string; escape_string handles quoting */ + tmp[strlen(tmp) - 1] = '\0'; + escape_string(tmp + 1, val); + sdb_strbuf_append(f->buf, "\"value\": %s, ", val); + } + else + sdb_strbuf_append(f->buf, "\"value\": %s, ", tmp); + } + else if (obj->type == SDB_METRIC) { + if (METRIC(obj)->store.type != NULL) + sdb_strbuf_append(f->buf, "\"timeseries\": true, "); + else + sdb_strbuf_append(f->buf, "\"timeseries\": false, "); } /* TODO: make time and interval formats configurable */ @@ -107,13 +168,16 @@ json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) */ sdb_store_json_formatter_t * -sdb_store_json_formatter(sdb_strbuf_t *buf, int flags) +sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags) { sdb_store_json_formatter_t *f; if (! buf) return NULL; + if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) + return NULL; + f = calloc(1, sizeof(*f)); if (! f) return NULL; @@ -121,6 +185,10 @@ sdb_store_json_formatter(sdb_strbuf_t *buf, int flags) f->buf = buf; f->context[0] = 0; f->current = 0; + + f->current_host = NULL; + + f->type = type; f->flags = flags; return f; } /* sdb_store_json_formatter */ @@ -131,15 +199,43 @@ sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) if ((! f) || (! obj)) return -1; - /* first host */ + if ((f->type != SDB_HOST) && (obj->type == SDB_HOST)) { + sdb_log(SDB_LOG_ERR, "store: Unexpected object of type host " + "during %s JSON serialization", + SDB_STORE_TYPE_TO_NAME(f->type)); + return -1; + } + + /* first top-level object */ if (! f->context[0]) { + if (obj->type != f->type) { + sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s " + "as the first element during %s JSON serialization", + SDB_STORE_TYPE_TO_NAME(obj->type), + SDB_STORE_TYPE_TO_NAME(f->type)); + return -1; + } if (f->flags & SDB_WANT_ARRAY) sdb_strbuf_append(f->buf, "["); assert(f->current == 0); - f->context[0] = SDB_HOST; + f->context[0] = obj->type; return json_emit(f, obj); } + if ((f->current >= 1) && (obj->type != SDB_ATTRIBUTE)) { + /* new entry of a previous type or a new type on the same level; + * rewind to the right state */ + while (f->current > 0) { + if (f->context[f->current] == obj->type) + break; + sdb_strbuf_append(f->buf, "}]"); + --f->current; + } + } + if ((obj->type == f->type) && (f->type != SDB_HOST)) + if (obj->parent != f->current_host) + sdb_strbuf_append(f->buf, "}]"); + if (obj->type == f->context[f->current]) { /* new entry of the same type */ sdb_strbuf_append(f->buf, "},"); @@ -153,19 +249,6 @@ sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) SDB_STORE_TYPE_TO_NAME(obj->type)); ++f->current; } - else if (f->current >= 1) { - /* new entry of a previous type or a new type on the same level - * -> rewind to the right state and then handle the new object */ - assert(obj->type != SDB_ATTRIBUTE); - while (f->current > 0) { - if (f->context[f->current] == obj->type) - break; - assert(f->context[f->current] != SDB_HOST); - sdb_strbuf_append(f->buf, "}]"); - --f->current; - } - return sdb_store_json_emit(f, obj); - } else { sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s " "on level %zu during JSON serialization", @@ -244,8 +327,13 @@ sdb_store_json_finish(sdb_store_json_formatter_t *f) sdb_strbuf_append(f->buf, "}]"); --f->current; } + if (f->context[0] != SDB_HOST) { + assert(f->type != SDB_HOST); + sdb_strbuf_append(f->buf, "}]}"); + } + else + sdb_strbuf_append(f->buf, "}"); - sdb_strbuf_append(f->buf, "}"); if (f->flags & SDB_WANT_ARRAY) sdb_strbuf_append(f->buf, "]"); return 0;