SerializableJsonFile.h

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 #ifndef __SERIALIZABLE_JSON_FILE_H__
00011 #define __SERIALIZABLE_JSON_FILE_H__
00012 
00013 #include <shogun/lib/config.h>
00014 #ifdef HAVE_JSON
00015 
00016 #include <json.h>
00017 
00018 #include <shogun/io/SerializableFile.h>
00019 #include <shogun/base/DynArray.h>
00020 
00021 #define STR_KEY_TYPE               "type"
00022 #define STR_KEY_DATA               "data"
00023 #define STR_KEY_INSTANCE_NAME      "instance_name"
00024 #define STR_KEY_INSTANCE           "instance"
00025 #define STR_KEY_GENERIC_NAME       "generic_name"
00026 #define STR_KEY_SPARSE_VECINDEX    "vec_index"
00027 #define STR_KEY_SPARSE_FEATURES    "features"
00028 #define STR_KEY_SPARSE_FEATINDEX   "feat_index"
00029 #define STR_KEY_SPARSE_ENTRY       "entry"
00030 
00031 namespace shogun
00032 {
00033 #define IGNORE_IN_CLASSLIST
00034 IGNORE_IN_CLASSLIST class CSerializableJsonFile
00035     :public CSerializableFile
00036 {
00037     friend class SerializableJsonReader00;
00038 
00039     DynArray<json_object*> m_stack_stream;
00040 
00041     void init(const char* fname);
00042     void push_object(json_object* o);
00043     void pop_object(void);
00044 
00045     static bool get_object_any(json_object** dest, json_object* src,
00046                                const char* key);
00047     static bool get_object(json_object** dest, json_object* src,
00048                            const char* key, json_type t);
00049 
00050 protected:
00051     virtual TSerializableReader* new_reader(
00052         char* dest_version, size_t n);
00053 
00054     virtual bool write_scalar_wrapped(
00055         const TSGDataType* type, const void* param);
00056 
00057     virtual bool write_cont_begin_wrapped(
00058         const TSGDataType* type, index_t len_real_y,
00059         index_t len_real_x);
00060     virtual bool write_cont_end_wrapped(
00061         const TSGDataType* type, index_t len_real_y,
00062         index_t len_real_x);
00063 
00064     virtual bool write_string_begin_wrapped(
00065         const TSGDataType* type, index_t length);
00066     virtual bool write_string_end_wrapped(
00067         const TSGDataType* type, index_t length);
00068 
00069     virtual bool write_stringentry_begin_wrapped(
00070         const TSGDataType* type, index_t y);
00071     virtual bool write_stringentry_end_wrapped(
00072         const TSGDataType* type, index_t y);
00073 
00074     virtual bool write_sparse_begin_wrapped(
00075         const TSGDataType* type, index_t vec_index,
00076         index_t length);
00077     virtual bool write_sparse_end_wrapped(
00078         const TSGDataType* type, index_t vec_index,
00079         index_t length);
00080 
00081     virtual bool write_sparseentry_begin_wrapped(
00082         const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
00083         index_t feat_index, index_t y);
00084     virtual bool write_sparseentry_end_wrapped(
00085         const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
00086         index_t feat_index, index_t y);
00087 
00088     virtual bool write_item_begin_wrapped(
00089         const TSGDataType* type, index_t y, index_t x);
00090     virtual bool write_item_end_wrapped(
00091         const TSGDataType* type, index_t y, index_t x);
00092 
00093     virtual bool write_sgserializable_begin_wrapped(
00094         const TSGDataType* type, const char* sgserializable_name,
00095         EPrimitiveType generic);
00096     virtual bool write_sgserializable_end_wrapped(
00097         const TSGDataType* type, const char* sgserializable_name,
00098         EPrimitiveType generic);
00099 
00100     virtual bool write_type_begin_wrapped(
00101         const TSGDataType* type, const char* name,
00102         const char* prefix);
00103     virtual bool write_type_end_wrapped(
00104         const TSGDataType* type, const char* name,
00105         const char* prefix);
00106 
00107 public:
00109     explicit CSerializableJsonFile(void);
00110 
00116     explicit CSerializableJsonFile(const char* fname, char rw='r');
00117 
00119     virtual ~CSerializableJsonFile();
00120 
00122     inline virtual const char* get_name() const {
00123         return "SerializableJsonFile";
00124     }
00125 
00126     virtual void close(void);
00127     virtual bool is_opened(void);
00128 };
00129 }
00130 #endif /* HAVE_JSON  */
00131 #endif /* __SERIALIZABLE_JSON_FILE_H__  */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation