emlabcpp
modern opinionated embedded C++ library
json.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "./point.h"
27 #include "./pose.h"
28 #include "./quaternion.h"
29 #include "./simplex.h"
30 
31 #ifdef EMLABCPP_USE_NLOHMANN_JSON
32 #include <nlohmann/json.hpp>
33 #endif
34 
35 namespace emlabcpp
36 {
37 #ifdef EMLABCPP_USE_NLOHMANN_JSON
38 
39 template < std::size_t N >
40 inline void from_json( nlohmann::json const& j, point< N >& p )
41 {
42  using container = typename point< N >::container;
43 
44  p = point< N >( j.get< container >() );
45 }
46 
47 template < std::size_t N >
48 inline void to_json( nlohmann::json& j, point< N > const& p )
49 {
50  j = view{ p };
51 }
52 
53 inline void to_json( nlohmann::json& j, quaternion const& quat )
54 {
55  j = { quat[0], quat[1], quat[2], quat[3] };
56 }
57 
58 inline void from_json( nlohmann::json const& j, quaternion& quat )
59 {
60  quat = quaternion{
61  j.at( 0 ).get< float >(),
62  j.at( 1 ).get< float >(),
63  j.at( 2 ).get< float >(),
64  j.at( 3 ).get< float >() };
65 }
66 
67 inline void to_json( nlohmann::json& j, pose const& p )
68 {
69  j["position"] = p.position;
70  j["orientation"] = p.orientation;
71 }
72 
73 inline void from_json( nlohmann::json const& j, pose& p )
74 {
75  p = pose{
76  j.at( "position" ).get< point< 3 > >(), j.at( "orientation" ).get< quaternion >() };
77 }
78 
79 template < typename Item, std::size_t N >
80 inline void to_json( nlohmann::json& j, simplex< Item, N > const& sim )
81 {
82  std::copy( sim.begin(), sim.end(), std::back_inserter( j ) );
83 }
84 
85 template < typename Item, std::size_t N >
86 inline void from_json( nlohmann::json const& j, simplex< Item, N >& sim )
87 {
88  sim = simplex< Item, N >{ j.get< std::array< Item, N + 1 > >() };
89 }
90 
91 #endif
92 
93 } // namespace emlabcpp
std::array< float, N > container
Definition: vec_point_base.h:37
MIT License.
Definition: impl.h:31
view(Container &cont) -> view< iterator_of_t< Container > >
The container deduction guide uses iterator_of_t.
void copy(Container &&cont, Iterator iter)
Definition: algorithm.h:455
concept container
Definition: concepts.h:93