#include "json_xml.h"
#include "fieldmapper_lookup.h"
void _rest_xml_output(growing_buffer*, jsonObject*, char*, int, int);
char* _escape_xml (char*);
char* json_string_to_xml(char* content) {
jsonObject * obj;
growing_buffer * res_xml;
char * output;
int i;
obj = json_parse_string( content );
res_xml = buffer_init(1024);
if (!obj)
return NULL;
buffer_add(res_xml, "");
if(obj->type == JSON_ARRAY ) {
for( i = 0; i!= obj->size; i++ ) {
_rest_xml_output(res_xml, jsonObjectGetIndex(obj,i), NULL, 0,0);
}
} else {
_rest_xml_output(res_xml, obj, NULL, 0,0);
}
buffer_add(res_xml, "");
output = buffer_data(res_xml);
buffer_free(res_xml);
jsonObjectFree(obj);
return output;
}
char* _escape_xml (char* text) {
char* out;
growing_buffer* b = buffer_init(256);
int len = strlen(text);
int i;
for (i = 0; i < len; i++) {
if (text[i] == '&')
buffer_add(b,"&");
else if (text[i] == '<')
buffer_add(b,"<");
else if (text[i] == '>')
buffer_add(b,">");
else
buffer_add_char(b,text[i]);
}
out = buffer_data(b);
buffer_free(b);
return out;
}
void _rest_xml_output(growing_buffer* buf, jsonObject* obj, char * obj_class, int arr_index, int notag) {
char * tag;
int i;
if(!obj) return;
if (obj->classname)
notag = 1;
if(isFieldmapper(obj_class)) {
tag = fm_pton(obj_class,arr_index);
} else if(obj_class) {
tag = strdup(obj_class);
} else {
tag = strdup("datum");
}
/* add class hints if we have a class name */
if(obj->classname) {
if(obj->type == JSON_NULL) {
buffer_fadd(buf,"<%s>