azure-core
Loading...
Searching...
No Matches
json_optional.hpp
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
10#pragma once
11
12#include "azure/core/internal/json/json.hpp"
14
15#include <functional>
16#include <string>
17
18namespace Azure { namespace Core { namespace Json { namespace _internal {
19
24 struct JsonOptional final
25 {
36 template <class T>
37 static inline void SetIfExists(
38 Azure::Nullable<T>& destination,
39 Azure::Core::Json::_internal::json const& jsonKey,
40 std::string const& key) noexcept
41 {
42 if (jsonKey.contains(key) && !jsonKey[key].is_null()) // In Json and not-Null
43 {
44 destination = jsonKey[key].get<T>();
45 }
46 }
47
62 template <class V, class T>
63 static inline void SetIfExists(
64 T& destination,
65 Azure::Core::Json::_internal::json const& jsonKey,
66 std::string const& key,
67 std::function<T(V value)> decorator) noexcept
68 {
69 if (jsonKey.contains(key))
70 {
71 if (!jsonKey[key].is_null())
72 {
73 destination = decorator(jsonKey[key].get<V>());
74 }
75 }
76 }
77
90 template <class T, class V>
91 static inline void SetIfExists(
92 Azure::Nullable<V>& destination,
93 Azure::Core::Json::_internal::json const& jsonKey,
94 std::string const& key,
95 std::function<V(T value)> decorator) noexcept
96 {
97 if (jsonKey.contains(key))
98 {
99 destination = decorator(jsonKey[key].get<T>());
100 }
101 }
102
103 template <class T>
104 static inline void SetFromIfPredicate(
105 T const& source,
106 std::function<bool(T const&)> predicate,
107 Azure::Core::Json::_internal::json& jsonKey,
108 std::string const& keyName,
109 std::function<std::string(T const&)> decorator)
110 {
111 if (predicate(source))
112 {
113 jsonKey[keyName] = decorator(source);
114 }
115 }
116
117 template <class T, class R>
118 static inline void SetFromNullable(
119 Azure::Nullable<T> const& source,
120 Azure::Core::Json::_internal::json& jsonKey,
121 std::string const& keyName,
122 std::function<R(T const&)> factory)
123 {
124 if (source)
125 {
126 jsonKey[keyName] = factory(source.Value());
127 }
128 }
129
130 template <class T>
131 static inline void SetFromNullable(
132 Azure::Nullable<T> const& source,
133 Azure::Core::Json::_internal::json& jsonKey,
134 std::string const& keyName)
135 {
136 if (source)
137 {
138 jsonKey[keyName] = source.Value();
139 }
140 }
141 };
142}}}} // namespace Azure::Core::Json::_internal
Manages an optional contained value, i.e. a value that may or may not be present.
Definition nullable.hpp:30
const T & Value() const &noexcept
Get the contained value.
Definition nullable.hpp:236
Compute the hash value for the input binary data, using SHA256, SHA384 and SHA512.
Definition azure_assert.hpp:57
Manages an optional contained value, i.e. a value that may or may not be present.