userver: formats::json::StringBuilder Class Reference
No Matches
formats::json::StringBuilder Class Referencefinal

#include <userver/formats/json/string_builder.hpp>

Detailed Description

SAX like builder of JSON string. Use with extreme caution and only in performance critical part of your code.

Prefer using WriteToStream function to add data to the StringBuilder.

Example usage:

namespace my_namespace {
struct MyKeyValue {
std::string field1;
int field2;
// The function must be declared in the namespace of your type
inline void WriteToStream(const MyKeyValue& data,
// A class that adds '{' in the constructor and '}' in the destructor to JSON
// Use the WriteToStream functions for values, don't work with StringBuilder
// directly
WriteToStream(data.field1, sw);
WriteToStream(data.field2, sw);
TEST(JsonStringBuilder, ExampleUsage) {
StringBuilder sb;
MyKeyValue data = {"one", 1};
WriteToStream(data, sb);
ASSERT_EQ(sb.GetString(), "{\"field1\":\"one\",\"field2\":1}");
} // namespace my_namespace
See also
Formats (JSON, YAML, BSON, ...)

Definition at line 36 of file string_builder.hpp.

+ Inheritance diagram for formats::json::StringBuilder:
+ Collaboration diagram for formats::json::StringBuilder:


class  ArrayGuard
class  ObjectGuard

Public Types

using Value = formats::json::Value

Public Member Functions

std::string GetString () const
std::string_view GetStringView () const
void WriteNull ()
void WriteString (std::string_view value)
void WriteBool (bool value)
void WriteInt64 (int64_t value)
void WriteUInt64 (uint64_t value)
void WriteDouble (double value)
void Key (std::string_view sw)
 ONLY for objects/dicts: write key.
void WriteRawString (std::string_view value)
 Appends raw data.
void WriteValue (const Value &value)

Member Typedef Documentation

◆ Value

Member Function Documentation

◆ GetString()

std::string formats::json::StringBuilder::GetString ( ) const
JSON string

The documentation for this class was generated from the following file: