ffead.server.doc
JSONSerialize.h
1 /*
2  Copyright 2010, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * JSONSerialize.h
18  *
19  * Created on: 27-Jan-2013
20  * Author: sumeetc
21  */
22 
23 #ifndef JSONSERIALIZE_H_
24 #define JSONSERIALIZE_H_
25 #include "XmlParser.h"
26 #include "CastUtil.h"
27 #include <stdexcept>
28 #include <dlfcn.h>
29 #include <cxxabi.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include "string"
33 #include <sstream>
34 #include <typeinfo>
35 #include "Object.h"
36 #include "queue"
37 #include "deque"
38 #include "list"
39 #include "map"
40 #include "set"
41 #include "stack"
42 #include <algorithm>
43 #include "DateFormat.h"
44 #include "BinaryData.h"
45 #include "Constants.h"
46 #include "AMEFResources.h"
47 #include "JSONUtil.h"
48 
50  static string demangle(const char *mangled);
51  static string getClassName(void* instance);
52  static string _handleAllSerialization(string className,void *t);
53  static void* _handleAllUnSerialization(string json,string className);
54  static string _ser(void* t,string classN);
55  static string _serContainers(void* t,string classN,string type);
56  static string _ser(Object);
57  static void* _unser(string json,string classN);
58  static void* unserializeConatiner(string json, string className,string type);
59 
60  static void* _unserCont(string json,string className,string type);
61 
62  template <class T> static string serializevec(vector<T> t)
63  {
64  vector<T> st = t;
65  T td;
66  int cnt = 0;
67  const char *mangled = typeid(td).name();
68  string className = demangle(mangled);
69  string json = "[";
70  while(cnt++<(int)t.size())
71  {
72  json += serialize<T>(*st.begin());
73  if(cnt!=(int)t.size())
74  json += ",";
75  st.erase(st.begin());
76  }
77  json += "]";
78  return json;
79  }
80 
81  template <class T> static string serializelist(list<T> t)
82  {
83  list<T> st = t;
84  T td;
85  int cnt = 0;
86  const char *mangled = typeid(td).name();
87  string className = demangle(mangled);
88  string json = "[";
89  while(cnt++<(int)t.size())
90  {
91  json += serialize<T>(*st.begin());
92  if(cnt!=(int)t.size())
93  json += ",";
94  st.erase(st.begin());
95  }
96  json += "]";
97  return json;
98  }
99 
100  template <class T> static string serializeset(set<T> t)
101  {
102  set<T> st = t;
103  T td;
104  int cnt = 0;
105  const char *mangled = typeid(td).name();
106  string className = demangle(mangled);
107  string json = "[";
108  while(cnt++<(int)t.size())
109  {
110  json += serialize<T>(*st.begin());
111  if(cnt!=(int)t.size())
112  json += ",";
113  st.erase(st.begin());
114  }
115  json += "]";
116  return json;
117  }
118 
119  template <class T> static string serializemultiset(multiset<T> t)
120  {
121  multiset<T> st = t;
122  T td;
123  int cnt = 0;
124  const char *mangled = typeid(td).name();
125  string className = demangle(mangled);
126  string json = "[";
127  while(cnt++<(int)t.size())
128  {
129  json += serialize<T>(*st.begin());
130  if(cnt!=(int)t.size())
131  json += ",";
132  st.erase(st.begin());
133  }
134  json += "]";
135  return json;
136  }
137 
138  template <class T> static string serializeq(std::queue<T> t)
139  {
140  std::queue<T> st = t;
141  T td;
142  int cnt = 0;
143  const char *mangled = typeid(td).name();
144  string className = demangle(mangled);
145  string json = "[";
146  while(cnt++<(int)t.size())
147  {
148  json += serialize<T>(st.front());
149  if(cnt!=(int)t.size())
150  json += ",";
151  st.pop();
152  }
153  json += "]";
154  return json;
155  }
156 
157  template <class T> static string serializedq(deque<T> t)
158  {
159  deque<T> st = t;
160  T td;
161  int cnt = 0;
162  const char *mangled = typeid(td).name();
163  string className = demangle(mangled);
164  string json = "[";
165  while(cnt++<(int)t.size())
166  {
167  json += serialize<T>(*st.begin());
168  if(cnt!=(int)t.size())
169  json += ",";
170  st.erase(st.begin());
171  }
172  json += "]";
173  return json;
174  }
175 
176  template <class T> static void* unserContainer(vector<T> &t, string type)
177  {
178  if(type=="Lis")
179  {
180  list<T>* tt = new list<T>;
181  std::copy(t.begin(), t.end(), std::back_inserter(*tt));
182  return tt;
183  }
184  else if(type=="Set")
185  {
186  set<T>* tt = new set<T>;
187  std::copy(t.begin(), t.end(), std::inserter(*tt, tt->begin()));
188  return tt;
189  }
190  else if(type=="MulSet")
191  {
192  multiset<T>* tt = new multiset<T>;
193  std::copy(t.begin(), t.end(), std::inserter(*tt, tt->begin()));
194  return tt;
195  }
196  else if(type=="Dq")
197  {
198  deque<T>* tt = new deque<T>;
199  std::copy(t.begin(), t.end(), std::inserter(*tt, tt->begin()));
200  return tt;
201  }
202  else if(type=="Q")
203  {
204  std::queue<T>* qq = new std::queue<T>;
205  for (int var = 0; var < (int)t.size(); ++var) {
206  qq->push(t.at(var));
207  }
208  return qq;
209  }
210  else
211  return &t;
212  }
213 
214 public:
215  JSONSerialize(){}
216  ~JSONSerialize(){}
217 
218  template <class T> static string serialize(T t)
219  {
220  string json;
221  const char *mangled = typeid(t).name();
222  string className = demangle(mangled);
223  return _handleAllSerialization(className,&t);
224  }
225 
226  static string serializeObject(Object t)
227  {
228  return _handleAllSerialization(t.getTypeName(),t.getVoidPointer());
229  }
230  static string serializeUnknown(void* t,string className)
231  {
232  return _handleAllSerialization(className,t);
233  }
234 
235  template <class K,class V> static string serialize(map<K,V> mp)
236  {
237  map<K,V> mpt = mp;
238  string json;
239  json = "[";
240  int cnt = 0;
241  while (cnt++<mp.size())
242  {
243  json += "{\"key\":";
244  json += serialize<K>(mpt.begin()->first);
245  json += ",\"value\":";
246  json += serialize<V>(mpt.begin()->second);
247  mpt.erase(mpt.begin());
248  json += "}";
249  if(cnt!=(int)mp.size())
250  json += ",";
251  }
252  json += "]";
253  return json;
254  }
255  template <class K,class V> static string serialize(multimap<K,V> mp)
256  {
257  multimap<K,V> mpt = mp;
258  string json;
259  json = "[";
260  int cnt = 0;
261  while (cnt++<mp.size())
262  {
263  json += "{\"key\":";
264  json += serialize<K>(mpt.begin()->first);
265  json += ",\"value\":";
266  json += serialize<V>(mpt.begin()->second);
267  mpt.erase(mpt.begin());
268  json += "}";
269  if(cnt!=(int)mp.size())
270  json += ",";
271  }
272  json += "]";
273  return json;
274  }
275 
276  template <class T> static T unserialize(string json)
277  {
278  T t;
279  const char *mangled = typeid(t).name();
280  string className = demangle(mangled);
281  return *(T*)_handleAllUnSerialization(json,className);
282  }
283  static void* unSerializeUnknown(string json,string className)
284  {
285  return _handleAllUnSerialization(json,className);
286  }
287 };
288 
289 #endif /* JSONSERIALIZE_H_ */