GA::kit v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
json_types.h
Go to the documentation of this file.
1#pragma once
2#include "ga/json.h"
3
4// ---------------- json conversions ----------------
5
6namespace ga {
7
8// ga::vec4
9inline void from_json( const ga::Json& j, ga::vec4& v )
10{
11 try {
12 v = { j.at( 0 ), j.at( 1 ), j.at( 2 ), j.at( 3 ) };
13 } catch ( std::exception& e ) {
14 std::stringstream ss;
15 ss << "Error, vec4 from Json: " << j.dump() << " - " << e.what();
16 throw std::domain_error( ss.str() );
17 }
18}
19inline void to_json( ga::Json& j, const ga::vec4& v )
20{
21 j = { v[0], v[1], v[2], v[3] };
22}
23
24// ga::quat
25inline void from_json( const ga::Json& j, ga::quat& q )
26{
27 try {
28 q.x = j.at( 0 );
29 q.y = j.at( 1 );
30 q.z = j.at( 2 );
31 q.w = j.at( 3 );
32 } catch ( std::exception& e ) {
33 std::stringstream ss;
34 ss << "Error, quat from Json: " << j.dump() << " - " << e.what();
35 throw std::domain_error( ss.str() );
36 }
37}
38inline void to_json( ga::Json& j, const ga::quat& q )
39{
40 j = { q.x, q.y, q.z, q.w };
41}
42
43// ga::vec3
44inline void from_json( const ga::Json& j, ga::vec3& v )
45{
46 try {
47 v = { j.at( 0 ), j.at( 1 ), j.at( 2 ) };
48 } catch ( std::exception& e ) {
49 std::stringstream ss;
50 ss << "Error, vec3 from Json: " << j.dump() << " - " << e.what();
51 throw std::domain_error( ss.str() );
52 }
53}
54inline void to_json( ga::Json& j, const ga::vec3& v )
55{
56 j = { v[0], v[1], v[2] };
57}
58
59// ga::vec2
60inline void from_json( const ga::Json& j, ga::vec2& v )
61{
62 try {
63 v = { j.at( 0 ), j.at( 1 ) };
64 } catch ( std::exception& e ) {
65 std::stringstream ss;
66 ss << "Error, vec2 from Json: " << j.dump() << " - " << e.what();
67 throw std::domain_error( ss.str() );
68 }
69}
70inline void to_json( ga::Json& j, const ga::vec2& v )
71{
72 j = { v[0], v[1] };
73}
74
75// ga::mat4
76inline void from_json( const ga::Json& j, ga::mat4& m )
77{
78 ga::mat4 _m = m;
79 try {
80 // glm is column major - convert from row major
81 for ( int r = 0; r < 4; ++r ) {
82 for ( int c = 0; c < 4; ++c ) {
83 _m[c][r] = j.at(r).at(c);
84 }
85 }
86 } catch ( std::exception& e ) {
87 std::stringstream ss;
88 ss << "Error, mat4 from Json: " << j.dump() << " - " << e.what();
89 throw std::domain_error( ss.str() );
90 }
91 m = _m;
92}
93
94inline void to_json( ga::Json& j, const ga::mat4& m )
95{
96 j.clear();
97 // store row major - convert from column major
98 for ( int r = 0; r < 4; ++r ) {
99 ga::Json row;
100 for ( int c = 0; c < 4; ++c ) {
101 row.push_back( m[c][r] );
102 }
103 j.push_back( row );
104 }
105}
106
107// ga::Transform
108//inline void from_json(const ga::Json& j, ga::Transform& t)
109//{
110// ga::Transform _t = t;
111// try {
112// _t.setTranslation(j.at("translation").get<ga::vec3>());
113// _t.setRotation(j.at("rotation").get<ga::quat>());
114// _t.setScale(j.at("scale").get<ga::vec3>());
115// } catch (std::exception& e) {
116// std::stringstream ss;
117// ss << "Error, ga::Transform from Json: " << j.dump() << " - " << e.what();
118// throw std::domain_error(ss.str());
119// }
120// t = _t;
121//}
122//
123//inline void to_json(ga::Json& j, const ga::Transform& t)
124//{
125// j.clear();
126// j["translation"] = t.getTranslation();
127// j["rotation"] = t.getRotation();
128// j["scale"] = t.getScale();
129//}
130} // namespace ga
131
132// todo: overload glm namespace?
Definition: color.h:9
nlohmann::json Json
Definition: json.h:5
void to_json(ga::Json &j, const ga::vec4 &v)
Definition: json_types.h:19
void from_json(const ga::Json &j, ga::vec4 &v)
Definition: json_types.h:9