simdjson 4.0.7
Ridiculously Fast JSON
Loading...
Searching...
No Matches
json_string_builder.h
1
5#ifndef SIMDJSON_GENERIC_STRING_BUILDER_H
6
7#ifndef SIMDJSON_CONDITIONAL_INCLUDE
8#define SIMDJSON_GENERIC_STRING_BUILDER_H
9#include "simdjson/generic/implementation_simdjson_result_base.h"
10#endif // SIMDJSON_CONDITIONAL_INCLUDE
11
12namespace simdjson {
13
14
15#if SIMDJSON_SUPPORTS_CONCEPTS
16
17namespace SIMDJSON_IMPLEMENTATION {
18namespace builder {
19 class string_builder;
20}}
21
22template <typename T, typename = void>
23struct has_custom_serialization : std::false_type {};
24
25inline constexpr struct serialize_tag {
26 template <typename T>
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);
30 }
31
32
33} serialize{};
34template <typename T>
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 {};
38
39template <typename T>
40constexpr bool require_custom_serialization = has_custom_serialization<T>::value;
41#else
42struct has_custom_serialization : std::false_type {};
43#endif // SIMDJSON_SUPPORTS_CONCEPTS
44
45namespace SIMDJSON_IMPLEMENTATION {
46namespace builder {
57public:
58 simdjson_inline string_builder(size_t initial_capacity = DEFAULT_INITIAL_CAPACITY);
59
60 static constexpr size_t DEFAULT_INITIAL_CAPACITY = 1024;
61
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;
71
75 simdjson_inline void append(char c) noexcept;
76
80 simdjson_inline void append_null() noexcept;
81
85 simdjson_inline void clear() noexcept;
86
91 simdjson_inline void escape_and_append(std::string_view input) noexcept;
92
97 simdjson_inline void escape_and_append_with_quotes(std::string_view input) noexcept;
98#if SIMDJSON_SUPPORTS_CONCEPTS
99 template<constevalutil::fixed_string key>
100 simdjson_inline void escape_and_append_with_quotes() noexcept;
101#endif
106 simdjson_inline void escape_and_append_with_quotes(char input) noexcept;
107
112 simdjson_inline void escape_and_append_with_quotes(const char* input) noexcept;
113
118 simdjson_inline void append_raw(const char *c) noexcept;
119
123 simdjson_inline void start_object() noexcept;
124
128 simdjson_inline void end_object() noexcept;
129
133 simdjson_inline void start_array() noexcept;
134
138 simdjson_inline void end_array() noexcept;
139
143 simdjson_inline void append_comma() noexcept;
144
148 simdjson_inline void append_colon() noexcept;
149
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>
159 simdjson_inline void append_key_value(value_type value) noexcept;
160
161 // Support for optional types (std::optional, etc.)
162 template <concepts::optional_type T>
163 requires(!require_custom_serialization<T>)
164 simdjson_inline void append(const T &opt);
165
166 template <typename T>
167 requires(require_custom_serialization<T>)
168 simdjson_inline void append(const T &val);
169
170 // Support for string-like types
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);
175#endif
176#if SIMDJSON_SUPPORTS_RANGES && SIMDJSON_SUPPORTS_CONCEPTS
177 // Support for range-based appending (std::ranges::view, etc.)
178 template <std::ranges::range R>
179requires (!std::is_convertible<R, std::string_view>::value)
180 simdjson_inline void append(const R &range) noexcept;
181#endif
186 simdjson_inline void append_raw(std::string_view input) noexcept;
187
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);
202
210 simdjson_inline operator std::string_view() const noexcept(false) simdjson_lifetime_bound;
211#endif
212
220 simdjson_inline simdjson_result<std::string_view> view() const noexcept;
221
231 simdjson_inline simdjson_result<const char *> c_str() noexcept;
232
236 simdjson_inline bool validate_unicode() const noexcept;
237
242 simdjson_inline size_t size() const noexcept;
243
244private:
250 simdjson_inline bool capacity_check(size_t upcoming_bytes);
251
257 simdjson_inline void grow_buffer(size_t desired_capacity);
258
262 simdjson_inline void set_valid(bool valid) noexcept;
263
264 std::unique_ptr<char[]> buffer{};
265 size_t position{0};
266 size_t capacity{0};
267 bool is_valid{true};
268};
269
270
271
272}
273}
274
275
276#if !SIMDJSON_STATIC_REFLECTION
277// fallback implementation until we have static reflection
278template <class Z>
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) {
281 b.append(z);
282 std::string_view s;
283 auto e = b.view().get(s);
284 if(e) { return e; }
285 return std::string(s);
286}
287template <class Z>
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) {
290 b.append(z);
291 std::string_view sv;
292 auto e = b.view().get(sv);
293 if(e) { return e; }
294 s.assign(sv.data(), sv.size());
295 return simdjson::SUCCESS;
296}
297#endif
298
299#if SIMDJSON_SUPPORTS_CONCEPTS
300#endif // SIMDJSON_SUPPORTS_CONCEPTS
301
302} // namespace simdjson
303
304#endif // SIMDJSON_GENERIC_STRING_BUILDER_H
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.
Definition base.h:8
@ SUCCESS
No error.
Definition error.h:20
The result of a simdjson operation that could fail.
Definition error.h:278