SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  SG_ERROR("read_scalar_wrapped(): Implementation error during"
90  " reading JsonFile!");
91  return false;
92  }
93 
94  return true;
95 }
96 
97 bool
98 SerializableJsonReader00::read_cont_begin_wrapped(
99  const TSGDataType* type, index_t* len_read_y, index_t* len_read_x)
100 {
101  json_object* m = m_file->m_stack_stream.back();
102 
103  if (!json_object_is_type(m, json_type_array)) return false;
104 
105  *len_read_y = json_object_array_length(m);
106 
107  if (type->m_ctype==CT_MATRIX || type->m_ctype==CT_SGMATRIX) {
108  *len_read_x = *len_read_y;
109  for (index_t i=0; i<*len_read_x; i++) {
110  json_object* buf = json_object_array_get_idx(m, i);
111  if (!json_object_is_type(buf, json_type_array))
112  return false;
113 
114  index_t len = json_object_array_length(buf);
115  if (i == 0) *len_read_y = len;
116  else if (*len_read_y != len) return false;
117  }
118  }
119 
120  return true;
121 }
122 
123 bool
124 SerializableJsonReader00::read_cont_end_wrapped(
125  const TSGDataType* type, index_t len_read_y, index_t len_read_x)
126 {
127  return true;
128 }
129 
130 bool
131 SerializableJsonReader00::read_string_begin_wrapped(
132  const TSGDataType* type, index_t* length)
133 {
134  json_object* m = m_file->m_stack_stream.back();
135 
136  if (!json_object_is_type(m, json_type_array)) return false;
137 
138  *length = json_object_array_length(m);
139 
140  return true;
141 }
142 
143 bool
144 SerializableJsonReader00::read_string_end_wrapped(
145  const TSGDataType* type, index_t length)
146 {
147  return true;
148 }
149 
150 bool
151 SerializableJsonReader00::read_stringentry_begin_wrapped(
152  const TSGDataType* type, index_t y)
153 {
154  json_object* m = m_file->m_stack_stream.back();
155 
156  json_object* buf = json_object_array_get_idx(m, y);
157  if (is_error(buf)) return false;
158 
159  m_file->push_object(buf);
160  return true;
161 }
162 
163 bool
164 SerializableJsonReader00::read_stringentry_end_wrapped(
165  const TSGDataType* type, index_t y)
166 {
167  m_file->pop_object();
168  return true;
169 }
170 
171 bool
172 SerializableJsonReader00::read_sparse_begin_wrapped(
173  const TSGDataType* type, index_t* length)
174 {
175  json_object* m = m_file->m_stack_stream.back();
176 
177  if (!json_object_is_type(m, json_type_object)) return false;
178 
179  json_object* buf;
180  if (!m_file->get_object(&buf, m, STR_KEY_SPARSE_FEATURES,
181  json_type_array)) return false;
182  *length = json_object_array_length(buf);
183  m_file->push_object(buf);
184 
185  return true;
186 }
187 
188 bool
189 SerializableJsonReader00::read_sparse_end_wrapped(
190  const TSGDataType* type, index_t length)
191 {
192  m_file->pop_object();
193  return true;
194 }
195 
196 bool
197 SerializableJsonReader00::read_sparseentry_begin_wrapped(
198  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
199  index_t* feat_index, index_t y)
200 {
201  json_object* m = m_file->m_stack_stream.back();
202 
203  json_object* buf_obj
204  = json_object_array_get_idx(m, y);
205  if (is_error(buf_obj)) return false;
206  if (!json_object_is_type(buf_obj, json_type_object)) return false;
207 
208  json_object* buf;
209  if (!m_file->get_object(&buf, buf_obj, STR_KEY_SPARSE_FEATINDEX,
210  json_type_int)) return false;
211  *feat_index = json_object_get_int(buf);
212 
213  if (!m_file->get_object_any(&buf, buf_obj, STR_KEY_SPARSE_ENTRY))
214  return false;
215  m_file->push_object(buf);
216 
217  return true;
218 }
219 
220 bool
221 SerializableJsonReader00::read_sparseentry_end_wrapped(
222  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
223  index_t* feat_index, index_t y)
224 {
225  m_file->pop_object();
226  return true;
227 }
228 
229 bool
230 SerializableJsonReader00::read_item_begin_wrapped(
231  const TSGDataType* type, index_t y, index_t x)
232 {
233  json_object* m = m_file->m_stack_stream.back();
234 
235  if (type->m_ctype==CT_MATRIX || type->m_ctype==CT_SGMATRIX)
236  m = json_object_array_get_idx(m, x);
237  m = json_object_array_get_idx(m, y);
238 
239  m_file->push_object(m);
240  return true;
241 }
242 
243 bool
244 SerializableJsonReader00::read_item_end_wrapped(
245  const TSGDataType* type, index_t y, index_t x)
246 {
247  m_file->pop_object();
248  return true;
249 }
250 
251 bool
252 SerializableJsonReader00::read_sgserializable_begin_wrapped(
253  const TSGDataType* type, char* sgserializable_name,
254  EPrimitiveType* generic)
255 {
256  json_object* m = m_file->m_stack_stream.back();
257 
258  if (m == NULL || json_object_is_type(m, json_type_null)) {
259  *sgserializable_name = '\0'; return true;
260  }
261 
262  if (!json_object_is_type(m, json_type_object)) return false;
263 
264  json_object* buf;
265  if (!m_file->get_object(&buf, m, STR_KEY_INSTANCE_NAME,
266  json_type_string)) return false;
267  strncpy(sgserializable_name, json_object_get_string(buf),
268  STRING_LEN);
269 
270  if (m_file->get_object(&buf, m, STR_KEY_GENERIC_NAME,
271  json_type_string)) {
273  generic, json_object_get_string(buf))) return false;
274  }
275 
276  if (!m_file->get_object(&buf, m, STR_KEY_INSTANCE,
277  json_type_object)) return false;
278  m_file->push_object(buf);
279 
280  return true;
281 }
282 
283 bool
284 SerializableJsonReader00::read_sgserializable_end_wrapped(
285  const TSGDataType* type, const char* sgserializable_name,
286  EPrimitiveType generic)
287 {
288  if (*sgserializable_name == '\0') return true;
289 
290  m_file->pop_object();
291  return true;
292 }
293 
294 bool
295 SerializableJsonReader00::read_type_begin_wrapped(
296  const TSGDataType* type, const char* name, const char* prefix)
297 {
298  json_object* m = m_file->m_stack_stream.back();
299 
300  if (!json_object_is_type(m, json_type_object)) return false;
301 
302  json_object* buf_type;
303  if (!m_file->get_object(&buf_type, m, name, json_type_object))
304  return false;
305 
306  string_t str_buf; json_object* buf;
307  type->to_string(str_buf, STRING_LEN);
308  if (!m_file->get_object(&buf, buf_type, STR_KEY_TYPE,
309  json_type_string)) return false;
310  if (strcmp(str_buf, json_object_get_string(buf)) != 0)
311  return false;
312 
313  // data (and so buf) can be NULL for empty objects
314  m_file->get_object_any(&buf, buf_type, STR_KEY_DATA);
315  m_file->push_object(buf);
316 
317  return true;
318 }
319 
320 bool
321 SerializableJsonReader00::read_type_end_wrapped(
322  const TSGDataType* type, const char* name, const char* prefix)
323 {
324  m_file->pop_object();
325  return true;
326 }
327 
328 #endif /* HAVE_JSON */

SHOGUN Machine Learning Toolbox - Documentation