ffead.server.doc
JSONUtil.h
1 /*
2  Copyright 2009-2012, 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  * JSONUtil.h
18  *
19  * Created on: 06-Aug-2012
20  * Author: sumeetc
21  */
22 
23 #ifndef JSONUTIL_H_
24 #define JSONUTIL_H_
25 #include "string"
26 #include "JSONElement.h"
27 #include "StringUtil.h"
28 #include "CastUtil.h"
29 #include <stdio.h>
30 using namespace std;
31 
32 class JSONUtil {
33  static void array(string& json, JSONElement* element);
34  static void object(string& json, JSONElement* element);
35  static void arrayOrObject(string& json, JSONElement* element);
36  static void readJSON(string& json,bool isarray,JSONElement *par);
37  static void validateSetValue(JSONElement* element, string value);
38 public:
39  JSONUtil();
40  virtual ~JSONUtil();
41  static JSONElement getDocument(const string& json);
42  static string getDocumentStr(JSONElement doc);
43  template <typename T> static vector<T>* toVectorP(JSONElement root)
44  {
45  vector<T>* vec = new vector<T>;
46  for(int i=0;i<(int)root.getChildren().size();i++)
47  {
48  JSONElement* element = root.getChildren().at(i);
49  if(element!=NULL)
50  vec->push_back(CastUtil::lexical_cast<T>(element->getValue()));
51  }
52  return vec;
53  }
54 };
55 
56 #endif /* JSONUTIL_H_ */