SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SerializableJsonReader00.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2010 Soeren Sonnenburg
8  * Copyright (C) 2010 Berlin Institute of Technology
9  */
10 
11 #include <shogun/lib/config.h>
12 #ifdef HAVE_JSON
13 
15 
16 using namespace shogun;
17 
18 SerializableJsonReader00::SerializableJsonReader00(
19  CSerializableJsonFile* file) { m_file = file; }
20 
21 SerializableJsonReader00::~SerializableJsonReader00()
22 {
23 }
24 
25 bool
26 SerializableJsonReader00::read_scalar_wrapped(
27  const TSGDataType* type, void* param)
28 {
29  json_object* m = m_file->m_stack_stream.back();
30 
31  switch (type->m_ptype) {
32  case PT_BOOL:
33  if (!json_object_is_type(m, json_type_boolean)) return false;
34  *(bool*) param = json_object_get_boolean(m);
35  break;
36  case PT_CHAR:
37  if (!json_object_is_type(m, json_type_int)) return false;
38  *(char*) param = json_object_get_int(m);
39  break;
40  case PT_INT8:
41  if (!json_object_is_type(m, json_type_int)) return false;
42  *(int8_t*) param = json_object_get_int(m);
43  break;
44  case PT_UINT8:
45  if (!json_object_is_type(m, json_type_int)) return false;
46  *(uint8_t*) param = json_object_get_int(m);
47  break;
48  case PT_INT16:
49  if (!json_object_is_type(m, json_type_int)) return false;
50  *(int16_t*) param = json_object_get_int(m);
51  break;
52  case PT_UINT16:
53  if (!json_object_is_type(m, json_type_int)) return false;
54  *(uint16_t*) param = json_object_get_int(m);
55  break;
56  case PT_INT32:
57  if (!json_object_is_type(m, json_type_int)) return false;
58  *(int32_t*) param = json_object_get_int(m);
59  break;
60  case PT_UINT32:
61  if (!json_object_is_type(m, json_type_int)) return false;
62  *(uint32_t*) param = json_object_get_int(m);
63  break;
64  case PT_INT64:
65  if (!json_object_is_type(m, json_type_int)) return false;
66  *(int64_t*) param = json_object_get_int(m);
67  break;
68  case PT_UINT64:
69  if (!json_object_is_type(m, json_type_int)) return false;
70  *(uint64_t*) param = json_object_get_int(m);
71  break;
72  case PT_FLOAT32:
73  if (!json_object_is_type(m, json_type_double)) return false;
74  *(float32_t*) param = json_object_get_double(m);
75  break;
76  case PT_FLOAT64:
77  if (!json_object_is_type(m, json_type_double)) return false;
78  *(float64_t*) param = json_object_get_double(m);
79  break;
80  case PT_FLOATMAX:
81  if (!json_object_is_type(m, json_type_double)) return false;
82  *(floatmax_t*) param = json_object_get_double(m);
83  break;
84  case PT_COMPLEX128:
85  SG_ERROR("read_scalar_wrapped(): Not supported for complex128_t"
86  " for reading from JsonFile!");
87  break;
88  case PT_SGOBJECT:
89  case PT_UNDEFINED:
90  SG_ERROR("read_scalar_wrapped(): Implementation error during"
91  " reading JsonFile!");
92  return false;
93  }
94 
95  return true;
96 }
97 
98 bool
99 SerializableJsonReader00::read_cont_begin_wrapped(
100  const TSGDataType* type, index_t* len_read_y, index_t* len_read_x)
101 {
102  json_object* m = m_file->m_stack_stream.back();
103 
104  if (!json_object_is_type(m, json_type_array)) return false;
105 
106  *len_read_y = json_object_array_length(m);
107 
108  if (type->m_ctype==CT_MATRIX || type->m_ctype==CT_SGMATRIX) {
109  *len_read_x = *len_read_y;
110  for (index_t i=0; i<*len_read_x; i++) {
111  json_object* buf = json_object_array_get_idx(m, i);
112  if (!json_object_is_type(buf, json_type_array))
113  return false;
114 
115  index_t len = json_object_array_length(buf);
116  if (i == 0) *len_read_y = len;
117  else if (*len_read_y != len) return false;
118  }
119  }
120 
121  return true;
122 }
123 
124 bool
125 SerializableJsonReader00::read_cont_end_wrapped(
126  const TSGDataType* type, index_t len_read_y, index_t len_read_x)
127 {
128  return true;
129 }
130 
131 bool
132 SerializableJsonReader00::read_string_begin_wrapped(
133  const TSGDataType* type, index_t* length)
134 {
135  json_object* m = m_file->m_stack_stream.back();
136 
137  if (!json_object_is_type(m, json_type_array)) return false;
138 
139  *length = json_object_array_length(m);
140 
141  return true;
142 }
143 
144 bool
145 SerializableJsonReader00::read_string_end_wrapped(
146  const TSGDataType* type, index_t length)
147 {
148  return true;
149 }
150 
151 bool
152 SerializableJsonReader00::read_stringentry_begin_wrapped(
153  const TSGDataType* type, index_t y)
154 {
155  json_object* m = m_file->m_stack_stream.back();
156 
157  json_object* buf = json_object_array_get_idx(m, y);
158  if (is_error(buf)) return false;
159 
160  m_file->push_object(buf);
161  return true;
162 }
163 
164 bool
165 SerializableJsonReader00::read_stringentry_end_wrapped(
166  const TSGDataType* type, index_t y)
167 {
168  m_file->pop_object();
169  return true;
170 }
171 
172 bool
173 SerializableJsonReader00::read_sparse_begin_wrapped(
174  const TSGDataType* type, index_t* length)
175 {
176  json_object* m = m_file->m_stack_stream.back();
177 
178  if (!json_object_is_type(m, json_type_object)) return false;
179 
180  json_object* buf;
181  if (!m_file->get_object(&buf, m, STR_KEY_SPARSE_FEATURES,
182  json_type_array)) return false;
183  *length = json_object_array_length(buf);
184  m_file->push_object(buf);
185 
186  return true;
187 }
188 
189 bool
190 SerializableJsonReader00::read_sparse_end_wrapped(
191  const TSGDataType* type, index_t length)
192 {
193  m_file->pop_object();
194  return true;
195 }
196 
197 bool
198 SerializableJsonReader00::read_sparseentry_begin_wrapped(
199  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
200  index_t* feat_index, index_t y)
201 {
202  json_object* m = m_file->m_stack_stream.back();
203 
204  json_object* buf_obj
205  = json_object_array_get_idx(m, y);
206  if (is_error(buf_obj)) return false;
207  if (!json_object_is_type(buf_obj, json_type_object)) return false;
208 
209  json_object* buf;
210  if (!m_file->get_object(&buf, buf_obj, STR_KEY_SPARSE_FEATINDEX,
211  json_type_int)) return false;
212  *feat_index = json_object_get_int(buf);
213 
214  if (!m_file->get_object_any(&buf, buf_obj, STR_KEY_SPARSE_ENTRY))
215  return false;
216  m_file->push_object(buf);
217 
218  return true;
219 }
220 
221 bool
222 SerializableJsonReader00::read_sparseentry_end_wrapped(
223  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
224  index_t* feat_index, index_t y)
225 {
226  m_file->pop_object();
227  return true;
228 }
229 
230 bool
231 SerializableJsonReader00::read_item_begin_wrapped(
232  const TSGDataType* type, index_t y, index_t x)
233 {
234  json_object* m = m_file->m_stack_stream.back();
235 
236  if (type->m_ctype==CT_MATRIX || type->m_ctype==CT_SGMATRIX)
237  m = json_object_array_get_idx(m, x);
238  m = json_object_array_get_idx(m, y);
239 
240  m_file->push_object(m);
241  return true;
242 }
243 
244 bool
245 SerializableJsonReader00::read_item_end_wrapped(
246  const TSGDataType* type, index_t y, index_t x)
247 {
248  m_file->pop_object();
249  return true;
250 }
251 
252 bool
253 SerializableJsonReader00::read_sgserializable_begin_wrapped(
254  const TSGDataType* type, char* sgserializable_name,
255  EPrimitiveType* generic)
256 {
257  json_object* m = m_file->m_stack_stream.back();
258 
259  if (m == NULL || json_object_is_type(m, json_type_null)) {
260  *sgserializable_name = '\0'; return true;
261  }
262 
263  if (!json_object_is_type(m, json_type_object)) return false;
264 
265  json_object* buf;
266  if (!m_file->get_object(&buf, m, STR_KEY_INSTANCE_NAME,
267  json_type_string)) return false;
268  strncpy(sgserializable_name, json_object_get_string(buf),
269  STRING_LEN);
270 
271  if (m_file->get_object(&buf, m, STR_KEY_GENERIC_NAME,
272  json_type_string)) {
274  generic, json_object_get_string(buf))) return false;
275  }
276 
277  if (!m_file->get_object(&buf, m, STR_KEY_INSTANCE,
278  json_type_object)) return false;
279  m_file->push_object(buf);
280 
281  return true;
282 }
283 
284 bool
285 SerializableJsonReader00::read_sgserializable_end_wrapped(
286  const TSGDataType* type, const char* sgserializable_name,
287  EPrimitiveType generic)
288 {
289  if (*sgserializable_name == '\0') return true;
290 
291  m_file->pop_object();
292  return true;
293 }
294 
295 bool
296 SerializableJsonReader00::read_type_begin_wrapped(
297  const TSGDataType* type, const char* name, const char* prefix)
298 {
299  json_object* m = m_file->m_stack_stream.back();
300 
301  if (!json_object_is_type(m, json_type_object)) return false;
302 
303  json_object* buf_type;
304  if (!m_file->get_object(&buf_type, m, name, json_type_object))
305  return false;
306 
307  string_t str_buf; json_object* buf;
308  type->to_string(str_buf, STRING_LEN);
309  if (!m_file->get_object(&buf, buf_type, STR_KEY_TYPE,
310  json_type_string)) return false;
311  if (strcmp(str_buf, json_object_get_string(buf)) != 0)
312  return false;
313 
314  // data (and so buf) can be NULL for empty objects
315  m_file->get_object_any(&buf, buf_type, STR_KEY_DATA);
316  m_file->push_object(buf);
317 
318  return true;
319 }
320 
321 bool
322 SerializableJsonReader00::read_type_end_wrapped(
323  const TSGDataType* type, const char* name, const char* prefix)
324 {
325  m_file->pop_object();
326  return true;
327 }
328 
329 #endif /* HAVE_JSON */
int32_t index_t
Definition: common.h:62
static bool string_to_ptype(EPrimitiveType *ptype, const char *str)
Definition: DataType.cpp:393
#define SG_ERROR(...)
Definition: SGIO.h:129
Datatypes that shogun supports.
Definition: DataType.h:68
double float64_t
Definition: common.h:50
long double floatmax_t
Definition: common.h:51
#define STRING_LEN
Definition: common.h:55
float float32_t
Definition: common.h:49
void to_string(char *dest, size_t n) const
Definition: DataType.cpp:145
EContainerType m_ctype
Definition: DataType.h:71
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
template class SGSparseVectorEntry
Definition: File.h:23
char string_t[STRING_LEN]
Definition: common.h:57
EPrimitiveType m_ptype
Definition: DataType.h:75

SHOGUN Machine Learning Toolbox - Documentation