simdjson 4.4.0
Ridiculously Fast JSON
Loading...
Searching...
No Matches
json_string_builder.h
1#ifndef SIMDJSON_GENERIC_STRING_BUILDER_H
2
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_STRING_BUILDER_H
5#include "simdjson/generic/implementation_simdjson_result_base.h"
6#endif // SIMDJSON_CONDITIONAL_INCLUDE
7
8namespace simdjson {
9
10
11#if SIMDJSON_SUPPORTS_CONCEPTS
12
13namespace SIMDJSON_IMPLEMENTATION {
14namespace builder {
15 class string_builder;
16}}
17
18template <typename T, typename = void>
19struct has_custom_serialization : std::false_type {};
20
21inline constexpr struct serialize_tag {
22 template <typename T>
23 constexpr void operator()(SIMDJSON_IMPLEMENTATION::builder::string_builder& b, T&& obj) const{
24 return tag_invoke(*this, b, std::forward<T>(obj));
25 }
26
27
28} serialize{};
29template <typename T>
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 {};
33
34template <typename T>
35constexpr bool require_custom_serialization = has_custom_serialization<T>::value;
36#else
37struct has_custom_serialization : std::false_type {};
38#endif // SIMDJSON_SUPPORTS_CONCEPTS
39
40namespace SIMDJSON_IMPLEMENTATION {
41namespace builder {
52public:
53 simdjson_inline string_builder(size_t initial_capacity = DEFAULT_INITIAL_CAPACITY);
54
55 static constexpr size_t DEFAULT_INITIAL_CAPACITY = 1024;
56
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;
66
70 simdjson_inline void append(char c) noexcept;
71
75 simdjson_inline void append_null() noexcept;
76
80 simdjson_inline void clear() noexcept;
81
86 simdjson_inline void escape_and_append(std::string_view input) noexcept;
87
92 simdjson_inline void escape_and_append_with_quotes(std::string_view input) noexcept;
93#if SIMDJSON_SUPPORTS_CONCEPTS
94 template<constevalutil::fixed_string key>
95 simdjson_inline void escape_and_append_with_quotes() noexcept;
96#endif
101 simdjson_inline void escape_and_append_with_quotes(char input) noexcept;
102
107 simdjson_inline void escape_and_append_with_quotes(const char* input) noexcept;
108
113 simdjson_inline void append_raw(const char *c) noexcept;
114
118 simdjson_inline void start_object() noexcept;
119
123 simdjson_inline void end_object() noexcept;
124
128 simdjson_inline void start_array() noexcept;
129
133 simdjson_inline void end_array() noexcept;
134
138 simdjson_inline void append_comma() noexcept;
139
143 simdjson_inline void append_colon() noexcept;
144
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>
154 simdjson_inline void append_key_value(value_type value) noexcept;
155
156 // Support for optional types (std::optional, etc.)
157 template <concepts::optional_type T>
158 requires(!require_custom_serialization<T>)
159 simdjson_inline void append(const T &opt);
160
161 template <typename T>
162 requires(require_custom_serialization<T>)
163 simdjson_inline void append(T &&val);
164
165 // Support for string-like types
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);
170#endif
171#if SIMDJSON_SUPPORTS_RANGES && SIMDJSON_SUPPORTS_CONCEPTS
172 // Support for range-based appending (std::ranges::view, etc.)
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;
176#endif
181 simdjson_inline void append_raw(std::string_view input) noexcept;
182
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);
197
205 simdjson_inline operator std::string_view() const noexcept(false) simdjson_lifetime_bound;
206#endif
207
215 simdjson_inline simdjson_result<std::string_view> view() const noexcept;
216
226 simdjson_inline simdjson_result<const char *> c_str() noexcept;
227
231 simdjson_inline bool validate_unicode() const noexcept;
232
237 simdjson_inline size_t size() const noexcept;
238
239private:
245 simdjson_inline bool capacity_check(size_t upcoming_bytes);
246
252 simdjson_inline void grow_buffer(size_t desired_capacity);
253
257 simdjson_inline void set_valid(bool valid) noexcept;
258
259 std::unique_ptr<char[]> buffer{};
260 size_t position{0};
261 size_t capacity{0};
262 bool is_valid{true};
263};
264
265
266
267}
268}
269
270
271#if !SIMDJSON_STATIC_REFLECTION
272// fallback implementation until we have static reflection
273template <class Z>
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) {
276 b.append(z);
277 std::string_view s;
278 auto e = b.view().get(s);
279 if(e) { return e; }
280 return std::string(s);
281}
282template <class Z>
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) {
285 b.append(z);
286 std::string_view sv;
287 auto e = b.view().get(sv);
288 if(e) { return e; }
289 s.assign(sv.data(), sv.size());
290 return simdjson::SUCCESS;
291}
292#endif
293
294#if SIMDJSON_SUPPORTS_CONCEPTS
295#endif // SIMDJSON_SUPPORTS_CONCEPTS
296
297} // namespace simdjson
298
299#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
error_code
All possible errors returned by simdjson.
Definition error.h:19
@ SUCCESS
No error.
Definition error.h:20
The result of a simdjson operation that could fail.
Definition error.h:280