Metall  v0.29
A persistent memory allocator for data-centric analytics
value_from.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 METALL_JSON_VALUE_FROM_HPP
7 #define METALL_JSON_VALUE_FROM_HPP
8 
10 
11 namespace metall::json::jsndtl {
12 
13 namespace {
14 namespace bj = boost::json;
15 }
16 
17 template <typename allocator_type>
19  const bj::value &input_bj_value,
20  allocator_type allocator = allocator_type()) {
21  value<allocator_type> out_value(allocator);
22 
23  if (input_bj_value.is_null()) {
24  out_value.emplace_null();
25  } else if (input_bj_value.is_bool()) {
26  out_value = input_bj_value.as_bool();
27  } else if (input_bj_value.is_int64()) {
28  out_value = input_bj_value.as_int64();
29  } else if (input_bj_value.is_uint64()) {
30  out_value = input_bj_value.as_uint64();
31  } else if (input_bj_value.is_double()) {
32  out_value = input_bj_value.as_double();
33  } else if (input_bj_value.is_string()) {
34  const auto& str = input_bj_value.as_string();
35  out_value.emplace_string().assign(str.c_str(), str.size());
36  } else if (input_bj_value.is_array()) {
37  auto &out_array = out_value.emplace_array();
38  for (const auto &item : input_bj_value.as_array()) {
39  out_array.resize(out_array.size() + 1);
40  out_array[out_array.size() - 1] = value_from_impl(item, allocator);
41  }
42  } else if (input_bj_value.is_object()) {
43  auto &out_obj = out_value.emplace_object();
44  for (const auto &pair : input_bj_value.as_object()) {
45  out_obj[pair.key_c_str()] = value_from_impl(pair.value(), allocator);
46  }
47  }
48 
49  return out_value;
50 }
51 
52 template <typename allocator_type>
54  bj::value &&input_bj_value, allocator_type allocator = allocator_type()) {
55  bj::value tmp_input_bj_value(std::move(input_bj_value));
56  return value_from_impl(tmp_input_bj_value, allocator);
57 }
58 
59 } // namespace metall::json::jsndtl
60 
61 namespace metall::json {
62 
69 #ifdef DOXYGEN_SKIP
70 template <typename T, typename allocator_type = std::allocator<std::byte>>
72  T &&input_data, const allocator_type &allocator = allocator_type())
73 #else
74 template <typename T, typename allocator_type>
75 inline value<allocator_type> value_from(T &&input_data,
76  const allocator_type &allocator)
77 #endif
78 {
79  return jsndtl::value_from_impl(std::forward<T>(input_data), allocator);
80 }
81 
82 } // namespace metall::json
83 
84 #endif // METALL_JSON_VALUE_FROM_HPP
void resize(const std::size_t size)
Change the number of elements stored.
Definition: array.hpp:105
double & as_double()
Return a reference to the underlying double, or throw an exception.
Definition: value.hpp:444
object_type & emplace_object()
Set an empty object and return a reference. The old content is destroyed.
Definition: value.hpp:411
string_type & emplace_string()
Set an empty string and return a reference. The old content is destroyed.
Definition: value.hpp:397
std::uint64_t & as_uint64()
Return a reference to the underlying std::uint64_t, or throw an exception.
Definition: value.hpp:435
std::int64_t & as_int64()
Return a reference to the underlying std::int64_t, or throw an exception.
Definition: value.hpp:425
bool & as_bool()
Return a reference to the underlying bool, or throw an exception.
Definition: value.hpp:417
void emplace_null()
Set a null. The old content is destroyed.
Definition: value.hpp:365
array_type & emplace_array()
Set an empty array and return a reference. The old content is destroyed.
Definition: value.hpp:404
Definition: array.hpp:23
value< allocator_type > value_from_impl(const bj::value &input_bj_value, allocator_type allocator=allocator_type())
Definition: value_from.hpp:18
Namespace for Metall JSON container, which is in an experimental phase.
Definition: array.hpp:17
value< allocator_type > value_from(T &&input_data, const allocator_type &allocator=allocator_type())
Convert an input data and construct a JSON value.
Definition: value_from.hpp:71