userver: userver/formats/json/string_builder.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
string_builder.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/formats/json/string_builder.hpp
4/// @brief @copybrief formats::json::StringBuilder
5
6#include <string>
7#include <string_view>
8
9#include <userver/formats/json/string_builder_fwd.hpp>
10#include <userver/formats/json/value.hpp>
11#include <userver/formats/serialize/to.hpp>
12#include <userver/formats/serialize/write_to_stream.hpp>
13#include <userver/utils/fast_pimpl.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace formats::json {
18
19// clang-format off
20
21/// @ingroup userver_universal userver_containers userver_formats userver_formats_serialize_sax
22///
23/// @brief SAX like builder of JSON string. Use with extreme caution and only in
24/// performance critical part of your code.
25///
26/// Prefer using WriteToStream function to add data to the StringBuilder.
27///
28/// ## Example usage:
29///
30/// @snippet formats/json/string_builder_test.cpp Sample formats::json::StringBuilder usage
31///
32/// @see @ref scripts/docs/en/userver/formats.md
33
34// clang-format on
35
36class StringBuilder final : public serialize::SaxStream {
37 public:
38 // Required by the WriteToStream fallback to Serialize
39 using Value = formats::json::Value;
40
41 StringBuilder();
42 ~StringBuilder();
43
44 /// Construct this guard on new object start and its destructor will end the
45 /// object
46 class ObjectGuard final {
47 public:
48 explicit ObjectGuard(StringBuilder& sw);
49 ~ObjectGuard();
50
51 private:
52 StringBuilder& sw_;
53 };
54
55 /// Construct this guard on new array start and its destructor will end the
56 /// array
57 class ArrayGuard final {
58 public:
59 explicit ArrayGuard(StringBuilder& sw);
60 ~ArrayGuard();
61
62 private:
63 StringBuilder& sw_;
64 };
65
66 /// @return JSON string
67 std::string GetString() const;
68 std::string_view GetStringView() const;
69
70 void WriteNull();
71 void WriteString(std::string_view value);
72 void WriteBool(bool value);
73 void WriteInt64(int64_t value);
74 void WriteUInt64(uint64_t value);
75 void WriteDouble(double value);
76
77 /// ONLY for objects/dicts: write key
78 void Key(std::string_view sw);
79
80 /// Appends raw data
81 void WriteRawString(std::string_view value);
82
83 void WriteValue(const Value& value);
84
85 private:
86 struct Impl;
87 utils::FastPimpl<Impl, 112, 8> impl_;
88};
89
90void WriteToStream(bool value, StringBuilder& sw);
91void WriteToStream(long long value, StringBuilder& sw);
92void WriteToStream(unsigned long long value, StringBuilder& sw);
93void WriteToStream(int value, StringBuilder& sw);
94void WriteToStream(unsigned value, StringBuilder& sw);
95void WriteToStream(long value, StringBuilder& sw);
96void WriteToStream(unsigned long value, StringBuilder& sw);
97void WriteToStream(double value, StringBuilder& sw);
98void WriteToStream(const char* value, StringBuilder& sw);
99void WriteToStream(std::string_view value, StringBuilder& sw);
100void WriteToStream(const formats::json::Value& value, StringBuilder& sw);
101void WriteToStream(const std::string& value, StringBuilder& sw);
102
103void WriteToStream(std::chrono::system_clock::time_point tp, StringBuilder& sw);
104
105} // namespace formats::json
106
107USERVER_NAMESPACE_END