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

Jsonデータを取り扱うクラス [詳解]

#include <Json.hpp>

公開メンバ関数

 Json ()
 
 Json (std::string _str)
 
 Json (bool _bool)
 
 Json (float _float)
 
 Json (int _int)
 
 Json (const Json &other)=default
 
 Json (Json &&other) noexcept=default
 
Jsonoperator= (Json &&other) noexcept=default
 
Jsonoperator= (const Json &other)
 
template<class T>
const T & Get () const
 
template<class T>
T & Get ()
 
template<class T>
const bool IsType ()
 
template<class T>
Jsonoperator= (const T _in)
 
template<typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
Jsonoperator[] (const T _index)
 
Jsonoperator[] (const std::string &_key)
 
void Write (const fs::path &_fileName, const fs::path &_directry="./jsonFiles")
 自身をJsonファイルとして書き込みます
 

非公開型

using Value
 値を管理する共用体
 

非公開メンバ関数

void Write (std::ofstream &_file, unsigned int _depth=0)
 
void WriteNotText (std::ofstream &_file, unsigned int _depth=0)
 
void WriteText (std::ofstream &_file, unsigned int _depth=0)
 
void WriteObject (std::ofstream &_file, unsigned int _depth=0)
 
void WriteArray (std::ofstream &_file, unsigned int _depth=0)
 
void WriteJsonDepth (std::ofstream &_file, unsigned int _depth=0)
 

非公開変数類

const int TEXT_INDEX = 4
 
Value value
 

詳解

Jsonデータを取り扱うクラス

から
ver1.0.0 コメント追加

型定義メンバ詳解

◆ Value

初期値:
std::variant
<
std::nullptr_t,
bool,
int,
float,
std::string,
std::unordered_map<std::string, Json>,
std::vector<Json>
>

値を管理する共用体

構築子と解体子

◆ Json() [1/7]

Kisaragi_Lib::Json::Json ( )
inline
38 : value{nullptr}
39 {
40
41 }
Value value
Definition Json.hpp:362

参照元 Json(), Json(), operator=(), operator=(), operator=(), operator[](), operator[]().

◆ Json() [2/7]

Kisaragi_Lib::Json::Json ( std::string _str)
inline
43 : value{ _str }
44 {
45
46 }

◆ Json() [3/7]

Kisaragi_Lib::Json::Json ( bool _bool)
inlineexplicit
48 : value{ _bool }
49 {
50
51 }

◆ Json() [4/7]

Kisaragi_Lib::Json::Json ( float _float)
inlineexplicit
53 : value{ _float }
54 {
55
56 }

◆ Json() [5/7]

Kisaragi_Lib::Json::Json ( int _int)
inlineexplicit
58 : value{ _int }
59 {
60
61 }

◆ Json() [6/7]

Kisaragi_Lib::Json::Json ( const Json & other)
default

◆ Json() [7/7]

Kisaragi_Lib::Json::Json ( Json && other)
defaultnoexcept

関数詳解

◆ operator=() [1/3]

Json & Kisaragi_Lib::Json::operator= ( Json && other)
defaultnoexcept

◆ operator=() [2/3]

Json & Kisaragi_Lib::Json::operator= ( const Json & other)
inline
69 {
70 if (this != &other)
71 {
72 value = other.value;
73 }
74 return *this;
75 }

◆ Get() [1/2]

template<class T>
const T & Kisaragi_Lib::Json::Get ( ) const
inline
79 {
80 if (std::holds_alternative<T>(value))
81 {
82 return std::get<T>(value);
83 }
84
85 Debug::PrintAssertStatic("その型を保有していません");
86 }
static void PrintAssertStatic(const std::string _in)
エラー出力用コンソール出力(文字色しか変わらない)
Definition Debug.cpp:152

参照元 Kisaragi_Lib::SerializableStruct::FormJson().

◆ Get() [2/2]

template<class T>
T & Kisaragi_Lib::Json::Get ( )
inline
90 {
91 if (std::holds_alternative<T>(value))
92 {
93 return std::get<T>(value);
94 }
95
96 Debug::PrintAssertStatic("その型を保有していません");
97 }

◆ IsType()

template<class T>
const bool Kisaragi_Lib::Json::IsType ( )
inline
102 {
103 if (std::holds_alternative<T>(value))
104 {
105 return true;
106 }
107
108 return false;
109 }

◆ operator=() [3/3]

template<class T>
Json & Kisaragi_Lib::Json::operator= ( const T _in)
inline
113 {
114 value = _in;
115
116 return *this;
117 }

◆ operator[]() [1/2]

template<typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
Json & Kisaragi_Lib::Json::operator[] ( const T _index)
inline
122 {
123 int index{ _index };
124
125 if (!std::holds_alternative<std::vector<Json>>(value))
126 {
127 value = std::vector<Json>{};
128 }
129
130 // 配列が小さすぎる場合にresizeする
131 auto& vec = std::get<std::vector<Json>>(value);
132 if (index >= static_cast<int>(vec.size()))
133 {
134 vec.resize(index + 1);
135 }
136
137 return vec[index];
138 }

◆ operator[]() [2/2]

Json & Kisaragi_Lib::Json::operator[] ( const std::string & _key)
inline
141 {
142 if (!std::holds_alternative<std::unordered_map<std::string, Json>>(value))
143 {
144 value = std::unordered_map<std::string, Json>{};
145 }
146
147 return std::get<std::unordered_map<std::string,Json>>(value)[_key];
148 }

◆ Write() [1/2]

void Kisaragi_Lib::Json::Write ( const fs::path & _fileName,
const fs::path & _directry = "./jsonFiles" )
inline

自身をJsonファイルとして書き込みます

引数
_fileName保存ファイル名(拡張子の省略可能)
_directry保存先ディレクトリのパス デフォルト値は"./jsonFiles"
160 {
161 if (value.index() < 5)
162 {
163 Debug::PrintAssertStatic("エラー : 最上位におけるのは配列か連想配列のみです");
164 return;
165 }
166
167 if (!fs::exists(_directry))
168 {
169 try
170 {
171 fs::create_directories(_directry);
172 }
173 catch (const fs::filesystem_error& e)
174 {
175 Debug::PrintAssertStatic("エラー : ファイルシステムでエラーが発生しました");
176 return;
177 }
178 }
179
180 // フォルダ位置
181 fs::path filePass = _directry;
182 // /= /を追加(ない場合のみ)
183 // ディレクトリ + ファイル名
184 filePass /= _fileName;
185 // 拡張子がない場合つける
186 filePass.replace_extension(".json");
187
188 std::ofstream file(filePass, std::ios::out | std::ios::trunc); // ファイルを開く(新規作成・上書き)
189
190 if (!file)
191 {
192 Debug::PrintAssertStatic("エラー: ファイルが開けませんでした");
193 return;
194 }
195
196 this->Write(file);
197
198 file.close();
199 return;
200 }
void Write(const fs::path &_fileName, const fs::path &_directry="./jsonFiles")
自身をJsonファイルとして書き込みます
Definition Json.hpp:159

参照元 Write().

◆ Write() [2/2]

void Kisaragi_Lib::Json::Write ( std::ofstream & _file,
unsigned int _depth = 0 )
inlineprivate
207 {
208 if (value.index() < TEXT_INDEX)
209 {
210 WriteNotText(_file,_depth);
211 }
212 else if (value.index() == TEXT_INDEX)
213 {
214 WriteText(_file, _depth);
215 }
216 else if(value.index() == TEXT_INDEX + 1)
217 {
218 WriteObject(_file, _depth);
219 }
220 else
221 {
222 WriteArray(_file, _depth);
223 }
224 }
void WriteArray(std::ofstream &_file, unsigned int _depth=0)
Definition Json.hpp:302
const int TEXT_INDEX
Definition Json.hpp:360
void WriteObject(std::ofstream &_file, unsigned int _depth=0)
Definition Json.hpp:260
void WriteText(std::ofstream &_file, unsigned int _depth=0)
Definition Json.hpp:248
void WriteNotText(std::ofstream &_file, unsigned int _depth=0)
Definition Json.hpp:226

◆ WriteNotText()

void Kisaragi_Lib::Json::WriteNotText ( std::ofstream & _file,
unsigned int _depth = 0 )
inlineprivate
227 {
228 std::visit([&](auto&& arg)
229 {
230 using T = std::decay_t<decltype(arg)>;
231
232 if constexpr (std::is_same_v<T, std::nullptr_t>)
233 {
234 _file << "null";
235 }
236 else if constexpr (std::is_same_v<T, bool>)
237 {
238 _file << (arg ? "true" : "false");
239 }
240 else if constexpr (std::is_same_v<T, int> || std::is_same_v<T, float>)
241 {
242 _file << std::to_string(arg);
243 }
244
245 }, value);
246 }

参照元 Write().

◆ WriteText()

void Kisaragi_Lib::Json::WriteText ( std::ofstream & _file,
unsigned int _depth = 0 )
inlineprivate
249 {
250 if (!_file) {
251 Debug::PrintAssertStatic("エラー: 有効なファイルではありません");
252 return;
253 }
254
255 _file << "\"" + std::get<std::string>(value) + "\"";
256
257 return;
258 }

参照元 Write().

◆ WriteObject()

void Kisaragi_Lib::Json::WriteObject ( std::ofstream & _file,
unsigned int _depth = 0 )
inlineprivate
261 {
262 if (!_file)
263 {
264 Debug::PrintAssertStatic("エラー: 有効なファイルではありません");
265 return;
266 }
267
268
269 _depth++;
270
271 _file << "{\n";
272
273 bool isFirst = true;
274
275
276 for (auto& element : std::get<std::unordered_map<std::string, Json>>(value))
277 {
278 if (isFirst)
279 {
280 isFirst = false;
281 }
282 else
283 {
284 _file << ",\n";
285 }
286
287 //改行を出力
288 WriteJsonDepth(_file, _depth);
289 //keyを出力
290 _file << "\"" + element.first + "\"" + " : ";
291 //値を出力
292 element.second.Write(_file, _depth);
293
294 }
295
296 _file << "\n";
297 //改行を出力
298 WriteJsonDepth(_file, _depth - 1);
299 _file << "}";
300 }
void WriteJsonDepth(std::ofstream &_file, unsigned int _depth=0)
Definition Json.hpp:334

参照元 Write().

◆ WriteArray()

void Kisaragi_Lib::Json::WriteArray ( std::ofstream & _file,
unsigned int _depth = 0 )
inlineprivate
303 {
304 _depth++;
305
306 _file << "[\n";
307
308 bool isFirst = true;
309
310 for (auto& element : std::get<std::vector<Json>>(value))
311 {
312 if (isFirst)
313 {
314 isFirst = false;
315 }
316 else
317 {
318 _file << ",\n";
319 }
320
321 //改行を出力
322 WriteJsonDepth(_file, _depth);
323 //値を出力
324 element.Write(_file, _depth);
325
326 }
327
328 _file << "\n";
329 //改行を出力
330 WriteJsonDepth(_file, _depth - 1);
331 _file << "]";
332 }

参照元 Write().

◆ WriteJsonDepth()

void Kisaragi_Lib::Json::WriteJsonDepth ( std::ofstream & _file,
unsigned int _depth = 0 )
inlineprivate
335 {
336 if (!_file)
337 {
338 Debug::PrintAssertStatic("エラー: 有効なファイルではありません");
339 return;
340 }
341
342 _file << std::string(_depth, '\t');
343 }

参照元 WriteArray(), WriteObject().

メンバ詳解

◆ TEXT_INDEX

const int Kisaragi_Lib::Json::TEXT_INDEX = 4
private

参照元 Write().

◆ value


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