| 1 |
### jsoncons::reflect::json_conv_traits |
| 2 |
|
| 3 |
```cpp |
| 4 |
#include <jsoncons/reflect/json_conv_traits.hpp> |
| 5 |
``` |
| 6 |
|
| 7 |
<br> |
| 8 |
|
| 9 |
`json_conv_traits` defines a compile time template based interface for conversion between a `basic_json` value |
| 10 |
and a value of some other type. `json_conv_traits` implementations must specialize a traits class for a type `T`: |
| 11 |
|
| 12 |
```cpp |
| 13 |
template <typename Json,typename T,typename Enable=void> |
| 14 |
struct json_conv_traits |
| 15 |
{ |
| 16 |
static constexpr bool is(const Json& j) noexcept; |
| 17 |
|
| 18 |
template<typename Alloc,typename TempAlloc> |
| 19 |
static conversion_result<T> try_as(const allocator_set<Alloc,TempAlloc>&, const Json& j); |
| 20 |
|
| 21 |
template <typename Alloc, typename TempAlloc> |
| 22 |
static Json to_json(const allocator_set<Alloc,TempAlloc>& aset, const T& val); |
| 23 |
}; |
| 24 |
``` |
| 25 |
|
| 26 |
The function |
| 27 |
|
| 28 |
json_conv_traits<Json,T>::is(const Json& j) noexcept |
| 29 |
|
| 30 |
indictates whether `j` satisfies the requirements of type `T`. This function supports |
| 31 |
the type selection strategy when converting a `Json` value to the proper derived class |
| 32 |
in the polymorphic case, and when converting a `Json` value to the proper alternative |
| 33 |
type in the variant case. |
| 34 |
|
| 35 |
The function |
| 36 |
|
| 37 |
template <typename Alloc, typename TempAlloc> |
| 38 |
conversion_result<T> try_as(const allocator_set<Alloc,TempAlloc>& aset, const Json& j) |
| 39 |
|
| 40 |
tries to convert `j` to a value of type `T` and returns an `std::expected`-like result (like `std::expected<T,jsoncons::conversion_error>`.) |
| 41 |
|
| 42 |
The function |
| 43 |
|
| 44 |
template <typename Alloc, typename TempAlloc> |
| 45 |
Json to_json(const allocator_set<Alloc,TempAlloc>& aset, const T& val) |
| 46 |
|
| 47 |
tries to convert `val` into a `Json` value. |
| 48 |
|
| 49 |
jsoncons includes specializiations for most types in the standard library. And it includes convenience |
| 50 |
macros that make specializing `json_conv_traits` for your own types easier. |
| 51 |
|
| 52 |
### See also |
| 53 |
|
| 54 |
[allocator_set](../allocator_set.md) |
| 55 |
[conversion_result](../conversion_result.md) |
| 56 |
|
| 57 |
### Examples |
| 58 |
|
| 59 |
#### Optional values |
| 60 |
|
| 61 |
```cpp |
| 62 |
#include <jsoncons/json.hpp> |
| 63 |
#include <string> |
| 64 |
#include <optional> |
| 65 |
#include <iostream> |
| 66 |
#include <cassert> |
| 67 |
|
| 68 |
namespace ns { |
| 69 |
|
| 70 |
struct OptionalExample { |
| 71 |
|
| 72 |
std::string text; |
| 73 |
std::optional<std::string> optional; |
| 74 |
std::optional<std::string> optional_skip; |
| 75 |
|
| 76 |
explicit OptionalExample() = default; |
| 77 |
}; |
| 78 |
|
| 79 |
} // namespace ns |
| 80 |
|
| 81 |
namespace jsoncons { |
| 82 |
namespace reflect { |
| 83 |
|
| 84 |
template <typename Json> |
| 85 |
struct json_conv_traits<Json, ns::OptionalExample> |
| 86 |
{ |
| 87 |
using value_type = ns::OptionalExample; |
| 88 |
|
| 89 |
static bool is(const Json& j) noexcept |
| 90 |
{ |
| 91 |
return j.is_object() && j.contains("text") && j.contains("optional"); |
| 92 |
} |
| 93 |
template <typename Alloc, typename TempAlloc> |
| 94 |
static conversion_result<value_type> try_as(const allocator_set<Alloc,TempAlloc>&, const Json& j) |
| 95 |
{ |
| 96 |
using result_type = conversion_result<value_type>; |
| 97 |
|
| 98 |
if (!j.is_object()) |
| 99 |
{ |
| 100 |
return result_type(jsoncons::unexpect, conv_errc::not_map, "ns::OptionalExample"); |
| 101 |
} |
| 102 |
|
| 103 |
value_type val; |
| 104 |
{ |
| 105 |
auto it = j.find("text"); |
| 106 |
if (it == j.object_range().end()) |
| 107 |
{ |
| 108 |
return result_type(jsoncons::unexpect, conv_errc::missing_required_member, "text"); |
| 109 |
} |
| 110 |
auto r = it->value().template try_as<std::string>(); |
| 111 |
if (!r) |
| 112 |
{ |
| 113 |
return result_type(unexpect, r.error().code()); |
| 114 |
} |
| 115 |
val.text = std::move(*r); |
| 116 |
} |
| 117 |
{ |
| 118 |
auto it = j.find("optional"); |
| 119 |
if (it != j.object_range().end()) |
| 120 |
{ |
| 121 |
auto r = it->value().template try_as<std::string>(); |
| 122 |
if (!r) |
| 123 |
{ |
| 124 |
return result_type(unexpect, r.error().code()); |
| 125 |
} |
| 126 |
val.optional = std::move(*r); |
| 127 |
} |
| 128 |
} |
| 129 |
{ |
| 130 |
auto it = j.find("optional_skip"); |
| 131 |
if (it != j.object_range().end()) |
| 132 |
{ |
| 133 |
auto r = it->value().template try_as<std::string>(); |
| 134 |
if (!r) |
| 135 |
{ |
| 136 |
return result_type(unexpect, r.error().code()); |
| 137 |
} |
| 138 |
val.optional_skip = std::move(*r); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return result_type(std::move(val)); |
| 143 |
} |
| 144 |
template <typename Alloc, typename TempAlloc> |
| 145 |
static Json to_json(const allocator_set<Alloc,TempAlloc>& aset, const ns::OptionalExample& val) |
| 146 |
{ |
| 147 |
Json j = jsoncons::make_obj_using_allocator<Json>(aset.get_allocator(), json_object_arg); |
| 148 |
j.try_emplace("text", val.text); |
| 149 |
if (val.optional) |
| 150 |
{ |
| 151 |
j.try_emplace("optional", *val.optional); |
| 152 |
} |
| 153 |
if (val.optional_skip) |
| 154 |
{ |
| 155 |
j.try_emplace("optional_skip", *val.optional_skip); |
| 156 |
} |
| 157 |
return j; |
| 158 |
} |
| 159 |
}; |
| 160 |
} // namespace reflect |
| 161 |
} // namespace jsoncons |
| 162 |
|
| 163 |
int main() |
| 164 |
{ |
| 165 |
std::error_code ec; |
| 166 |
|
| 167 |
ns::OptionalExample val; |
| 168 |
val.text = "Sample!"; |
| 169 |
val.optional = "Available Value"; |
| 170 |
|
| 171 |
std::string buffer; |
| 172 |
auto wresult = jsoncons::try_encode_json_pretty(val, buffer); |
| 173 |
if (!wresult) |
| 174 |
{ |
| 175 |
std::cout << wresult.error().message() << "\n"; |
| 176 |
exit(1); |
| 177 |
} |
| 178 |
std::cout << buffer << "\n"; |
| 179 |
} |
| 180 |
``` |
| 181 |
|
| 182 |
Output: |
| 183 |
|
| 184 |
```json |
| 185 |
{ |
| 186 |
"text": "Sample!", |
| 187 |
"optional": "Available Value" |
| 188 |
} |
| 189 |
``` |
| 190 |
|
| 191 |
#### Remarks |
| 192 |
|
| 193 |
- If you're using C++20 or higher, you can use [std::make_obj_using_allocator](https://en.cppreference.com/w/cpp/memory/make_obj_using_allocator.html) |
| 194 |
in `to_json` instead of `jsoncons::make_obj_using_allocator`. |
| 195 |
|
| 196 |
- To save typing and enhance readability, you can use the convenience macro `JSONCONS_N_MEMBER_TRAITS` to generate the traits classes, |
| 197 |
|
| 198 |
``` |
| 199 |
JSONCONS_N_MEMBER_TRAITS(OptionalExample, 1, text, optional, optional_skip); |
| 200 |
``` |
| 201 |
|
| 202 |
#### Uses allocator construction example |
| 203 |
|
| 204 |
```cpp |
| 205 |
#include <jsoncons/json.hpp> |
| 206 |
#include <../examples/src/common/mock_stateful_allocator.hpp> |
| 207 |
#include <scoped_allocator> |
| 208 |
#include <vector> |
| 209 |
#include <string> |
| 210 |
#include <iostream> |
| 211 |
#include <cassert> |
| 212 |
|
| 213 |
namespace ns { |
| 214 |
|
| 215 |
template <typename Alloc> |
| 216 |
struct book |
| 217 |
{ |
| 218 |
using allocator_type = Alloc; |
| 219 |
|
| 220 |
using char_allocator_type = typename std::allocator_traits<Alloc>:: template rebind_alloc<char>; |
| 221 |
using string_type = std::basic_string<char, std::char_traits<char>, char_allocator_type>; |
| 222 |
|
| 223 |
string_type author; |
| 224 |
string_type title; |
| 225 |
double price{0}; |
| 226 |
|
| 227 |
book(const Alloc& alloc) |
| 228 |
: author(alloc), title(alloc) |
| 229 |
{ |
| 230 |
} |
| 231 |
|
| 232 |
book(book&& other, const Alloc& alloc) |
| 233 |
: author(std::move(other.author), alloc), title(std::move(other.title), alloc) |
| 234 |
{ |
| 235 |
} |
| 236 |
}; |
| 237 |
|
| 238 |
} // namespace ns |
| 239 |
|
| 240 |
namespace jsoncons { |
| 241 |
namespace reflect { |
| 242 |
|
| 243 |
template <typename Json, typename Alloc> |
| 244 |
struct json_conv_traits<Json, ns::book<Alloc>> |
| 245 |
{ |
| 246 |
using value_type = ns::book<Alloc>; |
| 247 |
|
| 248 |
static bool is(const Json& j) noexcept |
| 249 |
{ |
| 250 |
return j.is_object() && j.contains("author") && |
| 251 |
j.contains("title") && j.contains("price"); |
| 252 |
} |
| 253 |
template <typename Alloc, typename TempAlloc> |
| 254 |
static conversion_result<value_type> try_as(const allocator_set<Alloc, TempAlloc>& aset, const Json& j) |
| 255 |
{ |
| 256 |
using result_type = conversion_result<value_type>; |
| 257 |
|
| 258 |
if (!j.is_object()) |
| 259 |
{ |
| 260 |
return result_type(jsoncons::unexpect, conv_errc::not_map, "ns::book"); |
| 261 |
} |
| 262 |
|
| 263 |
auto val = jsoncons::make_obj_using_allocator<value_type>(aset.get_allocator()); |
| 264 |
|
| 265 |
{ |
| 266 |
auto it = j.find("author"); |
| 267 |
if (it == j.object_range().end()) |
| 268 |
{ |
| 269 |
return result_type(jsoncons::unexpect, conv_errc::missing_required_member, "author"); |
| 270 |
} |
| 271 |
auto r = it->value().template try_as<typename value_type::string_type>(aset); |
| 272 |
if (!r) |
| 273 |
{ |
| 274 |
return result_type(unexpect, r.error().code()); |
| 275 |
} |
| 276 |
val.author = std::move(*r); |
| 277 |
} |
| 278 |
{ |
| 279 |
auto it = j.find("title"); |
| 280 |
if (it == j.object_range().end()) |
| 281 |
{ |
| 282 |
return result_type(jsoncons::unexpect, conv_errc::missing_required_member, "title"); |
| 283 |
} |
| 284 |
auto r = it->value().template try_as<typename value_type::string_type>(aset); |
| 285 |
if (!r) |
| 286 |
{ |
| 287 |
return result_type(jsoncons::unexpect, r.error().code()); |
| 288 |
} |
| 289 |
val.title = std::move(*r); |
| 290 |
} |
| 291 |
{ |
| 292 |
auto it = j.find("price"); |
| 293 |
if (it == j.object_range().end()) |
| 294 |
{ |
| 295 |
return result_type(jsoncons::unexpect, conv_errc::missing_required_member, "price"); |
| 296 |
} |
| 297 |
auto r = it->value().template try_as<double>(aset); |
| 298 |
if (!r) |
| 299 |
{ |
| 300 |
return result_type(jsoncons::unexpect, r.error().code(), "price"); |
| 301 |
} |
| 302 |
val.price = *r; |
| 303 |
} |
| 304 |
|
| 305 |
return result_type(std::move(val)); |
| 306 |
} |
| 307 |
|
| 308 |
template <typename Alloc, typename TempAlloc> |
| 309 |
static Json to_json(const allocator_set<Alloc, TempAlloc>& aset, const value_type& val) |
| 310 |
{ |
| 311 |
auto j = jsoncons::make_obj_using_allocator<Json>(aset.get_allocator(), json_object_arg); |
| 312 |
j.try_emplace("author", val.author); |
| 313 |
j.try_emplace("title", val.title); |
| 314 |
j.try_emplace("price", val.price); |
| 315 |
return j; |
| 316 |
} |
| 317 |
}; |
| 318 |
|
| 319 |
} // namespace reflect |
| 320 |
} // namespace jsoncons |
| 321 |
|
| 322 |
template <typename T> |
| 323 |
using cust_allocator = std::scoped_allocator_adaptor<mock_stateful_allocator<T>>; |
| 324 |
|
| 325 |
using book_type = ns::book<cust_allocator<char>>; |
| 326 |
using books_type = std::vector<book_type, cust_allocator<book_type>>; |
| 327 |
|
| 328 |
int main() |
| 329 |
{ |
| 330 |
const std::string input = R"( |
| 331 |
[ |
| 332 |
{ |
| 333 |
"author" : "Haruki Murakami", |
| 334 |
"title" : "Kafka on the Shore", |
| 335 |
"price" : 25.17 |
| 336 |
}, |
| 337 |
{ |
| 338 |
"author" : "Charles Bukowski", |
| 339 |
"title" : "Pulp", |
| 340 |
"price" : 22.48 |
| 341 |
} |
| 342 |
] |
| 343 |
)"; |
| 344 |
|
| 345 |
cust_allocator<book_type> alloc(1); |
| 346 |
auto aset = jsoncons::make_alloc_set(alloc); |
| 347 |
auto rin = jsoncons::try_decode_json<books_type>(aset, input); |
| 348 |
assert(rin); |
| 349 |
|
| 350 |
std::string output; |
| 351 |
auto rout = jsoncons::try_encode_json_pretty(aset, *rin, output); |
| 352 |
assert(rout); |
| 353 |
std::cout << output << "\n"; |
| 354 |
} |
| 355 |
``` |
| 356 |
Output: |
| 357 |
``` |
| 358 |
[ |
| 359 |
{ |
| 360 |
"author": "Haruki Murakami", |
| 361 |
"title": "Kafka on the Shore", |
| 362 |
"price": 0.0 |
| 363 |
}, |
| 364 |
{ |
| 365 |
"author": "Charles Bukowski", |
| 366 |
"title": "Pulp", |
| 367 |
"price": 0.0 |
| 368 |
} |
| 369 |
] |
| 370 |
``` |
| 371 |
|
| 372 |
#### Remarks |
| 373 |
|
| 374 |
- If you're using C++20 or higher, you can use [std::make_obj_using_allocator](https://en.cppreference.com/w/cpp/memory/make_obj_using_allocator.html) |
| 375 |
in `try_as` and `to_json` instead of `jsoncons::make_obj_using_allocator`. |
| 376 |
|
| 377 |
- To save typing and enhance readability, you can use the convenience macro `JSONCONS_TMPL_ALL_MEMBER_TRAITS` to generate the traits classes, |
| 378 |
|
| 379 |
``` |
| 380 |
JSONCONS_TMPL_ALL_MEMBER_TRAITS(1, ns::book, author, title, price) |
| 381 |
``` |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|