userver: userver/dynamic_config/impl/to_json.hpp Source File
Loading...
Searching...
No Matches
to_json.hpp
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <string_view>
6
7#include <userver/formats/json_fwd.hpp>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace dynamic_config {
12struct ConfigDefault;
13} // namespace dynamic_config
14
15namespace dynamic_config::impl {
16
17std::string DoToJsonString(bool value);
18std::string DoToJsonString(double value);
19std::string DoToJsonString(std::uint64_t value);
20std::string DoToJsonString(std::int64_t value);
21std::string DoToJsonString(std::string_view value);
22
23template <typename T, typename /*Unused*/>
24struct IdWrapper {
25 using type = T;
26};
27
28template <typename T, typename Unused>
29using Id = typename IdWrapper<T, Unused>::type;
30
31// Avoids including formats::json::Value and formats::json::ValueBuilder
32// into frequently-used headers.
33template <typename T, typename Value = formats::json::Value>
34std::string ToJsonString(const T& value) {
35 if constexpr (std::is_same_v<T, bool>) {
36 return impl::DoToJsonString(static_cast<Id<bool, T>>(value));
37 } else if constexpr (std::is_floating_point_v<T>) {
38 return impl::DoToJsonString(static_cast<Id<double, T>>(value));
39 } else if constexpr (std::is_unsigned_v<T>) {
40 return impl::DoToJsonString(static_cast<Id<std::uint64_t, T>>(value));
41 } else if constexpr (std::is_integral_v<T>) {
42 return impl::DoToJsonString(static_cast<Id<std::int64_t, T>>(value));
43 } else if constexpr (std::is_convertible_v<const T&, std::string_view>) {
44 return impl::DoToJsonString(static_cast<Id<std::string_view, T>>(value));
45 } else {
46 return ToString(Serialize(value, formats::serialize::To<Value>{}));
47 }
48}
49
50std::string SingleToDocsMapString(std::string_view name,
51 std::string_view value);
52
53std::string MultipleToDocsMapString(const ConfigDefault* data,
54 std::size_t size);
55
56template <typename T>
57std::string ValueToDocsMapString(std::string_view name, const T& value) {
58 return impl::SingleToDocsMapString(name, impl::ToJsonString(value));
59}
60
61} // namespace dynamic_config::impl
62
63USERVER_NAMESPACE_END