Metall v0.30
A persistent memory allocator for data-centric analytics
 
Loading...
Searching...
No Matches
value_to.hpp
Go to the documentation of this file.
1// Copyright 2021 Lawrence Livermore National Security, LLC and other Metall
2// Project Developers. See the top-level COPYRIGHT file for details.
3//
4// SPDX-License-Identifier: (Apache-2.0 OR MIT)
5
6#ifndef METALLAL_JSON_VALUE_TO_HPP
7#define METALLAL_JSON_VALUE_TO_HPP
8
10
11namespace metall::json::jsndtl {
12
13namespace {
14namespace mj = metall::json;
15namespace bj = boost::json;
16} // namespace
17
18template <typename allocator_type>
19inline void value_to_impl_helper(const mj::value<allocator_type> &input_value,
20 bj::value *out_bj_value) {
21 if (input_value.is_bool()) {
22 *out_bj_value = input_value.as_bool();
23 } else if (input_value.is_int64()) {
24 *out_bj_value = input_value.as_int64();
25 } else if (input_value.is_uint64()) {
26 *out_bj_value = input_value.as_uint64();
27 } else if (input_value.is_double()) {
28 *out_bj_value = input_value.as_double();
29 } else if (input_value.is_string()) {
30 *out_bj_value = input_value.as_string().c_str();
31 } else if (input_value.is_array()) {
32 bj::array bj_array;
33 for (const auto &elem : input_value.as_array()) {
34 bj_array.emplace_back(mj::value_to<bj::value>(elem));
35 }
36 *out_bj_value = bj_array;
37 } else if (input_value.is_object()) {
38 bj::object bj_object;
39 for (const auto &elem : input_value.as_object()) {
40 bj_object[elem.key().data()] = mj::value_to<bj::value>(elem.value());
41 }
42 *out_bj_value = bj_object;
43 } else if (input_value.is_null()) {
44 out_bj_value->emplace_null();
45 }
46}
47
48template <typename allocator_type>
49inline bj::value value_to_impl(const mj::value<allocator_type> &input_value) {
50 // TODO: support this syntax
51 // return bj::value_from(input_value);
52
53 bj::value out_value;
54 value_to_impl_helper(input_value, &out_value);
55 return out_value;
56}
57
58} // namespace metall::json::jsndtl
59
60namespace metall::json {
61
62namespace {
63namespace mj = metall::json;
64}
65
71template <typename T, typename allocator_type>
75
76} // namespace metall::json
77
78#endif // METALLAL_JSON_VALUE_TO_HPP
JSON value. A container that holds a single bool, int64, uint64, double, JSON string,...
Definition value.hpp:82
array_type & as_array()
Return a reference to the underlying array, or throw an exception.
Definition value.hpp:458
bool is_null() const noexcept
Return true if this is a null.
Definition value.hpp:472
object_type & as_object()
Return a reference to the underlying object, or throw an exception.
Definition value.hpp:465
bool is_string() const noexcept
Return true if this is a string.
Definition value.hpp:495
bool is_int64() const noexcept
Return true if this is a int64.
Definition value.hpp:480
bool & as_bool()
Return a reference to the underlying bool, or throw an exception.
Definition value.hpp:417
bool is_uint64() const noexcept
Return true if this is a uint64.
Definition value.hpp:485
bool is_bool() const noexcept
Return true if this is a bool.
Definition value.hpp:477
bool is_object() const noexcept
Return true if this is a object.
Definition value.hpp:505
bool is_array() const noexcept
Return true if this is an array.
Definition value.hpp:500
std::uint64_t & as_uint64()
Return a reference to the underlying std::uint64_t, or throw an exception.
Definition value.hpp:435
string_type & as_string()
Return a reference to the underlying string, or throw an exception.
Definition value.hpp:451
bool is_double() const noexcept
Return true if this is a double.
Definition value.hpp:490
std::int64_t & as_int64()
Return a reference to the underlying std::int64_t, or throw an exception.
Definition value.hpp:425
double & as_double()
Return a reference to the underlying double, or throw an exception.
Definition value.hpp:444
Definition array.hpp:23
void value_to_impl_helper(const mj::value< allocator_type > &input_value, bj::value *out_bj_value)
Definition value_to.hpp:19
bj::value value_to_impl(const mj::value< allocator_type > &input_value)
Definition value_to.hpp:49
Namespace for Metall JSON container, which is in an experimental phase.
Definition array.hpp:17
T value_to(const mj::value< allocator_type > &value)
Convert a JSON value to another data type.
Definition value_to.hpp:72