RDK Documentation (Open Sourced RDK Components)
jsonParser.h
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 
21 /**
22 * @defgroup iarmmgrs
23 * @{
24 * @defgroup deviceUpdateMgr
25 * @{
26 **/
27 
28 
29 #if !defined(VREX_JSON_PARSER_H)
30 #define VREX_JSON_PARSER_H
31 
32 #include <map>
33 #include <string>
34 #include <utility>
35 #include <stack>
36 #include <list>
37 
38 #include <yajl/yajl_parse.h>
39 #include <yajl/yajl_gen.h>
40 
41 using namespace std;
42 
44 {
45 
46 
47 
48 public:
49  struct varVal{
50  string str;
51  list<varVal *> *array;
52  bool boolean;
53  };
54 
55  JSONParser();
56  virtual ~JSONParser();
57  map<string, varVal *> parse(const unsigned char *);
58 
59  void newKey(string keyName) { m_curKey = keyName; }
60  void newString(string value) {
61  varVal *vv=new varVal();
62  vv->str=value;
63  if(m_array!=NULL){
64  m_array->push_back(vv);
65  }else{
66  m_dict[m_curKey] = vv;
67  }
68 
69  }
70  void newNull() { m_dict[m_curKey] = new varVal(); }
71  void newBool(bool value) {
72  varVal *vv=new varVal();
73  vv->boolean=value;
74  if(m_array!=NULL){
75  m_array->push_back(vv);
76  }else{
77  m_dict[m_curKey] = vv;
78  }
79  }
80 
81  void newArray(){
82  varVal *vv=new varVal();
83  vv->array=new list<varVal *>;
84  if(m_array!=NULL){
85  m_array->push_back(vv);
86  m_arrays.push(m_array);
87  m_array=vv->array;
88  }else{
89  m_dict[m_curKey] = vv;
90  m_array=vv->array;
91  }
92  }
93 
94  void endArray(){
95  if(m_arrays.empty()==false){
96  m_array=m_arrays.top();
97  m_arrays.pop();
98  }
99  else{
100  m_array=NULL;
101  }
102  }
103 
104 private:
105  string m_curKey;
106  map<string, varVal *> m_dict;
107  stack< list<varVal *> *> m_arrays;
108  list<varVal *> *m_array;
109 
110 };
111 
112 #endif
113 
114 
115 /** @} */
116 /** @} */
JSONParser
Definition: jsonParser.h:43
JSONParser::varVal
Definition: jsonParser.h:49