1+ /*
2+ Copyright (c) 2022, The Cinder Project
3+ All rights reserved.
4+
5+ This code is designed for use with the Cinder C++ library, http://libcinder.org
6+
7+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that
8+ the following conditions are met:
9+
10+ * Redistributions of source code must retain the above copyright notice, this list of conditions and
11+ the following disclaimer.
12+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
13+ the following disclaimer in the documentation and/or other materials provided with the distribution.
14+
15+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
16+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17+ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19+ TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22+ POSSIBILITY OF SUCH DAMAGE.
23+ */
24+
25+ #pragma once
26+
27+ #include " cinder/Cinder.h"
28+ #include " cinder/Filesystem.h"
29+ #include " cinder/Rect.h"
30+ #include " cinder/Color.h"
31+ #include " cinder/Matrix.h"
32+ #include " cinder/DataSource.h"
33+ #include " glm/gtc/type_ptr.hpp"
34+ #include < nlohmann/json.hpp>
35+
36+ namespace cinder {
37+ using Json = nlohmann::json;
38+ }
39+
40+ namespace nlohmann {
41+
42+ // ! Serializer for tvec2
43+ template <typename T, glm::precision P>
44+ struct adl_serializer <glm::tvec2<T,P>> {
45+ static void to_json ( json& j, const glm::tvec2<T,P> &v ) { const T* ptr = glm::value_ptr ( v ); j = std::vector<T>( ptr, ptr + 2 ); }
46+ static void from_json ( const json &j, glm::tvec2<T,P> &v ) { std::vector<T> vec = j; v = glm::make_vec2 ( vec.data () ); }
47+ };
48+
49+ // ! Serializer for tvec3
50+ template <typename T, glm::precision P>
51+ struct adl_serializer <glm::tvec3<T,P>> {
52+ static void to_json ( json& j, const glm::tvec3<T,P> &v ) { const T* ptr = glm::value_ptr ( v ); j = std::vector<T>( ptr, ptr + 3 ); }
53+ static void from_json ( const json &j, glm::tvec3<T,P> &v ) { std::vector<T> vec = j; v = glm::make_vec3 ( vec.data () ); }
54+ };
55+
56+ // ! Serializer for tvec4
57+ template <typename T, glm::precision P>
58+ struct adl_serializer <glm::tvec4<T,P>> {
59+ static void to_json ( json& j, const glm::tvec4<T,P> &v ) { const T* ptr = glm::value_ptr ( v ); j = std::vector<T>( ptr, ptr + 4 ); }
60+ static void from_json ( const json &j, glm::tvec4<T,P> &v ) { std::vector<T> vec = j; v = glm::make_vec4 ( vec.data () ); }
61+ };
62+
63+ // ! Serializer for tquat
64+ template <typename T, glm::precision P>
65+ struct adl_serializer <glm::tquat<T,P>> {
66+ static void to_json ( json& j, const glm::tquat<T,P> &v ) { const T* ptr = glm::value_ptr ( v ); j = std::vector<T>( ptr, ptr + 4 ); }
67+ static void from_json ( const json &j, glm::tquat<T,P> &v ) { std::vector<T> vec = j; v = glm::make_quat ( vec.data () ); }
68+ };
69+
70+ // ! Serializer for tmat3x3
71+ template <typename T, glm::precision P>
72+ struct adl_serializer <glm::tmat3x3<T,P>> {
73+ static void to_json ( json& j, const glm::tmat3x3<T,P> &m ) { j = std::vector<T>( &m[0 ][0 ], &m[0 ][0 ] + 3 * 3 ); }
74+ static void from_json ( const json &j, glm::tmat3x3<T,P> &m ) { std::vector<T> vec = j; m = glm::make_mat3x3 ( vec.data () ); }
75+ };
76+
77+ // ! Serializer for tmat4x4
78+ template <typename T, glm::precision P>
79+ struct adl_serializer <glm::tmat4x4<T,P>> {
80+ static void to_json ( json& j, const glm::tmat4x4<T,P> &m ) { j = std::vector<T>( &m[0 ][0 ], &m[0 ][0 ] + 4 * 4 ); }
81+ static void from_json ( const json &j, glm::tmat4x4<T,P> &m ) { std::vector<T> vec = j; m = glm::make_mat4x4 ( vec.data () ); }
82+ };
83+
84+ // ! Serializer for RectT
85+ template <typename T>
86+ struct adl_serializer <ci::RectT<T>> {
87+ static void to_json ( json& j, const ci::RectT<T> &r ) { j = glm::tvec4<T>( r.getUpperLeft (), r.getLowerRight () ); }
88+ static void from_json ( const json &j, ci::RectT<T> &r ) { glm::tvec4<T> v = j; r = ci::RectT<T>( v.x , v.y , v.z , v.w ); }
89+ };
90+
91+ // ! Serializer for Area
92+ template <>
93+ struct adl_serializer <ci::Area> {
94+ static void to_json ( json& j, const ci::Area &a ) { j = glm::ivec4 ( a.getUL (), a.getLR () ); }
95+ static void from_json ( const json &j, ci::Area &a ) { glm::ivec4 v = j; a = ci::Area ( v.x , v.y , v.z , v.w ); }
96+ };
97+
98+ template <typename T>
99+ struct adl_serializer <ci::ColorT<T>> {
100+ static void to_json ( json& j, const ci::ColorT<T>& c ) { j = { c.r , c.g , c.b , }; }
101+ static void from_json ( const json& j, ci::ColorT<T>& c ) { std::vector<T> vec = j; c = ci::ColorT<T>( vec[0 ], vec[1 ], vec[2 ] ); }
102+ };
103+
104+ template <typename T>
105+ struct adl_serializer <ci::ColorAT<T>> {
106+ static void to_json ( json& j, const ci::ColorAT<T>& c ) { j = { c.r , c.g , c.b , c.a }; }
107+ static void from_json ( const json& j, ci::ColorAT<T>& c ) { std::vector<T> vec = j; c = ci::ColorAT<T>( vec[0 ], vec[1 ], vec[2 ], vec[3 ] ); }
108+ };
109+
110+ }
111+
112+ namespace cinder {
113+
114+ Json loadJson ( const cinder::fs::path &path, bool stripComments = false );
115+ Json loadJson ( const DataSourceRef &dataSource, bool stripComments = false );
116+
117+ } // namespace cinder
0 commit comments