JsoNeat
Some JSON parse/iterate C++ classes build on top of Jsmn
Loading...
Searching...
No Matches
from_json_jsmn.hh
1/**
2 * \brief de-serialize objects from JSON using JSMN library
3 */
4
5#pragma once
6
7#include "jsoneat.hh"
8
9namespace jsoneat::from_json::jsmn {
10
11/**
12 * \brief overloaded function template for de-serializing from json-string into char-array
13 * \tparam ITER JSMN iterator type
14 * \tparam SIZE char array size
15 * \param it JSMN iterator object reference
16 * \param dst data-member reference
17 * \param key json key to match against key name in iterator. or nullptr, for any key (?)
18 * \return true if key has matched and
19 */
20template<class ITER, size_t SIZE>
21bool take(ITER &it, char (&dst)[SIZE], const char *key) {
22 return it.takeValue(dst, key);
23}
24
25/**
26 * \brief overloaded function template for de-serializing integral types
27 * \tparam ITER JSMN iterator type
28 * \tparam SIZE char array size
29 * \param it JSMN iterator object reference
30 * \param dst data-member reference
31 * \param key json key to match against key name in iterator. or nullptr, for any key (?)
32 * \return true if key has matched and
33 */
34template<class ITER, typename INT, typename std::enable_if<std::is_integral<std::decay_t<INT>>::value>::type* = nullptr>
35bool take(ITER &it, INT &dst, const char *key) {
36 return it.takeValue(dst, key);
37}
38
39/**
40 * \brief overloaded function template for de-serializing floating point types
41 * \tparam ITER JSMN iterator type
42 * \tparam SIZE char array size
43 * \param it JSMN iterator object reference
44 * \param dst data-member reference
45 * \param key json key to match against key name in iterator. or nullptr, for any key (?)
46 * \return true if key has matched and
47 */
48template<class ITER, typename INT, typename std::enable_if<std::is_floating_point<std::decay_t<INT>>::value>::type* = nullptr>
49bool take(ITER &it, INT &dst, const char *key) {
50 return it.takeValue(dst, key);
51}
52
53/**
54 * \brief overloaded function template for de-serializing object types
55 * \tparam ITER JSMN iterator type
56 * \tparam SIZE char array size
57 * \param it JSMN iterator object reference
58 * \param dst data-member reference
59 * \param key json key to match against key name in iterator. or nullptr, for any key (?)
60 * \return true if key has matched and
61 */
62template<class ITER, class C, typename std::enable_if<std::is_class<std::decay_t<C>>::value>::type* = nullptr>
63bool take(ITER &it, C &dst, const char *key) {
64 return it.takeObject(dst, key);
65}
66
67/**
68 * \brief overloaded function template for de-serializing an array integral type values
69 * \tparam ITER JSMN iterator type
70 * \tparam INT type of array members
71 * \tparam SIZE array length
72 * \param it JSMN iterator object reference
73 * \param dst data-member reference
74 * \param key json key to match against key name in iterator. or nullptr, for any key (?)
75 * \return true if key has matched and
76 */
77template<class ITER, typename INT, size_t SIZE, typename std::enable_if<std::is_integral<std::decay_t<INT>>::value>::type* = nullptr>
78bool take(ITER &it, INT (&dst)[SIZE], const char *key) {
79 return it.takeValueArray(dst, key);
80}
81
82/**
83 * \brief overloaded function template for de-serializing an array floating point type values
84 * \tparam ITER JSMN iterator type
85 * \tparam INT type of array members
86 * \tparam SIZE array length
87 * \param it JSMN iterator object reference
88 * \param dst data-member reference
89 * \param key json key to match against key name in iterator. or nullptr, for any key (?)
90 * \return true if key has matched and
91 */
92template<class ITER, typename INT, size_t SIZE, typename std::enable_if<std::is_floating_point<std::decay_t<INT>>::value>::type* = nullptr>
93bool take(ITER &it, INT (&dst)[SIZE], const char *key) {
94 return it.takeValueArray(dst, key);
95}
96
97/**
98 * \brief overloaded function template for de-serializing an array of struct type objects
99 * \tparam ITER JSMN iterator type
100 * \tparam INT type of array members
101 * \tparam SIZE array length
102 * \param it JSMN iterator object reference
103 * \param dst data-member reference
104 * \param key json key to match against key name in iterator. or nullptr, for any key (?)
105 * \return true if key has matched and
106 */
107template<class ITER, class C, size_t SIZE, typename std::enable_if<std::is_class<std::decay_t<C>>::value>::type* = nullptr>
108bool take(ITER &it, C (&dst)[SIZE], const char *key) {
109 return it.takeObjectArray(dst, key);
110}
111
112template<class ITER, class C, size_t SIZE, typename std::enable_if<std::is_class<std::decay_t<C>>::value>::type* = nullptr>
113bool take(ITER &it, std::array<C, SIZE> &dst, const char *key) {
114 return it.takeObjectArray(dst, key);
115}
116
117/**
118 * \brief function template used as terminator in variadic template
119 * \tparam ITER type of iterator class embedded in JsoNeat
120 * \param it unused
121 * \return returns always false
122 */
123template<class ITER>
124bool take_one_of(ITER &it) {
125 return false;
126}
127
128/**
129 * \brief variadic function template for de-serializing data of current JSMN iterator into the matching pair from our args-list
130 * \tparam ITER type of iterator class embedded in JsoNeat
131 * \tparam T type of struct (pair) containing a reference to a data-member and the json-key name used for this member
132 * \tparam Args more types of these pairs like T but, which are only different by the type of the data-member referenced
133 * \param it iterator
134 * \param pair the first data-member-reference / json-key pair object
135 * \param args rest of the pair objects
136 * \return true if all key in JSMN object matched a data-member-reference in the pair objects
137 */
138template<class ITER, typename T, typename ... Args>
139bool take_one_of(ITER &it, T pair, Args ... args) {
140 if (jsoneat::from_json::jsmn::take(it, pair.val, pair.key))
141 return true;
142 return take_one_of(it, args...);
143}
144
145/**
146 * \brief variadic function template for de-serializing all members of an object in one call
147 * \tparam ITER type of iterator class embedded in JsoNeat
148 * \tparam T type of struct (pair) containing a reference to a data-member and the json-key name used for this member
149 * \tparam Args more types of these pairs like T but, which are only different by the type of the data-member referenced
150 * \param it iterator
151 * \param pair the first data-member-reference / json-key pair object
152 * \param args rest of the pair objects
153 * \return true if all key in JSMN object matched a data-member-reference in the pair objects
154 */
155template<class ITER, typename T, typename ... Args>
156bool deserialize_object(ITER &it, T pair, Args ... args) {
157 assert(it->type == JSMN_OBJECT);
158
159 auto count = it->size;
160 for (++it; count > 0 && it; --count) {
161 if (!take_one_of(it, pair, args...))
162 return false; // fail for unknown keys
163 }
164 return true;
165}
166
167}
bool take_one_of(ITER &it)
function template used as terminator in variadic template
Definition from_json_jsmn.hh:124
bool take(ITER &it, char(&dst)[SIZE], const char *key)
overloaded function template for de-serializing from json-string into char-array
Definition from_json_jsmn.hh:21
bool take(ITER &it, INT(&dst)[SIZE], const char *key)
overloaded function template for de-serializing an array integral type values
Definition from_json_jsmn.hh:78
bool take(ITER &it, INT &dst, const char *key)
overloaded function template for de-serializing integral types
Definition from_json_jsmn.hh:35
bool deserialize_object(ITER &it, T pair, Args ... args)
variadic function template for de-serializing all members of an object in one call
Definition from_json_jsmn.hh:156
bool take_one_of(ITER &it, T pair, Args ... args)
variadic function template for de-serializing data of current JSMN iterator into the matching pair fr...
Definition from_json_jsmn.hh:139