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 constexpr void operator()(SIMDJSON_IMPLEMENTATION::builder::string_builder& b, T&& obj)
const{
28 return tag_invoke(*
this, b, std::forward<T>(obj));
34struct has_custom_serialization<T, std::void_t<
35 decltype(tag_invoke(serialize, std::declval<SIMDJSON_IMPLEMENTATION::builder::string_builder&>(), std::declval<T&>()))
36>> : std::true_type {};
39constexpr bool require_custom_serialization = has_custom_serialization<T>::value;
44namespace SIMDJSON_IMPLEMENTATION {
57 simdjson_inline
string_builder(
size_t initial_capacity = DEFAULT_INITIAL_CAPACITY);
59 static constexpr size_t DEFAULT_INITIAL_CAPACITY = 1024;
67 template<
typename number_type,
68 typename =
typename std::enable_if<std::is_arithmetic<number_type>::value>::type>
69 simdjson_inline
void append(number_type v)
noexcept;
74 simdjson_inline
void append(
char c)
noexcept;
84 simdjson_inline
void clear()
noexcept;
97#if SIMDJSON_SUPPORTS_CONCEPTS
98 template<constevalutil::fixed_
string key>
117 simdjson_inline
void append_raw(
const char *c)
noexcept;
137 simdjson_inline
void end_array()
noexcept;
154 template<
typename key_type,
typename value_type>
155 simdjson_inline
void append_key_value(key_type key, value_type value)
noexcept;
156#if SIMDJSON_SUPPORTS_CONCEPTS
157 template<constevalutil::fixed_
string key,
typename value_type>
161 template <concepts::optional_type T>
162 requires(!require_custom_serialization<T>)
163 simdjson_inline
void append(
const T &opt);
165 template <
typename T>
166 requires(require_custom_serialization<T>)
167 simdjson_inline
void append(T &&val);
170 template <
typename T>
171 requires(std::is_convertible<T, std::string_view>::value ||
172 std::is_same<T, const char*>::value )
173 simdjson_inline
void append(
const T &value);
175#if SIMDJSON_SUPPORTS_RANGES && SIMDJSON_SUPPORTS_CONCEPTS
177 template <std::ranges::range R>
178requires (!std::is_convertible<R, std::string_view>::value && !require_custom_serialization<R>)
179 simdjson_inline
void append(
const R &range)
noexcept;
185 simdjson_inline
void append_raw(std::string_view input)
noexcept;
191 simdjson_inline
void append_raw(
const char *str,
size_t len)
noexcept;
192#if SIMDJSON_EXCEPTIONS
200 simdjson_inline
operator std::string()
const noexcept(
false);
209 simdjson_inline
operator std::string_view()
const noexcept(
false) simdjson_lifetime_bound;
241 simdjson_inline
size_t size()
const noexcept;
249 simdjson_inline
bool capacity_check(
size_t upcoming_bytes);
256 simdjson_inline
void grow_buffer(
size_t desired_capacity);
261 simdjson_inline
void set_valid(
bool valid)
noexcept;
263 std::unique_ptr<char[]> buffer{};
275#if !SIMDJSON_STATIC_REFLECTION
278simdjson_warn_unused
simdjson_result<std::string> to_json(
const Z &z,
size_t initial_capacity = simdjson::SIMDJSON_IMPLEMENTATION::builder::string_builder::DEFAULT_INITIAL_CAPACITY) {
282 auto e = b.view().get(s);
284 return std::string(s);
287simdjson_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) {
291 auto e = b.view().get(sv);
293 s.assign(sv.data(), sv.size());
298#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.