userver: userver/formats/json/string_builder.hpp Source File
Loading...
Searching...
No Matches
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/// @ingroup userver_universal userver_containers userver_formats userver_formats_serialize_sax
20///
21/// @brief SAX like builder of JSON string. Use with extreme caution and only in
22/// performance critical part of your code.
23///
24/// Prefer using WriteToStream function to add data to the StringBuilder.
25///
26/// ## Example usage:
27///
28/// @snippet formats/json/string_builder_test.cpp Sample formats::json::StringBuilder usage
29///
30/// @see @ref scripts/docs/en/userver/formats.md
31class StringBuilder final : public serialize::SaxStream {
32public:
33 // Required by the WriteToStream fallback to Serialize
34 using Value = formats::json::Value;
35
36 StringBuilder();
37 ~StringBuilder();
38
39 /// Construct this guard on new object start and its destructor will end the
40 /// object
41 class ObjectGuard final {
42 public:
43 explicit ObjectGuard(StringBuilder& sw);
44 ~ObjectGuard();
45
46 private:
47 StringBuilder& sw_;
48 };
49
50 /// Construct this guard on new array start and its destructor will end the
51 /// array
52 class ArrayGuard final {
53 public:
54 explicit ArrayGuard(StringBuilder& sw);
55 ~ArrayGuard();
56
57 private:
58 StringBuilder& sw_;
59 };
60
61 /// @return JSON string
62 std::string GetString() const;
63 std::string_view GetStringView() const;
64
65 void WriteNull();
66 void WriteString(std::string_view value);
67 void WriteBool(bool value);
68 void WriteInt64(int64_t value);
69 void WriteUInt64(uint64_t value);
70 void WriteDouble(double value);
71
72 /// ONLY for objects/dicts: write key
73 void Key(std::string_view sw);
74
75 /// Appends raw data
76 void WriteRawString(std::string_view value);
77
78 void WriteValue(const Value& value);
79
80private:
81 struct Impl;
82 utils::FastPimpl<Impl, 112, 8> impl_;
83};
84
85void WriteToStream(bool value, StringBuilder& sw);
86void WriteToStream(long long value, StringBuilder& sw);
87void WriteToStream(unsigned long long value, StringBuilder& sw);
88void WriteToStream(int value, StringBuilder& sw);
89void WriteToStream(unsigned value, StringBuilder& sw);
90void WriteToStream(long value, StringBuilder& sw);
91void WriteToStream(unsigned long value, StringBuilder& sw);
92void WriteToStream(double value, StringBuilder& sw);
93void WriteToStream(const char* value, StringBuilder& sw);
94void WriteToStream(std::string_view value, StringBuilder& sw);
95void WriteToStream(const formats::json::Value& value, StringBuilder& sw);
96void WriteToStream(const std::string& value, StringBuilder& sw);
97
98void WriteToStream(std::chrono::system_clock::time_point tp, StringBuilder& sw);
99
100} // namespace formats::json
101
102USERVER_NAMESPACE_END