CliXoN
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
Macros | Enumerations | Functions
clixon_json.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <fnmatch.h>
#include <stdint.h>
#include <syslog.h>
#include <assert.h>
#include <cligen/cligen.h>
#include "clixon_err.h"
#include "clixon_log.h"
#include "clixon_xml.h"
#include "clixon_json.h"
#include "clixon_json_parse.h"

Macros

#define JSON_INDENT   2 /* maybe we should set this programmatically? */
 
#define VEC_ARRAY   1
 

Enumerations

enum  array_element_type {
  NO_ARRAY =0, FIRST_ARRAY, MIDDLE_ARRAY, LAST_ARRAY,
  BODY_ARRAY
}
 
enum  childtype { NULL_CHILD =0, BODY_CHILD, ANY_CHILD }
 

Functions

static enum childtype childtype (cxobj *x)
 
static char * childtype2str (enum childtype lt)
 
static char * arraytype2str (enum array_element_type lt)
 
static enum array_element_type array_eval (cxobj *xprev, cxobj *x, cxobj *xnext)
 
char * json_escape (char *str)
 
static int xml2json1_cbuf (cbuf *cb, cxobj *x, enum array_element_type arraytype, int level, int pretty, int flat)
 
int xml2json_cbuf (cbuf *cb, cxobj *x, int pretty)
 
int xml2json_cbuf_vec (cbuf *cb, cxobj **vec, size_t veclen, int pretty)
 
int xml2json (FILE *f, cxobj *x, int pretty)
 
int xml2json_vec (FILE *f, cxobj **vec, size_t veclen, int pretty)
 
static int json_parse (char *str, const char *name, cxobj *xt)
 
int json_parse_str (char *str, cxobj **xt)
 

Macro Definition Documentation

#define JSON_INDENT   2 /* maybe we should set this programmatically? */
#define VEC_ARRAY   1

Enumeration Type Documentation

Enumerator
NO_ARRAY 
FIRST_ARRAY 
MIDDLE_ARRAY 
LAST_ARRAY 
BODY_ARRAY 
enum childtype
Enumerator
NULL_CHILD 
BODY_CHILD 
ANY_CHILD 

Function Documentation

static enum array_element_type array_eval ( cxobj *  xprev,
cxobj *  x,
cxobj *  xnext 
)
static
static char* arraytype2str ( enum array_element_type  lt)
static
static enum childtype childtype ( cxobj *  x)
static

x is element and has exactly one child which in turn has none Clone from clixon_xml_map.c

static char* childtype2str ( enum childtype  lt)
static
char* json_escape ( char *  str)
static int json_parse ( char *  str,
const char *  name,
cxobj *  xt 
)
static

Parse a string containing JSON and return an XML tree

Parameters
[in]strInput string containing JSON
[in]nameLog string, typically filename
[out]xtXML top of tree typically w/o children on entry (but created)
int json_parse_str ( char *  str,
cxobj **  xt 
)

Parse string containing JSON and return an XML tree

Parameters
[in]strString containing JSON
[out]xtOn success a top of XML parse tree is created with name 'top'
Return values
0OK
-1Error with clicon_err called
cxobj *cx = NULL;
if (json_parse_str(str, &cx) < 0)
err;
Note
you need to free the xml parse tree after use, using xml_free()
int xml2json ( FILE *  f,
cxobj *  x,
int  pretty 
)

Translate from xml tree to JSON and print to file

Parameters
[in]fFile to print to
[in]xXML tree to translate from
[in]prettySet if output is pretty-printed
Return values
0OK
-1Error
if (xml2json(stderr, xn, 0) < 0)
goto err;
static int xml2json1_cbuf ( cbuf *  cb,
cxobj *  x,
enum array_element_type  arraytype,
int  level,
int  pretty,
int  flat 
)
static

Do the actual work of translating XML to JSON

Parameters
[out]cbCligen text buffer containing json on exit
[in]xXML tree structure containing XML to translate
[in]arraytypeDoes x occur in a array (of its parent) and how?
[in]levelIndentation level
[in]prettyPretty-print output (2 means debug)
[in]flatDont print NO_ARRAY object name (for _vec call)

The following matrix explains how the mapping is done. You need to understand what arraytype means (no/first/middle/last) and what childtype is (null,body,any) +------—+-----------—+-----------—+-----------—+ |array,leaf| null | body | any | +------—+-----------—+-----------—+-----------—+

no 1
json: :null : :{

}

+------—+-----------—+-----------—+-----------—+

first <a.. 1<a.. <a.
json: :[
:[
:[
{

}

+------—+-----------—+-----------—+-----------—+

middle ..a><a.. .a>1<a.
json: {a

}

+------—+-----------—+-----------—+-----------—+

last ..a> ..a>1
json: {a

]

]

}]

+------—+-----------—+-----------—+-----------—+

int xml2json_cbuf ( cbuf *  cb,
cxobj *  x,
int  pretty 
)

Translate an XML tree to JSON in a CLIgen buffer

Parameters
[in,out]cbCligen buffer to write to
[in]xXML tree to translate from
[in]prettySet if output is pretty-printed
[in]topBy default only children are printed, set if include top
Return values
0OK
-1Error
cbuf *cb;
cb = cbuf_new();
if (xml2json_cbuf(cb, xn, 0, 1) < 0)
goto err;
cbuf_free(cb);

See also xml2json

int xml2json_cbuf_vec ( cbuf *  cb,
cxobj **  vec,
size_t  veclen,
int  pretty 
)

Translate a vector of xml objects to JSON CLigen buffer. This is done by adding a top pseudo-object, and add the vector as subs, and then not pritning the top pseudo-.object using the 'flat' option.

Parameters
[out]cbCligen buffer to write to
[in]vecVector of xml objecst
[in]veclenLength of vector
[in]prettySet if output is pretty-printed (2 for debug)
Return values
0OK
-1Error
Note
This only works if the vector is uniform, ie same object name. Example: –> –> {"b" : null,"c" : null}
See Also
xml2json1_cbuf
int xml2json_vec ( FILE *  f,
cxobj **  vec,
size_t  veclen,
int  pretty 
)

Translate a vector of xml objects to JSON File. This is done by adding a top pseudo-object, and add the vector as subs, and then not pritning the top pseudo-.object using the 'flat' option.

Parameters
[out]cbCligen buffer to write to
[in]vecVector of xml objecst
[in]veclenLength of vector
[in]prettySet if output is pretty-printed (2 for debug)
Return values
0OK
-1Error
Note
This only works if the vector is uniform, ie same object name. Example: –> –> {"b" : null,"c" : null}
See Also
xml2json1_cbuf