userver: userver/formats/json/value.hpp Source File
Loading...
Searching...
No Matches
value.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/formats/json/value.hpp
4/// @brief @copybrief formats::json::Value
5
6#include <chrono>
7#include <iosfwd>
8#include <string_view>
9#include <type_traits>
10
11#include <userver/compiler/impl/nodebug.hpp>
12#include <userver/formats/common/items.hpp>
13#include <userver/formats/common/meta.hpp>
14#include <userver/formats/json/exception.hpp>
15#include <userver/formats/json/impl/types.hpp>
16#include <userver/formats/json/iterator.hpp>
17#include <userver/formats/json/string_builder_fwd.hpp>
18#include <userver/formats/parse/common.hpp>
19
20USERVER_NAMESPACE_BEGIN
21
22namespace logging {
23class LogHelper;
24} // namespace logging
25
26namespace formats::json {
27namespace impl {
28class InlineObjectBuilder;
29class InlineArrayBuilder;
30class MutableValueWrapper;
31class StringBuffer;
32class StableSerializer;
33
34// do not make a copy of string
35impl::Value MakeJsonStringViewValue(std::string_view view);
36
37} // namespace impl
38
39class ValueBuilder;
40struct PrettyFormat;
41class Schema;
42
43namespace parser {
44class JsonValueParser;
45} // namespace parser
46
47/// @ingroup userver_universal userver_containers userver_formats
48///
49/// @brief Non-mutable JSON value representation.
50///
51/// Class provides non mutable access JSON value. For modification and
52/// construction of new JSON values use formats::json::ValueBuilder.
53///
54/// ## Example usage:
55///
56/// @snippet universal/src/formats/json/value_test.cpp Sample formats::json::Value usage
57///
58/// @see @ref scripts/docs/en/userver/formats.md
59///
60/// To iterate over `Value` as object use formats::common::Items.
61class Value {
62public:
63 struct IterTraits {
64 using ValueType = formats::json::Value;
65 using Reference = const formats::json::Value&;
66 using Pointer = const formats::json::Value*;
67 using ContainerType = Value;
68 };
70
71 using const_iterator = Iterator<IterTraits, common::IteratorDirection::kForward>;
72 using const_reverse_iterator = Iterator<IterTraits, common::IteratorDirection::kReverse>;
73 using Exception = formats::json::Exception;
74 using ParseException = formats::json::ParseException;
75 using ExceptionWithPath = formats::json::ExceptionWithPath;
76 using Builder = ValueBuilder;
77
78 /// @brief Constructs a Value that holds a null.
80
81 Value(const Value&) = default;
82 Value(Value&&) noexcept;
83
84 Value& operator=(const Value&) & = default;
85 Value& operator=(Value&&) noexcept;
86
87 template <class T>
88 Value& operator=(T&&) && {
89 static_assert(
90 !sizeof(T),
91 "You're assigning to a temporary formats::json::Value! Use "
92 "formats::json::ValueBuilder for data modifications."
93 );
94 return *this;
95 }
96
97 /// @brief Access member by key for read.
98 /// @throw TypeMismatchException if not a missing value, an object or null.
99 Value operator[](std::string_view key) const;
100 /// @brief Access array member by index for read.
101 /// @throw TypeMismatchException if not an array value.
102 /// @throw OutOfBoundsException if index is greater or equal
103 /// than size.
104 Value operator[](std::size_t index) const;
105
106 /// @brief Returns an iterator to the beginning of the held array or map.
107 /// @throw TypeMismatchException if not an array, object, or null.
108 ///
109 /// To iterate over `Value` as object use formats::common::Items.
110 const_iterator begin() const;
111
112 /// @brief Returns an iterator to the end of the held array or map.
113 /// @throw TypeMismatchException if not an array, object, or null.
114 const_iterator end() const;
115
116 /// @brief Returns an iterator to the reversed begin of the held array.
117 /// @throw TypeMismatchException if not an array or null.
118 const_reverse_iterator rbegin() const;
119
120 /// @brief Returns an iterator to the reversed end of the held array.
121 /// @throw TypeMismatchException if not an array or null.
122 const_reverse_iterator rend() const;
123
124 /// @brief Returns whether the array or object is empty.
125 /// Returns true for null.
126 /// @throw TypeMismatchException if not an array, object, or null.
127 bool IsEmpty() const;
128
129 /// @brief Returns array size, object members count, or 0 for null.
130 /// @throw TypeMismatchException if not an array, object, or null.
131 std::size_t GetSize() const;
132
133 /// @brief Compares values.
134 /// @throw MemberMissingException if `*this` or `other` is missing.
135 bool operator==(const Value& other) const;
136
137 /// @brief Returns true if *this holds nothing. When `IsMissing()` returns
138 /// `true` any attempt to get the actual value or iterate over *this will
139 bool IsMissing() const noexcept;
140
141 /// @brief Returns true if *this holds a null (Type::kNull).
142 bool IsNull() const noexcept;
143
144 /// @brief Returns true if *this holds a bool.
145 bool IsBool() const noexcept;
146
147 /// @brief Returns true if *this holds an int.
148 bool IsInt() const noexcept;
149
150 /// @brief Returns true if *this holds an int64_t.
151 bool IsInt64() const noexcept;
152
153 /// @brief Returns true if *this holds an uint.
154 bool IsUInt() const noexcept;
155
156 /// @brief Returns true if *this holds an uint64_t.
157 bool IsUInt64() const noexcept;
158
159 /// @brief Returns true if *this holds a double.
160 bool IsDouble() const noexcept;
161
162 /// @brief Returns true if *this holds a number (integer or floating point).
163 bool IsNumber() const noexcept;
164
165 /// @brief Returns true if *this is holds a std::string.
166 bool IsString() const noexcept;
167
168 /// @brief Returns true if *this is holds an array (Type::kArray).
169 bool IsArray() const noexcept;
170
171 /// @brief Returns true if *this holds a map (Type::kObject).
172 bool IsObject() const noexcept;
173
174 /// @brief Returns value of *this converted to the result type of
175 /// Parse(const Value&, parse::To<T>). Almost always it is T.
176 /// @throw Anything derived from std::exception.
177 ///
178 /// ## Example usage:
179 ///
180 /// @snippet universal/src/formats/json/value_test.cpp Sample formats::json::Value::As<T>() usage
181 ///
182 /// @see @ref scripts/docs/en/userver/formats.md
183 template <typename T>
184 auto As() const;
185
186 /// @brief Returns value of *this converted to T or T(args) if
187 /// this->IsMissing().
188 /// @throw Anything derived from std::exception.
189 template <typename T, typename First, typename... Rest>
190 auto As(First&& default_arg, Rest&&... more_default_args) const;
191
192 /// @brief Returns value of *this converted to T or T() if this->IsMissing().
193 /// @throw Anything derived from std::exception.
194 /// @note Use as `value.As<T>({})`
195 template <typename T>
197
198 /// @brief Extracts the specified type with relaxed type checks.
199 /// For example, `true` may be converted to 1.0.
200 template <typename T>
201 T ConvertTo() const;
202
203 /// Extracts the specified type with strict type checks, or constructs the
204 /// default value when the field is not present
205 template <typename T, typename First, typename... Rest>
206 T ConvertTo(First&& default_arg, Rest&&... more_default_args) const;
207
208 /// @brief Returns true if *this holds a `key`.
209 /// @throw TypeMismatchException if `*this` is not a map or null.
210 bool HasMember(std::string_view key) const;
211
212 /// @brief Returns full path to this value.
213 std::string GetPath() const;
214
215 /// @cond
216 void DropRootPath();
217 /// @endcond
218
219 /// @brief Returns new value that is an exact copy of the existing one
220 /// but references different memory (a deep copy of a *this). The returned
221 /// value is a root value with path '/'.
222 /// @throws MemberMissingException if `this->IsMissing()`.
223 Value Clone() const;
224
225 /// @throw MemberMissingException if `this->IsMissing()`.
226 void CheckNotMissing() const;
227
228 /// @throw TypeMismatchException if `*this` is not an array or null.
229 void CheckArrayOrNull() const;
230
231 /// @throw TypeMismatchException if `*this` is not a map or null.
232 void CheckObjectOrNull() const;
233
234 /// @throw TypeMismatchException if `*this` is not an array.
235 void CheckArray() const;
236
237 /// @throw TypeMismatchException if `*this` is not a map.
238 void CheckObject() const;
239
240 /// @throw TypeMismatchException if `*this` is not a map, array or null.
242
243 /// @throw TypeMismatchException if `*this` is not an array or null;
244 /// `OutOfBoundsException` if `index >= this->GetSize()`.
245 void CheckInBounds(std::size_t index) const;
246
247 /// @brief Returns true if *this is a first (root) value.
248 bool IsRoot() const noexcept;
249
250 /// @brief Returns true if `*this` and `other` reference the value by the same
251 /// pointer.
252 bool DebugIsReferencingSameMemory(const Value& other) const { return value_ptr_ == other.value_ptr_; }
253
254private:
255 struct EmplaceEnabler {
256 explicit EmplaceEnabler() = default;
257 };
258
259 class LazyDetachedPath;
260
261public:
262 /// @cond
263 Value(
264 EmplaceEnabler,
265 const impl::VersionedValuePtr& root,
266 const impl::Value* root_ptr_for_path,
267 const impl::Value* value_ptr,
268 int depth
269 );
270
271 Value(
272 EmplaceEnabler,
273 const impl::VersionedValuePtr& root,
274 impl::Value* root_ptr_for_path,
275 LazyDetachedPath&& lazy_detached_path
276 );
277 /// @endcond
278
279private:
280 explicit Value(impl::VersionedValuePtr root) noexcept;
281
282 bool IsUniqueReference() const;
283 void EnsureNotMissing() const;
284 const impl::Value& GetNative() const;
285 impl::Value& GetNative();
286 impl::Value MoveNativeOrCopy(::rapidjson::CrtAllocator& allocator) &&;
287 void SetNative(impl::Value&); // does not copy
288 int GetExtendedType() const;
289
290 impl::VersionedValuePtr holder_{};
291 impl::Value* root_ptr_for_path_{nullptr};
292 impl::Value* value_ptr_{nullptr};
293 /// Depth of the node to ease recursive traversal in GetPath()
294 int depth_{0};
295
296 // We don't want to calculate the path for missing node before it is
297 // explicitly requested, because GetPath() call is very costly.
298 // This helps with patterns like 'json["missing"].As<T>({})':
299 // path is not needed here (note default arg), and if we have a lot of missing
300 // keys during parsing we save a lot of expensive calculations.
301 class LazyDetachedPath final {
302 public:
303 LazyDetachedPath() noexcept;
304 LazyDetachedPath(impl::Value* parent_value_ptr, int parent_depth, std::string_view key);
305
306 LazyDetachedPath(const LazyDetachedPath&);
307 LazyDetachedPath(LazyDetachedPath&&) noexcept;
308 LazyDetachedPath& operator=(const LazyDetachedPath&);
309 LazyDetachedPath& operator=(LazyDetachedPath&&) noexcept;
310
311 std::string Get(const impl::Value* root) const;
312 LazyDetachedPath Chain(std::string_view key) const;
313
314 private:
315 impl::Value* parent_value_ptr_{nullptr};
316 int parent_depth_{0};
317 std::string virtual_path_{};
318 };
319
320 LazyDetachedPath lazy_detached_path_;
321
322 template <typename, common::IteratorDirection>
323 friend class Iterator;
324 friend class ValueBuilder;
325 friend class StringBuilder;
326 friend class Schema;
327 friend class impl::InlineObjectBuilder;
328 friend class impl::InlineArrayBuilder;
329 friend class impl::MutableValueWrapper;
330 friend class parser::JsonValueParser;
331 friend class impl::StringBuffer;
332 friend class impl::StableSerializer;
333
334 friend bool Parse(const Value& value, parse::To<bool>);
335 friend std::int64_t Parse(const Value& value, parse::To<std::int64_t>);
336 friend std::uint64_t Parse(const Value& value, parse::To<std::uint64_t>);
337 friend double Parse(const Value& value, parse::To<double>);
338 friend std::string Parse(const Value& value, parse::To<std::string>);
339
340 friend formats::json::Value FromString(std::string_view);
341 friend formats::json::Value FromStream(std::istream&);
342 friend void Serialize(const formats::json::Value&, std::ostream&);
343 friend std::string ToString(const formats::json::Value&);
344 friend std::string ToStableString(const formats::json::Value&);
345 friend std::string ToStableString(formats::json::Value&&);
346 friend std::string ToPrettyString(const formats::json::Value& doc, PrettyFormat format);
347 friend logging::LogHelper& operator<<(logging::LogHelper&, const Value&);
348};
349
350template <typename T>
351USERVER_IMPL_NODEBUG auto Value::As() const {
352 static_assert(
353 formats::common::impl::HasParse<Value, T>,
354 "There is no `Parse(const Value&, formats::parse::To<T>)` in namespace of `T` or `formats::parse`. "
355 "Probably you forgot to include the <userver/formats/parse/common_containers.hpp> or you "
356 "have not provided a `Parse` function overload."
357 );
358
359 return Parse(*this, formats::parse::To<T>{});
360}
361
362bool Parse(const Value& value, parse::To<bool>);
363
364std::int64_t Parse(const Value& value, parse::To<std::int64_t>);
365
366std::uint64_t Parse(const Value& value, parse::To<std::uint64_t>);
367
368double Parse(const Value& value, parse::To<double>);
369
370std::string Parse(const Value& value, parse::To<std::string>);
371
372template <>
373bool Value::ConvertTo<bool>() const;
374
375template <>
376int64_t Value::ConvertTo<int64_t>() const;
377
378template <>
379uint64_t Value::ConvertTo<uint64_t>() const;
380
381template <>
382double Value::ConvertTo<double>() const;
383
384template <>
385std::string Value::ConvertTo<std::string>() const;
386
387template <typename T, typename First, typename... Rest>
388auto Value::As(First&& default_arg, Rest&&... more_default_args) const {
389 if (IsMissing() || IsNull()) {
390 // intended raw ctor call, sometimes casts
391 // NOLINTNEXTLINE(google-readability-casting)
392 return decltype(As<T>())(std::forward<First>(default_arg), std::forward<Rest>(more_default_args)...);
393 }
394 return As<T>();
395}
396
397template <typename T>
399 return (IsMissing() || IsNull()) ? decltype(As<T>())() : As<T>();
400}
401
402template <typename T>
403USERVER_IMPL_NODEBUG T Value::ConvertTo() const {
404 if constexpr (formats::common::impl::HasConvert<Value, T>) {
405 return Convert(*this, formats::parse::To<T>{});
406 } else if constexpr (formats::common::impl::HasParse<Value, T>) {
407 return Parse(*this, formats::parse::To<T>{});
408 } else {
409 static_assert(
410 !sizeof(T),
411 "There is no `Convert(const Value&, formats::parse::To<T>)` or `Parse(const Value&, formats::parse::To<T>)`"
412 "in namespace of `T` or `formats::parse`. Probably you have not provided a `Convert` function overload."
413 );
414 }
415}
416
417template <typename T, typename First, typename... Rest>
418T Value::ConvertTo(First&& default_arg, Rest&&... more_default_args) const {
419 if (IsMissing() || IsNull()) {
420 // NOLINTNEXTLINE(google-readability-casting)
421 return T(std::forward<First>(default_arg), std::forward<Rest>(more_default_args)...);
422 }
423 return ConvertTo<T>();
424}
425
426inline Value Parse(const Value& value, parse::To<Value>) { return value; }
427
428inline Value Parse(Value&& value, parse::To<Value>) { return std::move(value); }
429
430std::chrono::microseconds Parse(const Value& value, parse::To<std::chrono::microseconds>);
431
432std::chrono::milliseconds Parse(const Value& value, parse::To<std::chrono::milliseconds>);
433
434std::chrono::minutes Parse(const Value& value, parse::To<std::chrono::minutes>);
435
436std::chrono::hours Parse(const Value& value, parse::To<std::chrono::hours>);
437
438/// @brief Wrapper for handy python-like iteration over a map
439///
440/// @snippet universal/src/formats/common/items_test.cpp Items const iteration
441using formats::common::Items;
442
443/// gtest formatter for formats::json::Value
444void PrintTo(const Value&, std::ostream*);
445
446} // namespace formats::json
447
448/// @brief Although we provide user defined literals, please beware that
449/// 'using namespace ABC' may contradict code style of your company.
450namespace formats::literals {
451
452json::Value operator""_json(const char* str, std::size_t len);
453
454} // namespace formats::literals
455
456USERVER_NAMESPACE_END