/* * Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren This file is part of CLIXON. CLIXON is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. CLIXON is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with CLIXON; see the file LICENSE. If not, see . * For unit testing compile with -_MAIN: * * JSON support functions. curl -G http://localhost/api/data/sender/userid=a4315f60-e890-4f8f-9a0b-eb53d4da2d3a [{ "sender": { "name": "dk-ore", "userid": "a4315f60-e890-4f8f-9a0b-eb53d4da2d3a", "ipv4_daddr": "109.105.110.78", "template": "nordunet", "version": "0", "description": "Nutanix ORE", "start": "true", "udp_dport": "43713", "debug": "0", "proto": "udp" } is translated into this: [ {"sender": ["name":"dk-ore", "userid":"a4315f60-e890-4f8f-9a0b-eb53d4da2d3a", "ipv4_daddr":"109.105.110.78", "template":"nordunet", "version":"0", "description":"Nutanix ORE", "start":"true", "udp_dport":"43713", "debug":"0", "proto":"udp"} , {"name":"dk-uni", ------------------------- hunerik foo { "t": { "sender": { "name": "hunerik" }, "sender": { "name": "foo" } } } { "t": { "sender": [ { "name": "hunerik" }, { "name": "foo" } ] } } OK, still something wrong with grafana plots */ #include #include #include #include #include #include #include #include #include #include #include /* cligen */ #include /* clixon */ #include "clixon_err.h" #include "clixon_log.h" #include "clixon_xml.h" #include "clixon_json.h" #include "clixon_json_parse.h" #define JSON_INDENT 3 /* maybe we should set this programmatically? */ /*! x is element and has eactly one child which in turn has none * Clone from clixon_xml_map.c */ static int tleaf(cxobj *x) { cxobj *c; if (xml_type(x) != CX_ELMNT) return 0; if (xml_child_nr(x) != 1) return 0; c = xml_child_i(x, 0); return (xml_child_nr(c) == 0); } enum list_element_type{ LIST_NO, LIST_FIRST, LIST_MIDDLE, LIST_LAST }; static enum list_element_type list_eval(cxobj *x) { enum list_element_type list = LIST_NO; cxobj *xp; cxobj *xprev=NULL; cxobj *xnext=NULL; int i; int eqprev=0; int eqnext=0; assert(xml_type(x)==CX_ELMNT); if ((xp = xml_parent(x)) == NULL) goto done; for (i=0; i123 -> {"a":[1,2],"b":3} * ie 132 -> {"a":1,"b":3,"a":2} */ static int xml2json1_cbuf(cbuf *cb, cxobj *x, int level, int pretty) { int retval = -1; int i; cxobj *xc; enum list_element_type list; switch(xml_type(x)){ case CX_BODY: if (xml_value(x)) cprintf(cb, "\"%s\"", xml_value(x)); else cprintf(cb, "null"); break; case CX_ELMNT: list = list_eval(x); switch (list){ case LIST_NO: cprintf(cb, "%*s\"%s\": ", pretty?(level*JSON_INDENT):0, "", xml_name(x)); if (!tleaf(x)) cprintf(cb, "{%s", pretty?"\n":""); break; case LIST_FIRST: cprintf(cb, "%*s\"%s\": [%s", pretty?(level*JSON_INDENT):0, "", xml_name(x), pretty?"\n":""); if (!tleaf(x)){ level++; cprintf(cb, "%*s{%s", pretty?(level*JSON_INDENT):0, "", pretty?"\n":""); } break; case LIST_MIDDLE: case LIST_LAST: level++; cprintf(cb, "%*s", pretty?(level*JSON_INDENT):0, ""); if (!tleaf(x)) cprintf(cb, "{%s", pretty?"\n":""); break; default: break; } for (i=0; i