5#ifndef SIMDJSON_GENERIC_STRING_BUILDER_H
7#ifndef SIMDJSON_CONDITIONAL_INCLUDE
8#define SIMDJSON_GENERIC_STRING_BUILDER_H
9#include "simdjson/generic/implementation_simdjson_result_base.h"
15#if SIMDJSON_SUPPORTS_CONCEPTS
17namespace SIMDJSON_IMPLEMENTATION {
22template <
typename T,
typename =
void>
23struct has_custom_serialization : std::false_type {};
25inline constexpr struct serialize_tag {
27 requires custom_deserializable<T>
28 constexpr void operator()(SIMDJSON_IMPLEMENTATION::builder::string_builder& b, T& obj)
const{
29 return tag_invoke(*
this, b, obj);
35struct has_custom_serialization<T, std::void_t<
36 decltype(tag_invoke(serialize, std::declval<SIMDJSON_IMPLEMENTATION::builder::string_builder&>(), std::declval<T&>()))
37>> : std::true_type {};
40constexpr bool require_custom_serialization = has_custom_serialization<T>::value;
45namespace SIMDJSON_IMPLEMENTATION {
58 simdjson_inline
string_builder(
size_t initial_capacity = DEFAULT_INITIAL_CAPACITY);
60 static constexpr size_t DEFAULT_INITIAL_CAPACITY = 1024;
68 template<
typename number_type,
69 typename =
typename std::enable_if<std::is_arithmetic<number_type>::value>::type>
70 simdjson_inline
void append(number_type v)
noexcept;
75 simdjson_inline
void append(
char c)
noexcept;
85 simdjson_inline
void clear()
noexcept;
98#if SIMDJSON_SUPPORTS_CONCEPTS
99 template<constevalutil::fixed_
string key>
118 simdjson_inline
void append_raw(
const char *c)
noexcept;
138 simdjson_inline
void end_array()
noexcept;
155 template<
typename key_type,
typename value_type>
156 simdjson_inline
void append_key_value(key_type key, value_type value)
noexcept;
157#if SIMDJSON_SUPPORTS_CONCEPTS
158 template<constevalutil::fixed_
string key,
typename value_type>
162 template <concepts::optional_type T>
163 requires(!require_custom_serialization<T>)
164 simdjson_inline
void append(
const T &opt);
166 template <
typename T>
167 requires(require_custom_serialization<T>)
168 simdjson_inline
void append(
const T &val);
171 template <
typename T>
172 requires(std::is_convertible<T, std::string_view>::value ||
173 std::is_same<T, const char*>::value )
174 simdjson_inline
void append(
const T &value);
176#if SIMDJSON_SUPPORTS_RANGES && SIMDJSON_SUPPORTS_CONCEPTS
178 template <std::ranges::range R>
179requires (!std::is_convertible<R, std::string_view>::value)
180 simdjson_inline
void append(
const R &range)
noexcept;
186 simdjson_inline
void append_raw(std::string_view input)
noexcept;
192 simdjson_inline
void append_raw(
const char *str,
size_t len)
noexcept;
193#if SIMDJSON_EXCEPTIONS
201 simdjson_inline
operator std::string()
const noexcept(
false);
210 simdjson_inline
operator std::string_view()
const noexcept(
false) simdjson_lifetime_bound;
242 simdjson_inline
size_t size()
const noexcept;
250 simdjson_inline
bool capacity_check(
size_t upcoming_bytes);
257 simdjson_inline
void grow_buffer(
size_t desired_capacity);
262 simdjson_inline
void set_valid(
bool valid)
noexcept;
264 std::unique_ptr<char[]> buffer{};
276#if !SIMDJSON_STATIC_REFLECTION
279simdjson_warn_unused
simdjson_result<std::string> to_json(
const Z &z,
size_t initial_capacity = simdjson::SIMDJSON_IMPLEMENTATION::builder::string_builder::DEFAULT_INITIAL_CAPACITY) {
283 auto e = b.view().get(s);
285 return std::string(s);
288simdjson_warn_unused simdjson_error to_json(
const Z &z, std::string &s,
size_t initial_capacity = simdjson::SIMDJSON_IMPLEMENTATION::builder::string_builder::DEFAULT_INITIAL_CAPACITY) {
292 auto e = b.view().get(sv);
294 s.assign(sv.data(), sv.size());
299#if SIMDJSON_SUPPORTS_CONCEPTS
A builder for JSON strings representing documents.
simdjson_inline void append_comma() noexcept
Append "," to the buffer.
simdjson_inline void escape_and_append_with_quotes(std::string_view input) noexcept
Append the std::string_view surrounded by double quotes, after escaping it.
simdjson_inline void append_key_value(key_type key, value_type value) noexcept
Append a key-value pair to the buffer.
simdjson_inline void append(number_type v) noexcept
Append number (includes Booleans).
simdjson_inline void escape_and_append(std::string_view input) noexcept
Append the std::string_view, after escaping it.
simdjson_inline void end_array() noexcept
Append "]" to the buffer.
simdjson_inline void start_object() noexcept
Append "{" to the buffer.
simdjson_inline simdjson_result< std::string_view > view() const noexcept
Returns a view on the written JSON buffer.
simdjson_inline size_t size() const noexcept
Returns the current size of the written JSON buffer.
simdjson_inline void append_raw(const char *c) noexcept
Append the C string directly, without escaping.
simdjson_inline simdjson_result< const char * > c_str() noexcept
Appends the null character to the buffer and returns a pointer to the beginning of the written JSON b...
simdjson_inline void start_array() noexcept
Append "[" to the buffer.
simdjson_inline void clear() noexcept
Clear the content.
simdjson_inline void end_object() noexcept
Append "}" to the buffer.
simdjson_inline void append_null() noexcept
Append the string 'null'.
simdjson_inline void append_colon() noexcept
Append ":" to the buffer.
simdjson_inline bool validate_unicode() const noexcept
Return true if the content is valid UTF-8.
The top level simdjson namespace, containing everything the library provides.
The result of a simdjson operation that could fail.