RDK Documentation (Open Sourced RDK Components)
jsonParser.cpp
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 #include "jsonParser.h"
30 
31 #include "deviceUpdateMgrInternal.h"
32 #include "iarmUtil.h"
33 
34 #include <stdio.h>
35 #include <string.h>
36 
37 static int parse_null(void * ctx)
38 {
39  JSONParser *parser = (JSONParser *)ctx;
40  parser->newNull();
41  //__TIMESTAMP(); printf ("Parse NULL\n");
42  //yajl_gen g = (yajl_gen) ctx;
43  //return yajl_gen_status_ok == yajl_gen_null(g);
44  return 1;
45 }
46 
47 static int parse_boolean(void * ctx, int boolean)
48 {
49  JSONParser *parser = (JSONParser *)ctx;
50  parser->newBool(boolean);
51  //__TIMESTAMP(); printf ("Parse Bool\n");
52  //yajl_gen g = (yajl_gen) ctx;
53  //return yajl_gen_status_ok == yajl_gen_bool(g, boolean);
54  return 1;
55 }
56 
57 static int parse_number(void * ctx, const char * s, size_t l)
58 {
59  std::string str;
60  str.append(s, l);
61  JSONParser *parser = (JSONParser *)ctx;
62  parser->newString(str);
63  //currentMap[currentMapKey] = str;
64  //__TIMESTAMP(); printf ("NUMBER: <%s>\n", str.c_str());
65  //yajl_gen g = (yajl_gen) ctx;
66  //return yajl_gen_status_ok == yajl_gen_number(g, s, l);
67  return 1;
68 }
69 
70 static int parse_string(void * ctx, const unsigned char * stringVal,
71  size_t stringLen)
72 {
73  std::string str;
74  str.append((const char *)stringVal, stringLen);
75  JSONParser *parser = (JSONParser *)ctx;
76  if(str=="true" || str=="false")
77  {
78  parser->newBool((str=="true")?true:false);
79  }else
80  {
81  parser->newString(str);
82  }
83  //currentMap[currentMapKey] = str;
84  //printf ("STRING: <%s>\n", str.c_str());
85  //yajl_gen g = (yajl_gen) ctx;
86  //return yajl_gen_status_ok == yajl_gen_string(g, stringVal, stringLen);
87  return 1;
88 }
89 
90 static int parse_map_key(void * ctx, const unsigned char * stringVal,
91  size_t stringLen)
92 {
93  std::string str;
94  str.append((const char *)stringVal, stringLen);
95  JSONParser *parser = (JSONParser *)ctx;
96  parser->newKey(str);
97  //printf ("MAPKEY: <%s>\n", str.c_str());
98  //currentMapKey = str;
99  //yajl_gen g = (yajl_gen) ctx;
100  //return yajl_gen_status_ok == yajl_gen_string(g, stringVal, stringLen);
101  return 1;
102 }
103 
104 static int parse_start_map(void * ctx)
105 {
106  yajl_gen g = (yajl_gen) ctx;
107  //return yajl_gen_status_ok == yajl_gen_map_open(g);
108  return 1;
109 }
110 
111 
112 static int parse_end_map(void * ctx)
113 {
114  yajl_gen g = (yajl_gen) ctx;
115  //return yajl_gen_status_ok == yajl_gen_map_close(g);
116  return 1;
117 }
118 
119 static int parse_start_array(void * ctx)
120 {
121  yajl_gen g = (yajl_gen) ctx;
122  printf("got array start\n\r");
123  JSONParser *parser = (JSONParser *)ctx;
124  parser->newArray();
125  //return yajl_gen_status_ok == yajl_gen_array_open(g);
126  return 1;
127 }
128 
129 static int parse_end_array(void * ctx)
130 {
131  yajl_gen g = (yajl_gen) ctx;
132  printf("got array end\n\r");
133  JSONParser *parser = (JSONParser *)ctx;
134  parser->endArray();
135  //return yajl_gen_status_ok == yajl_gen_array_close(g);
136  printf("exit array\n\r");
137  return 1;
138 }
139 
140 static yajl_callbacks callbacks = {
141  parse_null,
142  parse_boolean,
143  NULL,
144  NULL,
145  parse_number,
146  parse_string,
147  parse_start_map,
148  parse_map_key,
149  parse_end_map,
150  parse_start_array,
151  parse_end_array
152 };
153 
154 
155 JSONParser::JSONParser()
156 {
157 // m_arrays=new stack<queue<JSONParser::varVal *> *>();
158  m_array=NULL;
159 }
160 
161 JSONParser::~JSONParser()
162 {
163 }
164 
165 std::map<string, JSONParser::varVal *> JSONParser::parse(const unsigned char *json)
166 {
167  yajl_handle hand;
168  yajl_gen g;
169  yajl_status status;
170 
171  size_t jsonLen = strlen((const char *)json);
172 
173 #ifndef USE_YAJL2
174  yajl_gen_config config;
175  yajl_parser_config cfg;
176 
177  config.beautify = 1;
178 
179  g = yajl_gen_alloc(&config, NULL);
180  cfg.checkUTF8 = 0;
181  cfg.allowComments = 1;
182  //hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) g);
183  hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) this);
184 #else
185  g = yajl_gen_alloc(NULL);
186 
187  yajl_gen_config(g, yajl_gen_beautify, 1);
188  yajl_gen_config(g, yajl_gen_validate_utf8, 0);
189 
190  hand = yajl_alloc(&callbacks, NULL, (void *) this);
191 
192  yajl_config(hand, yajl_allow_comments, 1);
193 #endif
194 
195  status = yajl_parse(hand, json, jsonLen);
196 
197  if (status != yajl_status_ok) {
198  __TIMESTAMP();INT_LOG("JSONParser: Parse failed\n");
199  goto done;
200  }
201 
202 #ifdef USE_YAJL2
203  status = yajl_complete_parse(hand);
204 #else
205  status = yajl_parse_complete(hand);
206 #endif
207 
208  if (status != yajl_status_ok) {
209  unsigned char *errorString = yajl_get_error(hand, 1, json, jsonLen);
210  printf("%s", (const char *) errorString);
211  yajl_free_error(hand, errorString);
212  }
213 
214 done:
215  yajl_gen_free(g);
216  yajl_free(hand);
217 
218  return m_dict;
219 }
220 
221 
222 /** @} */
223 /** @} */
JSONParser
Definition: jsonParser.h:43