SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SerializableJsonFile.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 
16 
17 #define STR_KEY_FILETYPE "filetype"
18 #define STR_FILETYPE_00 \
19  "_SHOGUN_SERIALIZABLE_JSON_FILE_V_00_"
20 
21 using namespace shogun;
22 
23 CSerializableJsonFile::CSerializableJsonFile()
24  :CSerializableFile() { init(""); }
25 
26 CSerializableJsonFile::CSerializableJsonFile(const char* fname, char rw)
28 {
29  CSerializableFile::init(NULL, rw, fname);
30  init(fname);
31 }
32 
33 CSerializableJsonFile::~CSerializableJsonFile()
34 {
35  close();
36 }
37 
39 CSerializableJsonFile::new_reader(char* dest_version, size_t n)
40 {
41  const char* ftype;
42  json_object* buf;
43 
44  if ((buf = json_object_object_get(
45  m_stack_stream.back(), STR_KEY_FILETYPE)) == NULL
46  || is_error(buf)
47  || (ftype = json_object_get_string(buf)) == NULL)
48  return NULL;
49 
50  strncpy(dest_version, ftype, n);
51 
52  if (strcmp(STR_FILETYPE_00, dest_version) == 0)
53  return new SerializableJsonReader00(this);
54 
55  return NULL;
56 }
57 
58 void
59 CSerializableJsonFile::push_object(json_object* o)
60 { m_stack_stream.push_back(o); json_object_get(o); }
61 
62 void
63 CSerializableJsonFile::pop_object()
64 { json_object_put(m_stack_stream.back()); m_stack_stream.pop_back(); }
65 
66 bool
67 CSerializableJsonFile::get_object_any(
68  json_object** dest, json_object* src, const char* key)
69 {
70  *dest = json_object_object_get(src, key);
71 
72  return !is_error(*dest);
73 }
74 
75 bool
76 CSerializableJsonFile::get_object(json_object** dest, json_object* src,
77  const char* key, json_type t)
78 {
79  *dest = json_object_object_get(src, key);
80 
81  return *dest != NULL && !is_error(*dest)
82  && json_object_is_type(*dest, t);
83 }
84 
85 void
86 CSerializableJsonFile::init(const char* fname)
87 {
88  if (m_filename == NULL || *m_filename == '\0') {
89  SG_WARNING("Filename not given for opening file!\n");
90  close(); return;
91  }
92 
93  json_object* buf;
94  switch (m_task) {
95  case 'r':
96  buf = json_object_from_file((char*) fname);
97  if (is_error(buf)) {
98  SG_WARNING("Could not open file `%s' for reading!\n",
99  fname);
100  return;
101  }
102  push_object(buf);
103  break;
104  case 'w':
105  push_object(json_object_new_object());
106 
107  buf = json_object_new_string(STR_FILETYPE_00);
108  json_object_object_add(m_stack_stream.back(),
109  STR_KEY_FILETYPE, buf);
110  break;
111  default:
112  SG_WARNING("Could not open file `%s', unknown mode!\n",
113  m_filename);
114  close(); return;
115  }
116 }
117 
118 void
119 CSerializableJsonFile::close()
120 {
121  while (m_stack_stream.get_num_elements() > 1)
122  pop_object();
123 
124  if (m_stack_stream.get_num_elements() == 1) {
125  if (m_task == 'w'
126  && is_error(
127  json_object_to_file(m_filename, m_stack_stream.back())
128  )) {
129  SG_WARNING("Could not close file `%s' for writing!\n",
130  m_filename);
131  }
132 
133  pop_object();
134  }
135 }
136 
137 bool
138 CSerializableJsonFile::is_opened()
139 {
140  return m_stack_stream.get_num_elements() > 0;
141 }
142 
143 bool
144 CSerializableJsonFile::write_scalar_wrapped(
145  const TSGDataType* type, const void* param)
146 {
147  switch (type->m_ptype) {
148  case PT_BOOL:
149  push_object(json_object_new_boolean(*(bool*) param));
150  break;
151  case PT_CHAR:
152  push_object(json_object_new_int((int) *(char*) param));
153  break;
154  case PT_INT8:
155  push_object(json_object_new_int((int) *(int8_t*) param));
156  break;
157  case PT_UINT8:
158  push_object(json_object_new_int((int) *(uint8_t*) param));
159  break;
160  case PT_INT16:
161  push_object(json_object_new_int((int) *(int16_t*) param));
162  break;
163  case PT_UINT16:
164  push_object(json_object_new_int((int) *(uint16_t*) param));
165  break;
166  case PT_INT32:
167  push_object(json_object_new_int((int) *(int32_t*) param));
168  break;
169  case PT_UINT32:
170  push_object(json_object_new_int((int) *(uint32_t*) param));
171  break;
172  case PT_INT64:
173  push_object(json_object_new_int((int) *(int64_t*) param));
174  break;
175  case PT_UINT64:
176  push_object(json_object_new_int((int) *(uint64_t*) param));
177  break;
178  case PT_FLOAT32:
179  push_object(json_object_new_double(
180  (double) *(float32_t*) param));
181  break;
182  case PT_FLOAT64:
183  push_object(json_object_new_double(
184  (double) *(float64_t*) param));
185  break;
186  case PT_FLOATMAX:
187  push_object(json_object_new_double(
188  (double) *(floatmax_t*) param));
189  break;
190  case PT_SGOBJECT:
191  SG_ERROR("write_scalar_wrapped(): Implementation error during"
192  " writing JsonFile!");
193  return false;
194  }
195 
196  if (is_error(m_stack_stream.back())) return false;
197 
198  return true;
199 }
200 
201 bool
202 CSerializableJsonFile::write_cont_begin_wrapped(
203  const TSGDataType* type, index_t len_real_y, index_t len_real_x)
204 {
205  push_object(json_object_new_array());
206 
207  for (index_t i=0; i<len_real_x && (type->m_ctype==CT_MATRIX || type->m_ctype==CT_SGMATRIX); i++)
208  json_object_array_add(m_stack_stream.back(),
209  json_object_new_array());
210 
211  return true;
212 }
213 
214 bool
215 CSerializableJsonFile::write_cont_end_wrapped(
216  const TSGDataType* type, index_t len_real_y, index_t len_real_x)
217 {
218  return true;
219 }
220 
221 bool
222 CSerializableJsonFile::write_string_begin_wrapped(
223  const TSGDataType* type, index_t length)
224 {
225  push_object(json_object_new_array());
226 
227  return true;
228 }
229 
230 bool
231 CSerializableJsonFile::write_string_end_wrapped(
232  const TSGDataType* type, index_t length)
233 {
234  return true;
235 }
236 
237 bool
238 CSerializableJsonFile::write_stringentry_begin_wrapped(
239  const TSGDataType* type, index_t y)
240 {
241  return true;
242 }
243 
244 bool
245 CSerializableJsonFile::write_stringentry_end_wrapped(
246  const TSGDataType* type, index_t y)
247 {
248  json_object* array = m_stack_stream.get_element(
249  m_stack_stream.get_num_elements() - 2);
250 
251  if (is_error(json_object_array_put_idx(
252  array, y, m_stack_stream.back()))) return false;
253 
254  pop_object();
255  return true;
256 }
257 
258 bool
259 CSerializableJsonFile::write_sparse_begin_wrapped(
260  const TSGDataType* type, index_t length)
261 {
262  push_object(json_object_new_object());
263 
264  json_object* buf = json_object_new_array();
265  if (is_error(buf)) return false;
266  json_object_object_add(m_stack_stream.back(),
267  STR_KEY_SPARSE_FEATURES, buf);
268 
269  push_object(buf);
270  return true;
271 }
272 
273 bool
274 CSerializableJsonFile::write_sparse_end_wrapped(
275  const TSGDataType* type, index_t length)
276 {
277  pop_object();
278  return true;
279 }
280 
281 bool
282 CSerializableJsonFile::write_sparseentry_begin_wrapped(
283  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
284  index_t feat_index, index_t y)
285 {
286  json_object* buf = json_object_new_object();
287  if (is_error(json_object_array_put_idx(m_stack_stream.back(), y,
288  buf))) return false;
289  push_object(buf);
290 
291  buf = json_object_new_int(feat_index);
292  if (is_error(buf)) return false;
293  json_object_object_add(m_stack_stream.back(),
294  STR_KEY_SPARSE_FEATINDEX, buf);
295 
296  return true;
297 }
298 
299 bool
300 CSerializableJsonFile::write_sparseentry_end_wrapped(
301  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
302  index_t feat_index, index_t y)
303 {
304  json_object* o = m_stack_stream.get_element(
305  m_stack_stream.get_num_elements() - 2);
306 
307  json_object_object_add(o, STR_KEY_SPARSE_ENTRY,
308  m_stack_stream.back());
309 
310  pop_object(); pop_object();
311  return true;
312 }
313 
314 bool
315 CSerializableJsonFile::write_item_begin_wrapped(
316  const TSGDataType* type, index_t y, index_t x)
317 {
318  return true;
319 }
320 
321 bool
322 CSerializableJsonFile::write_item_end_wrapped(
323  const TSGDataType* type, index_t y, index_t x)
324 {
325  json_object* array = m_stack_stream.get_element(
326  m_stack_stream.get_num_elements() - 2);
327 
328  if (type->m_ctype==CT_MATRIX || type->m_ctype==CT_SGMATRIX)
329  array = json_object_array_get_idx(array, x);
330 
331  json_object_array_put_idx(array, y, m_stack_stream.back());
332 
333  pop_object();
334  return true;
335 }
336 
337 bool
338 CSerializableJsonFile::write_sgserializable_begin_wrapped(
339  const TSGDataType* type, const char* sgserializable_name,
340  EPrimitiveType generic)
341 {
342  if (*sgserializable_name == '\0') {
343  push_object(NULL); return true;
344  }
345 
346  push_object(json_object_new_object());
347 
348  json_object* buf;
349  buf = json_object_new_string(sgserializable_name);
350  if (is_error(buf)) return false;
351  json_object_object_add(m_stack_stream.back(),
352  STR_KEY_INSTANCE_NAME, buf);
353 
354  if (generic != PT_NOT_GENERIC) {
355  string_t buf_str;
356  TSGDataType::ptype_to_string(buf_str, generic, STRING_LEN);
357  buf = json_object_new_string(buf_str);
358  if (is_error(buf)) return false;
359  json_object_object_add(m_stack_stream.back(),
360  STR_KEY_GENERIC_NAME, buf);
361  }
362 
363  buf = json_object_new_object();
364  if (is_error(buf)) return false;
365  json_object_object_add(m_stack_stream.back(), STR_KEY_INSTANCE,
366  buf);
367  push_object(buf);
368 
369  return true;
370 }
371 
372 bool
373 CSerializableJsonFile::write_sgserializable_end_wrapped(
374  const TSGDataType* type, const char* sgserializable_name,
375  EPrimitiveType generic)
376 {
377  if (*sgserializable_name == '\0') return true;
378 
379  pop_object();
380  return true;
381 }
382 
383 bool
384 CSerializableJsonFile::write_type_begin_wrapped(
385  const TSGDataType* type, const char* name, const char* prefix)
386 {
387  json_object* buf = json_object_new_object();
388  if (is_error(buf)) return false;
389 
390  json_object_object_add(m_stack_stream.back(), name, buf);
391  push_object(buf);
392 
393  string_t str_buf;
394  type->to_string(str_buf, STRING_LEN);
395  buf = json_object_new_string(str_buf);
396  if (is_error(buf)) return false;
397  json_object_object_add(m_stack_stream.back(), STR_KEY_TYPE, buf);
398 
399  return true;
400 }
401 
402 bool
403 CSerializableJsonFile::write_type_end_wrapped(
404  const TSGDataType* type, const char* name, const char* prefix)
405 {
406  json_object_object_add(
407  m_stack_stream.get_element(
408  m_stack_stream.get_num_elements() - 2), STR_KEY_DATA,
409  m_stack_stream.back());
410  pop_object();
411 
412  pop_object();
413  return true;
414 }
415 
416 #endif /* HAVE_JSON */

SHOGUN Machine Learning Toolbox - Documentation