3
* Supports creating an event out of an JSON string.
4
*//* Libee - An Event Expression Library inspired by CEE
5
* Copyright 2012 by Rainer Gerhards and Adiscon GmbH.
7
* This file is part of libee.
9
* This library is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU Lesser General Public
11
* License as published by the Free Software Foundation; either
12
* version 2.1 of the License, or (at your option) any later version.
14
* This library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
* Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
* A copy of the LGPL v2.1 can be found in the file "COPYING" in this distribution.
29
#include "libee/libee.h"
30
#include "libee/fieldbucket.h"
31
#include "cjson/cjson.h"
34
callback(struct ee_fieldbucket *fields, char *name,int type,cJSON *item)
41
//printf("callback: type %d, name %s\n", type, name);
42
if(type == cJSON_Object)
43
return 1; // TODO: support!
45
if(type == cJSON_String) {
46
valstr = item->valuestring;
47
} else if(type == cJSON_Number) {
48
valstr = cJSON_print_number(item);
49
} else if(type == cJSON_NULL) {
51
} else if(type == cJSON_False) {
53
} else if(type == cJSON_True) {
56
//printf("callback: string value %s\n", valstr);
58
estr = es_newStrFromCStr(valstr, strlen(valstr));
59
val = ee_newValue(fields->ctx);
60
ee_setStrValue(val, estr);
61
f = ee_newFieldFromNV(fields->ctx, name, val);
62
ee_addFieldToBucket(fields, f);
64
if(type == cJSON_Number)
70
void parse_and_callback(struct ee_fieldbucket *fields, cJSON *item, char *prefix)
77
//printf("parse_and_callback, item %p, item->string %p, prefix(%d): '%s'\n", item, item->string, strlen(prefix), prefix);
80
lenprefix = strlen(prefix);
82
newprefix = (item->string == NULL) ? "" : item->string;
85
name = (item->string == NULL) ? "*" : item->string;
86
newprefix=malloc(strlen(prefix)+strlen(name)+2);
87
sprintf(newprefix,"%s.%s",prefix,name);
90
dorecurse = callback(fields, newprefix, item->type, item);
91
if (item->child && dorecurse)
92
parse_and_callback(fields, item->child,newprefix);
100
ee_newEventFromJSON(ee_ctx ctx, char *str)
103
struct ee_event *e = NULL;
105
json = cJSON_Parse(str);
108
e = ee_newEvent(ctx);
109
e->fields = ee_newFieldbucket(ctx);
110
parse_and_callback(e->fields, json, "");