KisaragiLibrary
 
読み取り中…
検索中…
一致する文字列を見つけられません
JsonUtility.hpp
[詳解]
1#pragma once
2
3/**
4 * @file JsonUtility.hpp
5 * @brief Json形式のデータをシリアライズ/デシリアライズ可能なデータ型を提供します
6 * @author 樺澤陽向
7 * @date 2025/05/08
8 * @since ver1.0.0 コメント追加
9 */
10
11#include <string>
12#include <vector>
13#include "Json.hpp"
14#include <type_traits>
15#include <initializer_list>
16#include <filesystem>
17
18
19namespace Kisaragi_Lib
20{
22
23 /// <summary>
24 /// SerializableObject内に格納されるインナークラス。
25 /// 使用者側で直接いじることはないと思いますので、
26 /// 興味がある場合は見るくらいでいいと思います。
27 /// </summary>
28 /// @since ver1.0.0 コメント追加
30 {
31 enum class ValueType
32 {
39
40 //VECTOR,
41 };
42
43 /// <summary>
44 /// 値を管理する共用体
45 /// </summary>
46 using Value = std::variant
47 <
48 std::reference_wrapper<std::nullptr_t>,
49 std::reference_wrapper<bool>,
50 std::reference_wrapper<int>,
51 std::reference_wrapper<float>,
52 std::reference_wrapper<std::string>,
53
54 std::reference_wrapper<SerializableObject> //mapの場合はこっち使ってね.
55
56 //std::reference_wrapper<std::vector<SerializableStruct>> //配列の場合はこっち
57 >;
58
60 std::string name;
62
63
65
66 SerializableStruct(std::string& _str, std::string _name) : value{ std::ref(_str) }, name{ _name }, type{ ValueType::STRING }
67 {
68
69 }
70
71 explicit SerializableStruct(bool& _bool, std::string _name) : value{ std::ref(_bool) }, name{ _name }, type{ ValueType::BOOL }
72 {
73
74 }
75
76 explicit SerializableStruct(float& _float, std::string _name) : value{ std::ref(_float) }, name{ _name }, type{ ValueType::FLOAT }
77 {
78
79 }
80
81 explicit SerializableStruct(int& _int, std::string _name) : value{ std::ref(_int) }, name{ _name } , type{ ValueType::INT }
82 {
83
84 }
85
86 //暗黙的な"基底クラス"への変換はできるらしい
87 explicit SerializableStruct(SerializableObject& _serializableClass, std::string _name) : value{ std::ref(_serializableClass) }, name{ _name } , type{ ValueType::SERIALIZABLE_OBJECT }
88 {
89
90 }
91
92 //explicit SerializableStruct(std::vector<SerializableStruct>& _vec, std::string _name) : value{ _vec }, name{ _name }, type{ ValueType::VECTOR }
93 //{
94
95 //}
96
97
98 //自分が所属する階層のjsonを受け取る
99 void FormJson(Json _json);
100
101 void ToJson(std::ostream& os, int indentLevel) const;
102
103 };
104
105 /// <summary>
106 /// SerializableObjectに変数を登録する際に扱うマクロ
107 /// </summary>
108 /// @since ver1.0.0 コメント追加
109#define SERIALIZABLE(_in) ( SerializableStruct(std::ref(_in) , #_in ) )
110
111
112 // 出力用
113 std::ostream& operator<<(std::ostream& os, const SerializableStruct& ss);
114
115 /// <summary>
116 /// 自身の監視対象データをJson形式にシリアライズしたり、
117 /// Json形式からデシリアライズできる基底データクラス
118 /// </summary>
119 /// @code 監視対象にする方法
120 /// class MyClass : public SerializableObject
121 /// {
122 /// public:
123 /// int a;
124 /// int b;
125 /// bool c;
126 ///
127 /// /// <summary>
128 /// /// コンストラクタでJson化する変数を登録,
129 /// /// int,
130 /// /// bool,
131 /// /// float,
132 /// /// string,
133 /// /// SerializableStructのみ
134 /// /// </summary>
135 /// MyClass() :SerializableObject({
136 /// SERIALIZABLE(a),
137 /// SERIALIZABLE(b),
138 /// SERIALIZABLE(c)
139 /// })
140 /// {
141 /// }
142 /// };
143 ///
144 /// class MyClass2 : public SerializableObject
145 /// {
146 /// public:
147 /// int a;
148 /// int b;
149 /// bool c;
150 /// MyClass aaa;
151 ///
152 /// /// <summary>
153 /// /// コンストラクタでJson化する変数を登録,
154 /// /// </summary>
155 /// MyClass2() :SerializableObject({
156 /// SERIALIZABLE(a),
157 /// SERIALIZABLE(b),
158 /// SERIALIZABLE(c),
159 /// SERIALIZABLE(aaa),
160 /// })
161 /// {
162 /// }
163 ///
164 /// };
165 /// @endcode
166 /// @since ver1.0.0 コメント追加
168 {
169 private:
170 std::vector<SerializableStruct> vec;
171
172 public:
173
174 //入れ子構造の時の記述用
175 std::ostream& ToJson(std::ostream& _file ,unsigned int _depth)
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 }
196
197
198 SerializableObject(std::initializer_list<SerializableStruct> _in) : vec{ _in }
199 {
200
201 }
202
203 void ToJson(const fs::path& _fileName, const fs::path& _directry = "./jsonFiles")
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 }
253
254 void FormJson(std::string _json)
255 {
256 Json json = JsonParser{ _json }.Parse();
257
258 for (auto& value : vec)
259 {
260 value.FormJson(json);
261 }
262 }
263
264 void FormJson(Json _json)
265 {
266 for (auto& value : vec)
267 {
268 value.FormJson(_json);
269 }
270 }
271 };
272}
Json形式のデータを取り扱うためのヘッダ
static void PrintAssertStatic(const std::string _in)
エラー出力用コンソール出力(文字色しか変わらない)
Definition Debug.cpp:152
Jsonデータを取り扱うクラス
Definition Json.hpp:35
Json読み込みクラス
Definition Json.hpp:370
Json Parse()
読み込んだJsonファイルを変換し、Jsonクラスとして返します
Definition Json.hpp:411
自身の監視対象データをJson形式にシリアライズしたり、 Json形式からデシリアライズできる基底データクラス
Definition JsonUtility.hpp:168
std::vector< SerializableStruct > vec
Definition JsonUtility.hpp:170
void ToJson(const fs::path &_fileName, const fs::path &_directry="./jsonFiles")
Definition JsonUtility.hpp:203
void FormJson(Json _json)
Definition JsonUtility.hpp:264
std::ostream & ToJson(std::ostream &_file, unsigned int _depth)
Definition JsonUtility.hpp:175
SerializableObject(std::initializer_list< SerializableStruct > _in)
Definition JsonUtility.hpp:198
void FormJson(std::string _json)
Definition JsonUtility.hpp:254
Definition Accessor.hpp:110
std::ostream & operator<<(std::ostream &os, const SerializableStruct &ss)
Definition JsonUtility.cpp:82
std::string name
Definition JsonUtility.hpp:60
std::variant< std::reference_wrapper< std::nullptr_t >, std::reference_wrapper< bool >, std::reference_wrapper< int >, std::reference_wrapper< float >, std::reference_wrapper< std::string >, std::reference_wrapper< SerializableObject > > Value
値を管理する共用体
Definition JsonUtility.hpp:46
SerializableStruct(bool &_bool, std::string _name)
Definition JsonUtility.hpp:71
SerializableStruct(float &_float, std::string _name)
Definition JsonUtility.hpp:76
SerializableStruct(std::string &_str, std::string _name)
Definition JsonUtility.hpp:66
Value value
Definition JsonUtility.hpp:59
SerializableStruct(SerializableObject &_serializableClass, std::string _name)
Definition JsonUtility.hpp:87
ValueType type
Definition JsonUtility.hpp:61
void FormJson(Json _json)
Definition JsonUtility.cpp:5
void ToJson(std::ostream &os, int indentLevel) const
Definition JsonUtility.cpp:60
SerializableStruct(int &_int, std::string _name)
Definition JsonUtility.hpp:81
ValueType
Definition JsonUtility.hpp:32
@ NULLPTR_T
Definition JsonUtility.hpp:33
@ INT
Definition JsonUtility.hpp:35
@ STRING
Definition JsonUtility.hpp:37
@ SERIALIZABLE_OBJECT
Definition JsonUtility.hpp:38
@ BOOL
Definition JsonUtility.hpp:34
@ FLOAT
Definition JsonUtility.hpp:36