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