simdjson 4.2.4
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 constexpr void operator()(SIMDJSON_IMPLEMENTATION::builder::string_builder& b, T&& obj) const{
28 return tag_invoke(*this, b, std::forward<T>(obj));
29 }
30
31
32} serialize{};
33template <typename T>
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 {};
37
38template <typename T>
39constexpr bool require_custom_serialization = has_custom_serialization<T>::value;
40#else
41struct has_custom_serialization : std::false_type {};
42#endif // SIMDJSON_SUPPORTS_CONCEPTS
43
44namespace SIMDJSON_IMPLEMENTATION {
45namespace builder {
56public:
57 simdjson_inline string_builder(size_t initial_capacity = DEFAULT_INITIAL_CAPACITY);
58
59 static constexpr size_t DEFAULT_INITIAL_CAPACITY = 1024;
60
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;
70
74 simdjson_inline void append(char c) noexcept;
75
79 simdjson_inline void append_null() noexcept;
80
84 simdjson_inline void clear() noexcept;
85
90 simdjson_inline void escape_and_append(std::string_view input) noexcept;
91
96 simdjson_inline void escape_and_append_with_quotes(std::string_view input) noexcept;
97#if SIMDJSON_SUPPORTS_CONCEPTS
98 template<constevalutil::fixed_string key>
99 simdjson_inline void escape_and_append_with_quotes() noexcept;
100#endif
105 simdjson_inline void escape_and_append_with_quotes(char input) noexcept;
106
111 simdjson_inline void escape_and_append_with_quotes(const char* input) noexcept;
112
117 simdjson_inline void append_raw(const char *c) noexcept;
118
122 simdjson_inline void start_object() noexcept;
123
127 simdjson_inline void end_object() noexcept;
128
132 simdjson_inline void start_array() noexcept;
133
137 simdjson_inline void end_array() noexcept;
138
142 simdjson_inline void append_comma() noexcept;
143
147 simdjson_inline void append_colon() noexcept;
148
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>
158 simdjson_inline void append_key_value(value_type value) noexcept;
159
160 // Support for optional types (std::optional, etc.)
161 template <concepts::optional_type T>
162 requires(!require_custom_serialization<T>)
163 simdjson_inline void append(const T &opt);
164
165 template <typename T>
166 requires(require_custom_serialization<T>)
167 simdjson_inline void append(T &&val);
168
169 // Support for string-like types
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);
174#endif
175#if SIMDJSON_SUPPORTS_RANGES && SIMDJSON_SUPPORTS_CONCEPTS
176 // Support for range-based appending (std::ranges::view, etc.)
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;
180#endif
185 simdjson_inline void append_raw(std::string_view input) noexcept;
186
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);
201
209 simdjson_inline operator std::string_view() const noexcept(false) simdjson_lifetime_bound;
210#endif
211
219 simdjson_inline simdjson_result<std::string_view> view() const noexcept;
220
230 simdjson_inline simdjson_result<const char *> c_str() noexcept;
231
235 simdjson_inline bool validate_unicode() const noexcept;
236
241 simdjson_inline size_t size() const noexcept;
242
243private:
249 simdjson_inline bool capacity_check(size_t upcoming_bytes);
250
256 simdjson_inline void grow_buffer(size_t desired_capacity);
257
261 simdjson_inline void set_valid(bool valid) noexcept;
262
263 std::unique_ptr<char[]> buffer{};
264 size_t position{0};
265 size_t capacity{0};
266 bool is_valid{true};
267};
268
269
270
271}
272}
273
274
275#if !SIMDJSON_STATIC_REFLECTION
276// fallback implementation until we have static reflection
277template <class Z>
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) {
280 b.append(z);
281 std::string_view s;
282 auto e = b.view().get(s);
283 if(e) { return e; }
284 return std::string(s);
285}
286template <class Z>
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) {
289 b.append(z);
290 std::string_view sv;
291 auto e = b.view().get(sv);
292 if(e) { return e; }
293 s.assign(sv.data(), sv.size());
294 return simdjson::SUCCESS;
295}
296#endif
297
298#if SIMDJSON_SUPPORTS_CONCEPTS
299#endif // SIMDJSON_SUPPORTS_CONCEPTS
300
301} // namespace simdjson
302
303#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:280