CPPMyth
Library to interoperate with MythTV server
Loading...
Searching...
No Matches
mythjsonbinder.cpp
1/*
2 * Copyright (C) 2014 Jean-Luc Barriere
3 *
4 * This Program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This Program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 */
21
22#include "mythjsonbinder.h"
23#include "builtin.h"
24#include "debug.h"
25
26#include <cstdlib> // for atof
27#include <cstring> // for strcmp
28#include <cstdio>
29#include <errno.h>
30
31using namespace Myth;
32
33void JSON::BindObject(const Node& node, void *obj, const bindings_t *bl)
34{
35 int i, err;
36
37 if (bl == nullptr || node.IsNull())
38 return;
39
40 for (i = 0; i < bl->attr_count; ++i)
41 {
42 err = 0;
43 const Node& field = node.GetObjectValue(bl->attr_bind[i].field);
44 if (field.IsNull())
45 continue;
46 if (field.IsString())
47 {
48 std::string value(field.GetStringValue());
49 switch (bl->attr_bind[i].type)
50 {
51 case IS_STRING:
52 bl->attr_bind[i].set(obj, value.c_str());
53 break;
54 case IS_INT8:
55 {
56 int8_t num = 0;
57 err = string_to_int8(value.c_str(), &num);
58 bl->attr_bind[i].set(obj, &num);
59 break;
60 }
61 case IS_INT16:
62 {
63 int16_t num = 0;
64 err = string_to_int16(value.c_str(), &num);
65 bl->attr_bind[i].set(obj, &num);
66 break;
67 }
68 case IS_INT32:
69 {
70 int32_t num = 0;
71 err = string_to_int32(value.c_str(), &num);
72 bl->attr_bind[i].set(obj, &num);
73 break;
74 }
75 case IS_INT64:
76 {
77 int64_t num = 0;
78 err = string_to_int64(value.c_str(), &num);
79 bl->attr_bind[i].set(obj, &num);
80 break;
81 }
82 case IS_UINT8:
83 {
84 uint8_t num = 0;
85 err = string_to_uint8(value.c_str(), &num);
86 bl->attr_bind[i].set(obj, &num);
87 break;
88 }
89 case IS_UINT16:
90 {
91 uint16_t num = 0;
92 err = string_to_uint16(value.c_str(), &num);
93 bl->attr_bind[i].set(obj, &num);
94 break;
95 }
96 case IS_UINT32:
97 {
98 uint32_t num = 0;
99 err = string_to_uint32(value.c_str(), &num);
100 bl->attr_bind[i].set(obj, &num);
101 break;
102 }
103 case IS_DOUBLE:
104 {
105 double num = atof(value.c_str());
106 bl->attr_bind[i].set(obj, &num);
107 break;
108 }
109 case IS_BOOLEAN:
110 {
111 bool b = (strcmp(value.c_str(), "true") == 0 ? true : false);
112 bl->attr_bind[i].set(obj, &b);
113 break;
114 }
115 case IS_TIME:
116 {
117 time_t time = 0;
118 err = string_to_time(value.c_str(), &time);
119 bl->attr_bind[i].set(obj, &time);
120 break;
121 }
122 default:
123 break;
124 }
125 }
126 else
127 {
128 switch (bl->attr_bind[i].type)
129 {
130 case IS_STRING:
131 {
132 if (field.IsString())
133 {
134 std::string value(field.GetStringValue());
135 bl->attr_bind[i].set(obj, value.c_str());
136 }
137 else
138 DBG(DBG_ERROR, "%s: invalid value for field \"%s\" type %d\n", __FUNCTION__, bl->attr_bind[i].field, bl->attr_bind[i].type);
139 break;
140 }
141 case IS_INT8:
142 {
143 if (field.IsInt())
144 {
145 int8_t num = field.GetIntValue();
146 bl->attr_bind[i].set(obj, &num);
147 }
148 else
149 DBG(DBG_ERROR, "%s: invalid value for field \"%s\" type %d\n", __FUNCTION__, bl->attr_bind[i].field, bl->attr_bind[i].type);
150 break;
151 }
152 case IS_INT16:
153 {
154 if (field.IsInt())
155 {
156 int16_t num = field.GetIntValue();
157 bl->attr_bind[i].set(obj, &num);
158 }
159 else
160 DBG(DBG_ERROR, "%s: invalid value for field \"%s\" type %d\n", __FUNCTION__, bl->attr_bind[i].field, bl->attr_bind[i].type);
161 break;
162 }
163 case IS_INT32:
164 {
165 if (field.IsInt())
166 {
167 int32_t num = field.GetIntValue();
168 bl->attr_bind[i].set(obj, &num);
169 }
170 else
171 DBG(DBG_ERROR, "%s: invalid value for field \"%s\" type %d\n", __FUNCTION__, bl->attr_bind[i].field, bl->attr_bind[i].type);
172 break;
173 }
174 case IS_INT64:
175 {
176 if (field.IsInt() || field.IsDouble())
177 {
178 int64_t num = field.GetBigIntValue();
179 bl->attr_bind[i].set(obj, &num);
180 }
181 else
182 DBG(DBG_ERROR, "%s: invalid value for field \"%s\" type %d\n", __FUNCTION__, bl->attr_bind[i].field, bl->attr_bind[i].type);
183 break;
184 }
185 case IS_UINT8:
186 {
187 if (field.IsInt())
188 {
189 uint8_t num = field.GetIntValue();
190 bl->attr_bind[i].set(obj, &num);
191 }
192 else
193 DBG(DBG_ERROR, "%s: invalid value for field \"%s\" type %d\n", __FUNCTION__, bl->attr_bind[i].field, bl->attr_bind[i].type);
194 break;
195 }
196 case IS_UINT16:
197 {
198 if (field.IsInt())
199 {
200 uint16_t num = field.GetIntValue();
201 bl->attr_bind[i].set(obj, &num);
202 }
203 else
204 DBG(DBG_ERROR, "%s: invalid value for field \"%s\" type %d\n", __FUNCTION__, bl->attr_bind[i].field, bl->attr_bind[i].type);
205 break;
206 }
207 case IS_UINT32:
208 {
209 if (field.IsInt() || field.IsDouble())
210 {
211 uint32_t num = (uint32_t) field.GetBigIntValue();
212 bl->attr_bind[i].set(obj, &num);
213 }
214 else
215 DBG(DBG_ERROR, "%s: invalid value for field \"%s\" type %d\n", __FUNCTION__, bl->attr_bind[i].field, bl->attr_bind[i].type);
216 break;
217 }
218 case IS_DOUBLE:
219 {
220 if (field.IsDouble() || field.IsInt())
221 {
222 double num = field.GetDoubleValue();
223 bl->attr_bind[i].set(obj, &num);
224 }
225 else
226 DBG(DBG_ERROR, "%s: invalid value for field \"%s\" type %d\n", __FUNCTION__, bl->attr_bind[i].field, bl->attr_bind[i].type);
227 break;
228 }
229 case IS_BOOLEAN:
230 {
231 if (field.IsTrue() || field.IsFalse())
232 {
233 bool b = field.IsTrue();
234 bl->attr_bind[i].set(obj, &b);
235 }
236 else
237 DBG(DBG_ERROR, "%s: invalid value for field \"%s\" type %d\n", __FUNCTION__, bl->attr_bind[i].field, bl->attr_bind[i].type);
238 break;
239 }
240 case IS_TIME:
241 {
242 if (field.IsString())
243 {
244 std::string value(field.GetStringValue());
245 time_t time = 0;
246 err = string_to_time(value.c_str(), &time);
247 bl->attr_bind[i].set(obj, &time);
248 }
249 else
250 DBG(DBG_ERROR, "%s: invalid value for field \"%s\" type %d\n", __FUNCTION__, bl->attr_bind[i].field, bl->attr_bind[i].type);
251 break;
252 }
253 default:
254 break;
255 }
256 }
257 if (err)
258 DBG(DBG_ERROR, "%s: failed (%d) field \"%s\" type %d\n", __FUNCTION__, err, bl->attr_bind[i].field, bl->attr_bind[i].type);
259 }
260}
This is the main namespace that encloses all public classes.
Definition mythcontrol.h:30
const char * field
Definition mythdto.h:57
FT_t type
Definition mythdto.h:58
void(* set)(void *, const void *)
Definition mythdto.h:59
Brings together all attribute bindings of an object.
Definition mythdto.h:67
attr_bind_t * attr_bind
Definition mythdto.h:69
int attr_count
Definition mythdto.h:68