KisaragiLibrary
 
読み取り中…
検索中…
一致する文字列を見つけられません
Kisaragi_Lib::SerializableObject クラス

自身の監視対象データをJson形式にシリアライズしたり、 Json形式からデシリアライズできる基底データクラス [詳解]

#include <JsonUtility.hpp>

Kisaragi_Lib::SerializableObject の継承関係図
MyClass01 MyClass02

公開メンバ関数

std::ostream & ToJson (std::ostream &_file, unsigned int _depth)
 
 SerializableObject (std::initializer_list< SerializableStruct > _in)
 
void ToJson (const fs::path &_fileName, const fs::path &_directry="./jsonFiles")
 
void FormJson (std::string _json)
 
void FormJson (Json _json)
 

非公開変数類

std::vector< SerializableStructvec
 

詳解

自身の監視対象データをJson形式にシリアライズしたり、 Json形式からデシリアライズできる基底データクラス

監視対象にする方法
{
public:
int a;
int b;
bool c;
/// <summary>
/// コンストラクタでJson化する変数を登録,
/// int,
/// bool,
/// float,
/// string,
/// SerializableStructのみ
/// </summary>
})
{
}
};
class MyClass2 : public SerializableObject
{
public:
int a;
int b;
bool c;
MyClass aaa;
/// <summary>
/// コンストラクタでJson化する変数を登録,
/// </summary>
MyClass2() :SerializableObject({
})
{
}
};
#define SERIALIZABLE(_in)
SerializableObjectに変数を登録する際に扱うマクロ
Definition JsonUtility.hpp:109
SerializableObject(std::initializer_list< SerializableStruct > _in)
Definition JsonUtility.hpp:198
Definition Sample_Accessor.cpp:7
から
ver1.0.0 コメント追加

構築子と解体子

◆ SerializableObject()

Kisaragi_Lib::SerializableObject::SerializableObject ( std::initializer_list< SerializableStruct > _in)
inline
198 : vec{ _in }
199 {
200
201 }
std::vector< SerializableStruct > vec
Definition JsonUtility.hpp:170

参照元 MyClass01::MyClass01(), MyClass02::MyClass02().

関数詳解

◆ ToJson() [1/2]

std::ostream & Kisaragi_Lib::SerializableObject::ToJson ( std::ostream & _file,
unsigned int _depth )
inline
176 {
177 std::string indent(_depth, '\t');
178 _file << "{" << std::endl;
179
180 for (size_t i = 0; i < vec.size(); ++i)
181 {
182 _file << indent << " " << vec[i];
183
184 if (i < vec.size() - 1)
185 {
186 _file << ",";
187 }
188
189 _file << std::endl;
190 }
191
192 _file << indent << "}";
193
194 return _file;
195 }

参照元 Main().

◆ ToJson() [2/2]

void Kisaragi_Lib::SerializableObject::ToJson ( const fs::path & _fileName,
const fs::path & _directry = "./jsonFiles" )
inline
204 {
205 if (!fs::exists(_directry))
206 {
207 try
208 {
209 fs::create_directories(_directry);
210 }
211 catch (const fs::filesystem_error& e)
212 {
213 Debug::PrintAssertStatic("エラー : ファイルシステムでエラーが発生しました");
214 return;
215 }
216 }
217
218 // フォルダ位置
219 fs::path filePass = _directry;
220 // /= /を追加(ない場合のみ)
221 // ディレクトリ + ファイル名
222 filePass /= _fileName;
223 // 拡張子がない場合つける
224 filePass.replace_extension(".json");
225
226 std::ofstream file(filePass, std::ios::out | std::ios::trunc); // ファイルを開く(新規作成・上書き)
227
228 if (!file)
229 {
230 Debug::PrintAssertStatic("エラー: ファイルが開けませんでした");
231 return;
232 }
233
234
235 file << "{" <<std::endl;
236
237 for (size_t i = 0; i < vec.size(); ++i)
238 {
239 file << " " << vec[i];
240
241 if (i < vec.size() - 1)
242 {
243 file << ",";
244 }
245
246 file << std::endl;
247 }
248
249 file << "}";
250
251 file.close();
252 }
static void PrintAssertStatic(const std::string _in)
エラー出力用コンソール出力(文字色しか変わらない)
Definition Debug.cpp:152

◆ FormJson() [1/2]

void Kisaragi_Lib::SerializableObject::FormJson ( std::string _json)
inline
255 {
256 Json json = JsonParser{ _json }.Parse();
257
258 for (auto& value : vec)
259 {
260 value.FormJson(json);
261 }
262 }

参照元 Main().

◆ FormJson() [2/2]

void Kisaragi_Lib::SerializableObject::FormJson ( Json _json)
inline
265 {
266 for (auto& value : vec)
267 {
268 value.FormJson(_json);
269 }
270 }

メンバ詳解

◆ vec

std::vector<SerializableStruct> Kisaragi_Lib::SerializableObject::vec
private

このクラス詳解は次のファイルから抽出されました: