SerializableJsonReader00.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2010 Soeren Sonnenburg
00008  * Copyright (C) 2010 Berlin Institute of Technology
00009  */
00010 
00011 #include <shogun/lib/config.h>
00012 #ifdef HAVE_JSON
00013 
00014 #include <shogun/io/SerializableJsonReader00.h>
00015 
00016 using namespace shogun;
00017 
00018 SerializableJsonReader00::SerializableJsonReader00(
00019     CSerializableJsonFile* file) { m_file = file; }
00020 
00021 SerializableJsonReader00::~SerializableJsonReader00() {}
00022 
00023 bool
00024 SerializableJsonReader00::read_scalar_wrapped(
00025     const TSGDataType* type, void* param)
00026 {
00027     json_object* m = m_file->m_stack_stream.back();
00028 
00029     switch (type->m_ptype) {
00030     case PT_BOOL:
00031         if (!json_object_is_type(m, json_type_boolean)) return false;
00032         *(bool*) param = json_object_get_boolean(m);
00033         break;
00034     case PT_CHAR:
00035         if (!json_object_is_type(m, json_type_int)) return false;
00036         *(char*) param = json_object_get_int(m);
00037         break;
00038     case PT_INT8:
00039         if (!json_object_is_type(m, json_type_int)) return false;
00040         *(int8_t*) param = json_object_get_int(m);
00041         break;
00042     case PT_UINT8:
00043         if (!json_object_is_type(m, json_type_int)) return false;
00044         *(uint8_t*) param = json_object_get_int(m);
00045         break;
00046     case PT_INT16:
00047         if (!json_object_is_type(m, json_type_int)) return false;
00048         *(int16_t*) param = json_object_get_int(m);
00049         break;
00050     case PT_UINT16:
00051         if (!json_object_is_type(m, json_type_int)) return false;
00052         *(uint16_t*) param = json_object_get_int(m);
00053         break;
00054     case PT_INT32:
00055         if (!json_object_is_type(m, json_type_int)) return false;
00056         *(int32_t*) param = json_object_get_int(m);
00057         break;
00058     case PT_UINT32:
00059         if (!json_object_is_type(m, json_type_int)) return false;
00060         *(uint32_t*) param = json_object_get_int(m);
00061         break;
00062     case PT_INT64:
00063         if (!json_object_is_type(m, json_type_int)) return false;
00064         *(int64_t*) param = json_object_get_int(m);
00065         break;
00066     case PT_UINT64:
00067         if (!json_object_is_type(m, json_type_int)) return false;
00068         *(uint64_t*) param = json_object_get_int(m);
00069         break;
00070     case PT_FLOAT32:
00071         if (!json_object_is_type(m, json_type_double)) return false;
00072         *(float32_t*) param = json_object_get_double(m);
00073         break;
00074     case PT_FLOAT64:
00075         if (!json_object_is_type(m, json_type_double)) return false;
00076         *(float64_t*) param = json_object_get_double(m);
00077         break;
00078     case PT_FLOATMAX:
00079         if (!json_object_is_type(m, json_type_double)) return false;
00080         *(floatmax_t*) param = json_object_get_double(m);
00081         break;
00082     case PT_SGOBJECT:
00083         SG_ERROR("write_scalar_wrapped(): Implementation error during"
00084                  " writing JsonFile!");
00085         return false;
00086     }
00087 
00088     return true;
00089 }
00090 
00091 bool
00092 SerializableJsonReader00::read_cont_begin_wrapped(
00093     const TSGDataType* type, index_t* len_read_y, index_t* len_read_x)
00094 {
00095     json_object* m = m_file->m_stack_stream.back();
00096 
00097     if (!json_object_is_type(m, json_type_array)) return false;
00098 
00099     *len_read_y = json_object_array_length(m);
00100 
00101     if (type->m_ctype==CT_MATRIX || type->m_ctype==CT_SGMATRIX) {
00102         *len_read_x = *len_read_y;
00103         for (index_t i=0; i<*len_read_x; i++) {
00104             json_object* buf = json_object_array_get_idx(m, i);
00105             if (!json_object_is_type(buf, json_type_array))
00106                 return false;
00107 
00108             index_t len = json_object_array_length(buf);
00109             if (i == 0) *len_read_y = len;
00110             else if (*len_read_y != len) return false;
00111         }
00112     }
00113 
00114     return true;
00115 }
00116 
00117 bool
00118 SerializableJsonReader00::read_cont_end_wrapped(
00119     const TSGDataType* type, index_t len_read_y, index_t len_read_x)
00120 {
00121     return true;
00122 }
00123 
00124 bool
00125 SerializableJsonReader00::read_string_begin_wrapped(
00126     const TSGDataType* type, index_t* length)
00127 {
00128     json_object* m = m_file->m_stack_stream.back();
00129 
00130     if (!json_object_is_type(m, json_type_array)) return false;
00131 
00132     *length = json_object_array_length(m);
00133 
00134     return true;
00135 }
00136 
00137 bool
00138 SerializableJsonReader00::read_string_end_wrapped(
00139     const TSGDataType* type, index_t length)
00140 {
00141     return true;
00142 }
00143 
00144 bool
00145 SerializableJsonReader00::read_stringentry_begin_wrapped(
00146     const TSGDataType* type, index_t y)
00147 {
00148     json_object* m = m_file->m_stack_stream.back();
00149 
00150     json_object* buf = json_object_array_get_idx(m, y);
00151     if (is_error(buf)) return false;
00152 
00153     m_file->push_object(buf);
00154     return true;
00155 }
00156 
00157 bool
00158 SerializableJsonReader00::read_stringentry_end_wrapped(
00159     const TSGDataType* type, index_t y)
00160 {
00161     m_file->pop_object();
00162     return true;
00163 }
00164 
00165 bool
00166 SerializableJsonReader00::read_sparse_begin_wrapped(
00167     const TSGDataType* type, index_t* length)
00168 {
00169     json_object* m = m_file->m_stack_stream.back();
00170 
00171     if (!json_object_is_type(m, json_type_object)) return false;
00172 
00173     json_object* buf;
00174     if (!m_file->get_object(&buf, m, STR_KEY_SPARSE_FEATURES,
00175                             json_type_array)) return false;
00176     *length = json_object_array_length(buf);
00177     m_file->push_object(buf);
00178 
00179     return true;
00180 }
00181 
00182 bool
00183 SerializableJsonReader00::read_sparse_end_wrapped(
00184     const TSGDataType* type, index_t length)
00185 {
00186     m_file->pop_object();
00187     return true;
00188 }
00189 
00190 bool
00191 SerializableJsonReader00::read_sparseentry_begin_wrapped(
00192     const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
00193     index_t* feat_index, index_t y)
00194 {
00195     json_object* m = m_file->m_stack_stream.back();
00196 
00197     json_object* buf_obj
00198         = json_object_array_get_idx(m, y);
00199     if (is_error(buf_obj)) return false;
00200     if (!json_object_is_type(buf_obj, json_type_object)) return false;
00201 
00202     json_object* buf;
00203     if (!m_file->get_object(&buf, buf_obj, STR_KEY_SPARSE_FEATINDEX,
00204                             json_type_int)) return false;
00205     *feat_index = json_object_get_int(buf);
00206 
00207     if (!m_file->get_object_any(&buf, buf_obj, STR_KEY_SPARSE_ENTRY))
00208         return false;
00209     m_file->push_object(buf);
00210 
00211     return true;
00212 }
00213 
00214 bool
00215 SerializableJsonReader00::read_sparseentry_end_wrapped(
00216     const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
00217     index_t* feat_index, index_t y)
00218 {
00219     m_file->pop_object();
00220     return true;
00221 }
00222 
00223 bool
00224 SerializableJsonReader00::read_item_begin_wrapped(
00225     const TSGDataType* type, index_t y, index_t x)
00226 {
00227     json_object* m = m_file->m_stack_stream.back();
00228 
00229     if (type->m_ctype==CT_MATRIX || type->m_ctype==CT_SGMATRIX)
00230         m = json_object_array_get_idx(m, x);
00231     m = json_object_array_get_idx(m, y);
00232 
00233     m_file->push_object(m);
00234     return true;
00235 }
00236 
00237 bool
00238 SerializableJsonReader00::read_item_end_wrapped(
00239     const TSGDataType* type, index_t y, index_t x)
00240 {
00241     m_file->pop_object();
00242     return true;
00243 }
00244 
00245 bool
00246 SerializableJsonReader00::read_sgserializable_begin_wrapped(
00247     const TSGDataType* type, char* sgserializable_name,
00248     EPrimitiveType* generic)
00249 {
00250     json_object* m = m_file->m_stack_stream.back();
00251 
00252     if (m == NULL || json_object_is_type(m, json_type_null)) {
00253         *sgserializable_name = '\0'; return true;
00254     }
00255 
00256     if (!json_object_is_type(m, json_type_object)) return false;
00257 
00258     json_object* buf;
00259     if (!m_file->get_object(&buf, m, STR_KEY_INSTANCE_NAME,
00260                             json_type_string)) return false;
00261     strncpy(sgserializable_name, json_object_get_string(buf),
00262             STRING_LEN);
00263 
00264     if (m_file->get_object(&buf, m, STR_KEY_GENERIC_NAME,
00265                            json_type_string)) {
00266         if (!TSGDataType::string_to_ptype(
00267                 generic, json_object_get_string(buf))) return false;
00268     }
00269 
00270     if (!m_file->get_object(&buf, m, STR_KEY_INSTANCE,
00271                             json_type_object)) return false;
00272     m_file->push_object(buf);
00273 
00274     return true;
00275 }
00276 
00277 bool
00278 SerializableJsonReader00::read_sgserializable_end_wrapped(
00279     const TSGDataType* type, const char* sgserializable_name,
00280     EPrimitiveType generic)
00281 {
00282     if (*sgserializable_name == '\0') return true;
00283 
00284     m_file->pop_object();
00285     return true;
00286 }
00287 
00288 bool
00289 SerializableJsonReader00::read_type_begin_wrapped(
00290     const TSGDataType* type, const char* name, const char* prefix)
00291 {
00292     json_object* m = m_file->m_stack_stream.back();
00293 
00294     if (!json_object_is_type(m, json_type_object)) return false;
00295 
00296     json_object* buf_type;
00297     if (!m_file->get_object(&buf_type, m, name, json_type_object))
00298         return false;
00299 
00300     string_t str_buf; json_object* buf;
00301     type->to_string(str_buf, STRING_LEN);
00302     if (!m_file->get_object(&buf, buf_type, STR_KEY_TYPE,
00303                             json_type_string)) return false;
00304     if (strcmp(str_buf, json_object_get_string(buf)) != 0)
00305         return false;
00306 
00307     if (!m_file->get_object_any(&buf, buf_type, STR_KEY_DATA))
00308         return false;
00309     m_file->push_object(buf);
00310 
00311     return true;
00312 }
00313 
00314 bool
00315 SerializableJsonReader00::read_type_end_wrapped(
00316     const TSGDataType* type, const char* name, const char* prefix)
00317 {
00318     m_file->pop_object();
00319     return true;
00320 }
00321 
00322 #endif /* HAVE_JSON  */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation