Pioneer
JsonUtils.h
Go to the documentation of this file.
1 // Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _JSON_UTILS_H
5 #define _JSON_UTILS_H
6 
7 #include "Color.h"
8 #include "FrameId.h"
9 #include "Json.h"
10 #include "Quaternion.h"
11 #include "RefCounted.h"
12 #include "fixed.h"
13 #include "matrix3x3.h"
14 #include "matrix4x4.h"
15 #include "vector3.h"
16 
17 namespace FileSystem {
18  class FileSource;
19  class FileData;
20 } // namespace FileSystem
21 
22 namespace JsonUtils {
23  // Low-level load JSON from a file descriptor.
25  // Load a JSON file from a path and a file source.
26  Json LoadJsonFile(const std::string &filename, FileSystem::FileSource &source);
27  // Load a JSON file from the game's data sources, optionally applying all
28  // files with the the name <filename>.patch as Json Merge Patch (RFC 7386) files
29  Json LoadJsonDataFile(const std::string &filename, bool with_merge = true);
30  // Loads an optionally-gzipped, optionally-CBOR encoded JSON file from the specified source.
31  Json LoadJsonSaveFile(const std::string &filename, FileSystem::FileSource &source);
32 } // namespace JsonUtils
33 
34 // To-JSON functions. These are called explicitly, and are passed a reference to the object to fill.
35 void VectorToJson(Json &jsonObj, const vector3f &vec);
36 void VectorToJson(Json &jsonObj, const vector3d &vec);
37 void QuaternionToJson(Json &jsonObj, const Quaternionf &quat);
38 void QuaternionToJson(Json &jsonObj, const Quaterniond &quat);
39 void MatrixToJson(Json &jsonObj, const matrix3x3f &mat);
40 void MatrixToJson(Json &jsonObj, const matrix3x3d &mat);
41 void MatrixToJson(Json &jsonObj, const matrix4x4f &mat);
42 void MatrixToJson(Json &jsonObj, const matrix4x4d &mat);
43 void ColorToJson(Json &jsonObj, const Color3ub &col);
44 void ColorToJson(Json &jsonObj, const Color4ub &col);
45 void BinStrToJson(Json &jsonObj, const std::string &str);
46 
47 // Drivers for automatic serialization of custom types. These are implicitly called by assigning to a Json object.
48 template <typename T>
49 void to_json(Json &obj, const vector3<T> &vec) { VectorToJson(obj, vec); }
50 template <typename T>
51 void to_json(Json &obj, const Quaternion<T> &vec) { QuaternionToJson(obj, vec); }
52 template <typename T>
53 void to_json(Json &obj, const matrix3x3<T> &mat) { MatrixToJson(obj, mat); }
54 template <typename T>
55 void to_json(Json &obj, const matrix4x4<T> &mat) { MatrixToJson(obj, mat); }
56 inline void to_json(Json &obj, const Color3ub &col) { ColorToJson(obj, col); }
57 inline void to_json(Json &obj, const Color4ub &col) { ColorToJson(obj, col); }
58 inline void to_json(Json &obj, const FrameId &t) { obj = t.id(); }
59 
60 void from_json(const Json &obj, fixed &n);
61 void to_json(Json &obj, const fixed &n);
62 
63 // Parse JSON functions. These functions will throw Json::type_error if passed an invalid type.
64 void JsonToVector(vector3f *vec, const Json &jsonObj);
65 void JsonToVector(vector3d *vec, const Json &jsonObj);
66 void JsonToQuaternion(Quaternionf *pQuat, const Json &jsonObj);
67 void JsonToQuaternion(Quaterniond *pQuat, const Json &jsonObj);
68 void JsonToMatrix(matrix3x3f *pMat, const Json &jsonObj);
69 void JsonToMatrix(matrix3x3d *pMat, const Json &jsonObj);
70 void JsonToMatrix(matrix4x4f *pMat, const Json &jsonObj);
71 void JsonToMatrix(matrix4x4d *pMat, const Json &jsonObj);
72 void JsonToColor(Color3ub *pCol, const Json &jsonObj);
73 void JsonToColor(Color4ub *pCol, const Json &jsonObj);
74 std::string JsonToBinStr(const Json &jsonObj);
75 
76 template <typename T>
77 void from_json(const Json &obj, vector3<T> &vec) { JsonToVector(&vec, obj); }
78 template <typename T>
79 void from_json(const Json &obj, Quaternion<T> &vec) { JsonToQuaternion(&vec, obj); }
80 template <typename T>
81 void from_json(const Json &obj, matrix3x3<T> &vec) { JsonToMatrix(&vec, obj); }
82 template <typename T>
83 void from_json(const Json &obj, matrix4x4<T> &vec) { JsonToMatrix(&vec, obj); }
84 inline void from_json(const Json &obj, Color3ub &col) { JsonToColor(&col, obj); }
85 inline void from_json(const Json &obj, Color4ub &col) { JsonToColor(&col, obj); }
86 inline void from_json(const Json &obj, FrameId &id) { id = (int)obj; }
87 
88 #endif /* _JSON_UTILS_H */
void JsonToMatrix(matrix3x3f *pMat, const Json &jsonObj)
Definition: JsonUtils.cpp:353
void JsonToVector(vector3f *vec, const Json &jsonObj)
Definition: JsonUtils.cpp:291
void MatrixToJson(Json &jsonObj, const matrix3x3f &mat)
Definition: JsonUtils.cpp:160
std::string JsonToBinStr(const Json &jsonObj)
Definition: JsonUtils.cpp:478
void QuaternionToJson(Json &jsonObj, const Quaternionf &quat)
Definition: JsonUtils.cpp:142
void ColorToJson(Json &jsonObj, const Color3ub &col)
Definition: JsonUtils.cpp:250
void to_json(Json &obj, const vector3< T > &vec)
Definition: JsonUtils.h:49
void JsonToColor(Color3ub *pCol, const Json &jsonObj)
Definition: JsonUtils.cpp:459
void VectorToJson(Json &jsonObj, const vector3f &vec)
Definition: JsonUtils.cpp:111
void BinStrToJson(Json &jsonObj, const std::string &str)
Definition: JsonUtils.cpp:269
void from_json(const Json &obj, fixed &n)
Definition: JsonUtils.cpp:504
void JsonToQuaternion(Quaternionf *pQuat, const Json &jsonObj)
Definition: JsonUtils.cpp:325
nlohmann::json Json
Definition: Json.h:8
Definition: FileSystem.h:196
Definition: IniConfig.h:10
Definition: JsonUtils.cpp:43
Json LoadJsonDataFile(const std::string &filename, bool with_merge)
Definition: JsonUtils.cpp:65
Json LoadJsonSaveFile(const std::string &filename, FileSystem::FileSource &source)
Definition: JsonUtils.cpp:78
Json LoadJsonFile(const std::string &filename, FileSystem::FileSource &source)
Definition: JsonUtils.cpp:60
Json LoadJson(RefCountedPtr< FileSystem::FileData > fd)
Definition: JsonUtils.cpp:44
Definition: Color.h:152
Definition: Color.h:66
Definition: FrameId.h:9
constexpr size_t id() const
Definition: FrameId.h:23