From df492da1804b14e53a56a3dd6a3b8cc8fb1e2d78 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Wed, 29 Aug 2012 12:32:54 +0200 Subject: implement ACT_JSON_PASSING NOTE: this is only compile-tested! For real testing, a module using that mode is required. Will be done as next steps. --- template.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'template.c') diff --git a/template.c b/template.c index 768608f1..a3d2b8d6 100644 --- a/template.c +++ b/template.c @@ -34,6 +34,7 @@ #include #include #include +#include #include "stringbuf.h" #include "syslogd-types.h" #include "template.h" @@ -270,6 +271,50 @@ finalize_it: } +/* This functions converts a template into a json object. + * For further general details, see the very similar funtion + * tpltoString(). + * rgerhards, 2012-08-29 + */ +rsRetVal tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson) +{ + struct templateEntry *pTpe; + char *cstr; + size_t propLen; + unsigned short bMustBeFreed; + uchar *pVal; + struct json_object *json, *jsonf; + DEFiRet; + + assert(pTpl != NULL); + assert(pMsg != NULL); + assert(json != NULL); + + json = json_object_new_object(); + for(pTpe = pTpl->pEntryRoot ; pTpe != NULL ; pTpe = pTpe->pNext) { + if(pTpe->eEntryType == CONSTANT) { + if(pTpe->fieldName == NULL) + continue; + jsonf = json_object_new_string((char*) pTpe->data.constant.pConstant); + } else if(pTpe->eEntryType == FIELD) { + pVal = (uchar*) MsgGetProp(pMsg, pTpe, pTpe->data.field.propid, + pTpe->data.field.propName, &propLen, &bMustBeFreed); + jsonf = json_object_new_string_len((char*)pVal, propLen); + if(bMustBeFreed) { /* json-c makes its own private copy! */ + free(pVal); + } + } + /* TODO: unify strings, handling currently quite inefficient! */ + cstr = es_str2cstr(pTpe->fieldName, NULL); + json_object_object_add(json, cstr, jsonf); + free(cstr); + } + + *pjson = (iRet == RS_RET_OK) ? json : NULL; + RETiRet; +} + + /* Helper to doEscape. This is called if doEscape * runs out of memory allocating the escaped string. * Then we are in trouble. We can -- cgit v1.2.3