1#ifndef SIMDJSON_GENERIC_STRING_BUILDER_H
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_STRING_BUILDER_H
5#include "simdjson/generic/implementation_simdjson_result_base.h"
11#if SIMDJSON_SUPPORTS_CONCEPTS
13namespace SIMDJSON_IMPLEMENTATION {
18template <
typename T,
typename =
void>
19struct has_custom_serialization : std::false_type {};
21inline constexpr struct serialize_tag {
23 constexpr void operator()(SIMDJSON_IMPLEMENTATION::builder::string_builder& b, T&& obj)
const{
24 return tag_invoke(*
this, b, std::forward<T>(obj));
30struct has_custom_serialization<T, std::void_t<
31 decltype(tag_invoke(serialize, std::declval<SIMDJSON_IMPLEMENTATION::builder::string_builder&>(), std::declval<T&>()))
32>> : std::true_type {};
35constexpr bool require_custom_serialization = has_custom_serialization<T>::value;
40namespace SIMDJSON_IMPLEMENTATION {
53 simdjson_inline
string_builder(
size_t initial_capacity = DEFAULT_INITIAL_CAPACITY);
55 static constexpr size_t DEFAULT_INITIAL_CAPACITY = 1024;
63 template<
typename number_type,
64 typename =
typename std::enable_if<std::is_arithmetic<number_type>::value>::type>
65 simdjson_inline
void append(number_type v)
noexcept;
70 simdjson_inline
void append(
char c)
noexcept;
80 simdjson_inline
void clear()
noexcept;
93#if SIMDJSON_SUPPORTS_CONCEPTS
94 template<constevalutil::fixed_
string key>
113 simdjson_inline
void append_raw(
const char *c)
noexcept;
133 simdjson_inline
void end_array()
noexcept;
150 template<
typename key_type,
typename value_type>
151 simdjson_inline
void append_key_value(key_type key, value_type value)
noexcept;
152#if SIMDJSON_SUPPORTS_CONCEPTS
153 template<constevalutil::fixed_
string key,
typename value_type>
157 template <concepts::optional_type T>
158 requires(!require_custom_serialization<T>)
159 simdjson_inline
void append(
const T &opt);
161 template <
typename T>
162 requires(require_custom_serialization<T>)
163 simdjson_inline
void append(T &&val);
166 template <
typename T>
167 requires(std::is_convertible<T, std::string_view>::value ||
168 std::is_same<T, const char*>::value )
169 simdjson_inline
void append(
const T &value);
171#if SIMDJSON_SUPPORTS_RANGES && SIMDJSON_SUPPORTS_CONCEPTS
173 template <std::ranges::range R>
174requires (!std::is_convertible<R, std::string_view>::value && !require_custom_serialization<R>)
175 simdjson_inline
void append(
const R &range)
noexcept;
181 simdjson_inline
void append_raw(std::string_view input)
noexcept;
187 simdjson_inline
void append_raw(
const char *str,
size_t len)
noexcept;
188#if SIMDJSON_EXCEPTIONS
196 simdjson_inline
operator std::string()
const noexcept(
false);
205 simdjson_inline
operator std::string_view()
const noexcept(
false) simdjson_lifetime_bound;
237 simdjson_inline
size_t size()
const noexcept;
245 simdjson_inline
bool capacity_check(
size_t upcoming_bytes);
252 simdjson_inline
void grow_buffer(
size_t desired_capacity);
257 simdjson_inline
void set_valid(
bool valid)
noexcept;
259 std::unique_ptr<char[]> buffer{};
271#if !SIMDJSON_STATIC_REFLECTION
274simdjson_warn_unused
simdjson_result<std::string> to_json(
const Z &z,
size_t initial_capacity = simdjson::SIMDJSON_IMPLEMENTATION::builder::string_builder::DEFAULT_INITIAL_CAPACITY) {
278 auto e = b.view().get(s);
280 return std::string(s);
283simdjson_warn_unused
error_code to_json(
const Z &z, std::string &s,
size_t initial_capacity = simdjson::SIMDJSON_IMPLEMENTATION::builder::string_builder::DEFAULT_INITIAL_CAPACITY) {
287 auto e = b.view().get(sv);
289 s.assign(sv.data(), sv.size());
294#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.
error_code
All possible errors returned by simdjson.
The result of a simdjson operation that could fail.