1 /**
2 
3         \file  ADM_coreJson.h
4         \brief
5 */
6 /***************************************************************************
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  ***************************************************************************/
14 #include "ADM_cpp.h"
15 #include "ADM_default.h"
16 
17 #include "ADM_coreJson.h"
18 extern "C"
19 {
20 #include "libjson.h"
21 }
22 #define COOKIE ((JSONNODE *)cookie)
23 /**
24 
25 */
admJson()26 admJson::admJson()
27 {
28     JSONNODE *n = json_new(JSON_NODE);
29     cookie=(void *)n;
30     cookies.push_back(cookie);
31 }
32 /**
33 
34 */
~admJson()35 admJson::~admJson()
36 {
37     int l=cookies.size();
38     for(int i=0;i<l;i++)
39     {
40         json_delete((JSONNODE*)cookies[i]);
41     }
42     cookie=NULL;
43     cookies.clear();
44 }
45 /**
46 
47 */
addNode(const char * nodeName)48 bool admJson::addNode(const char *nodeName)
49 {
50     JSONNODE *n = json_new(JSON_NODE);
51     cookies.push_back((void *)n);
52     json_set_name(n,nodeName);
53     cookie=(void *)n;
54     return true;
55 }
56 /**
57 
58 */
endNode(void)59 bool admJson::endNode(void)
60 {
61     int l=cookies.size();
62     ADM_assert(l>1);
63     JSONNODE *prev=(JSONNODE *)cookies[l-2];
64     json_push_back(prev,(JSONNODE *)cookie);
65     cookies.pop_back();
66     cookie=prev;
67     return true;
68 }
69 
70 /**
71 */
addString(const char * key,const char * value)72 bool admJson::addString(const char *key,const char *value)
73 {
74     json_push_back(COOKIE, json_new_a(key,value));
75     return true;
76 }
addString(const char * key,const std::string & value)77 bool admJson::addString(const char *key,const std::string &value)
78 {
79     json_push_back(COOKIE, json_new_a(key,value.c_str()));
80     return true;
81 
82 }
83 /**
84 
85 */
addUint32(const char * key,const uint32_t value)86 bool admJson::addUint32(const char *key,const uint32_t value)
87 {
88     json_push_back(COOKIE, json_new_i(key,value));
89     return true;
90 }
91 /**
92 */
addInt32(const char * key,const int32_t value)93 bool admJson::addInt32(const char *key,const int32_t value)
94 {
95     json_push_back(COOKIE, json_new_i(key,value));
96     return true;
97 }
98 /**
99 
100 */
addFloat(const char * key,const float value)101 bool admJson::addFloat(const char *key,const float value)
102 {
103     json_push_back(COOKIE, json_new_f(key,value));
104     return true;
105 }
106 /**
107 
108 */
addDouble(const char * key,const double value)109 bool admJson::addDouble(const char *key,const double value)
110 {
111     json_push_back(COOKIE, json_new_f(key,value));
112     return true;
113 }
114 /**
115 
116 */
addBool(const char * key,const bool value)117 bool admJson::addBool(const char *key,const bool value)
118 {
119     json_push_back(COOKIE, json_new_b(key,value));
120     return true;
121 }
122 /**
123     \fn addCompressParam
124 */
125 extern bool ADM_compressWriteToString(COMPRES_PARAMS *params,  char **str);
addCompressParam(const char * key,const COMPRES_PARAMS & param)126 bool admJson::addCompressParam(const char *key, const COMPRES_PARAMS &param)
127 {
128     char *str;
129     ADM_compressWriteToString((COMPRES_PARAMS *)&param,&str);
130     addString(key,str);
131     ADM_dealloc(str);
132     return true;
133 }
134 /**
135 
136 */
dumpToFile(const char * file)137 bool admJson::dumpToFile(const char *file)
138 {
139     FILE *f=fopen(file,"w");
140     if(!f)
141     {
142         ADM_error("Cannot open file %s\n",file);
143         return false;
144     }
145     json_char *jc = json_write_formatted(COOKIE);
146     fprintf(f,"%s",jc);
147     json_free(jc);
148 
149     fclose(f);
150     return true;
151 }
152 //****************************************************************
153 /**
154 
155 */
scan(void * xnode,string name)156 bool admJsonToCouple::scan( void *xnode,string name)
157 {
158     JSONNODE *node=(JSONNODE *)xnode;
159    if (!node){
160         ADM_error("Invalid JSON Node\n");
161         return false;
162     }
163 
164     JSONNODE_ITERATOR i = json_begin(node);
165     while (i != json_end(node)){
166         if (*i == NULL){
167             ADM_error("Invalid JSON Node\n");
168             return false;
169         }
170         json_char *node_name = json_name(*i);
171         //printf("Node :%s\n",node_name);
172         // recursively call ourselves to dig deeper into the tree
173         if (json_type(*i) == JSON_ARRAY || json_type(*i) == JSON_NODE)
174         {
175             if(name=="")
176                 scan(*i,string(node_name));
177             else
178                 scan(*i,name+string(".")+string(node_name));
179         }
180         else
181         {
182             keyVal k;
183             json_char *node_value = json_as_string(*i);
184             if(name=="")
185                 k.key=string(node_name);
186             else
187                 k.key=string(name)+string(".")+string(node_name);
188             k.value=string(node_value);
189             readItems.push_back(k);
190             json_free(node_value);
191         }
192         json_free(node_name);
193         ++i;
194     }
195     return true;
196 }
197 /**
198     \fn readFromFile
199     \brief construct a list of key/value from a json file
200 */
201 
readFromFile(const char * file)202 CONFcouple *admJsonToCouple::readFromFile(const char *file)
203 {
204         FILE *f=fopen(file,"rt");
205         if(!f)
206         {
207             ADM_error("Cannot open %s\n",file);
208             return NULL;
209         }
210         fseek(f,0,SEEK_END);
211         uint32_t fileSize=ftell(f);
212         fseek(f,0,SEEK_SET);
213         char *buffer = new char[fileSize+1];
214         char *head=buffer;
215         while(fgets(head,fileSize,f))
216         {
217             head=buffer+strlen(buffer);
218         }
219         fclose(f);
220         // Now parse and build a tree out of it...
221         JSONNODE *n = json_parse(buffer);
222 
223 		delete [] buffer;
224 
225         if(true==scan(n,""))
226         {
227 
228         }
229         json_delete(n);
230         // Dump
231         int l=readItems.size();
232         for(int i=0;i<l;i++)
233         {
234            // printf(" %s => %s\n",readItems[i].key.c_str(),readItems[i].value.c_str());
235         }
236         //
237         CONFcouple *c=new CONFcouple(l);
238         for(int i=0;i<l;i++)
239               c->setInternalName(readItems[i].key.c_str(),readItems[i].value.c_str());
240         return c;
241 }
242 
243 // EOF
244