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

SHOGUN Machine Learning Toolbox - Documentation