Skip to content

Commit 056d669

Browse files
authored
Renames a few macros and extends slightly our basic builder (#2422)
* This PR renames a few macros and extends slightly our basic builder * minor tuning
1 parent b434a50 commit 056d669

32 files changed

Lines changed: 2722 additions & 877 deletions

benchmark/from/from_benchmark.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void run_array_benchmarks() {
7070
dummy = dummy + car.year;
7171
}
7272
}));
73-
#if SIMDJSON_SUPPORTS_RANGES
73+
#if SIMDJSON_SUPPORTS_RANGES_FROM
7474
// Benchmark simdjson::from() without parser (EXPERIMENTAL)
7575
pretty_print_array("simdjson::from<Car>() (experimental, no parser)",
7676
json.size(), N, bench([&json, &dummy]() {
@@ -90,7 +90,7 @@ void run_array_benchmarks() {
9090
dummy = dummy + car.year;
9191
}
9292
}));
93-
#endif // SIMDJSON_SUPPORTS_RANGES
93+
#endif // SIMDJSON_SUPPORTS_RANGES_FROM
9494
}
9595

9696
void run_stream_benchmarks() {

doc/basics.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -885,13 +885,13 @@ type:
885885
886886
We may do so by providing additional template definitions to the `ondemand::value` type.
887887
We may start by providing a definition for `std::vector<double>` as follows. Observe
888-
how we guard the code with `#if !SIMDJSON_SUPPORTS_DESERIALIZATION`: that is because the necessary code
888+
how we guard the code with `#if !SIMDJSON_SUPPORTS_CONCEPTS`: that is because the necessary code
889889
is automatically provided by simdjson if C++20 (and concepts) are available.
890890
See [Use `tag_invoke` for custom types](#2-use-tag_invoke-for-custom-types-c20) if you have
891891
C++20 support.
892892
893893
```c++
894-
#if !SIMDJSON_SUPPORTS_DESERIALIZATION
894+
#if !SIMDJSON_SUPPORTS_CONCEPTS
895895
// The code is unnecessary with C++20:
896896
template <>
897897
simdjson_inline simdjson_result<std::vector<double>>
@@ -947,7 +947,7 @@ struct Car {
947947
std::vector<double> tire_pressure;
948948
};
949949
950-
#if !SIMDJSON_SUPPORTS_DESERIALIZATION
950+
#if !SIMDJSON_SUPPORTS_CONCEPTS
951951
// This code is not necessary if you have a C++20 compliant system:
952952
template <>
953953
simdjson_inline simdjson_result<std::vector<double>>
@@ -1048,7 +1048,7 @@ struct Car {
10481048
std::vector<double> tire_pressure;
10491049
};
10501050
1051-
#if !SIMDJSON_SUPPORTS_DESERIALIZATION
1051+
#if !SIMDJSON_SUPPORTS_CONCEPTS
10521052
// This code is not necessary if you have a C++20 compliant system:
10531053
template <>
10541054
simdjson_inline simdjson_result<std::vector<double>>
@@ -1143,7 +1143,7 @@ The first argument is usually a tag type (often an empty struct) that uniquely i
11431143
11441144
If your system supports C++20, we recommend that you adopt the `tag_invoke` approach
11451145
instead to deserialize custom types. It may prove to be considerably simpler. When
1146-
simdjson detects the necessary support, it sets the `SIMDJSON_SUPPORTS_DESERIALIZATION` macro
1146+
simdjson detects the necessary support, it sets the `SIMDJSON_SUPPORTS_CONCEPTS` macro
11471147
to 1, otherwise it is set to 0.
11481148
11491149
Consider a custom class `Car`:

doc/builder.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ bool car_test() {
109109
}
110110
```
111111

112+
113+
114+
112115
The `string_builder` constructor takes an optional parameter which specifies the initial
113116
memory allocation in byte. If you know approximately the size of your JSON output, you can
114117
pass this value as a parameter (e.g., `simdjson::builder::string_builder sb{1233213}`).
@@ -124,6 +127,57 @@ The `string_builder` might throw an exception in case of error when you cast it
124127
125128
In all cases, the `std::string_view` instance depends the corresponding `string_builder` instance.
126129
130+
### C++20
131+
132+
133+
134+
If you have C++20, you can simplify the code, as the `std::vector<double>` is automatically
135+
supported.
136+
137+
```cpp
138+
Car c = {"Toyota", "Corolla", 2017, {30.0,30.2,30.513,30.79}};
139+
simdjson::builder::string_builder sb;
140+
sb.start_object();
141+
sb.append_key_value("make", c.make);
142+
sb.append_comma();
143+
sb.append_key_value("model", c.model);
144+
sb.append_comma();
145+
sb.append_key_value("year", c.year);
146+
sb.append_comma();
147+
sb.append_key_value("tire_pressure", c.tire_pressure);
148+
sb.end_object();
149+
std::string_view p = sb.view();
150+
```
151+
152+
With C++20, you can similarly handle standard containers transparently.
153+
For example, you can serialize `std::map<std::string,T>` types.
154+
155+
```cpp
156+
std::map<std::string,double> c = {{"key1", 1}, {"key2", 1}};
157+
simdjson::builder::string_builder sb;
158+
sb.append(c);
159+
std::string_view p = sb.view();
160+
```
161+
162+
You can also serialize `std::vector<T>` types.
163+
164+
```cpp
165+
std::vector<std::vector<double>> c = {{1.0, 2.0}, {3.0, 4.0}};
166+
simdjson::builder::string_builder sb;
167+
sb.append(c);
168+
std::string_view p = sb.view();
169+
```
170+
171+
You can also skip the creation for the `string_builder` instance in such simple cases.
172+
173+
```cpp
174+
std::vector<std::vector<double>> c = {{1.0, 2.0}, {3.0, 4.0}};
175+
std::string json = simdjson::to_json(c);
176+
```
177+
178+
We do recommend that you create and reuse the `string_builder` instance for performance
179+
reasons.
180+
127181
C++26 static reflection
128182
------------------------
129183

doc/iterate_many.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ The first argument is usually a tag type (often an empty struct) that uniquely i
309309

310310

311311
You can deserialize you own data structures conveniently if your system supports C++20.
312-
When it is the case, the macro `SIMDJSON_SUPPORTS_DESERIALIZATION` will be set to 1 by
312+
When it is the case, the macro `SIMDJSON_SUPPORTS_CONCEPTS` will be set to 1 by
313313
the simdjson library.
314314

315315
Consider a custom class `Car`:

include/simdjson/compiler_check.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,32 @@
8181
#endif
8282
#endif
8383

84+
#if defined(__cpp_lib_ranges) && __cpp_lib_ranges >= 201911L
85+
#include <ranges>
86+
#define SIMDJSON_SUPPORTS_RANGES 1
87+
#else
88+
#define SIMDJSON_SUPPORTS_RANGES 0
89+
#endif
8490

8591
#if defined(__cpp_concepts) && !defined(SIMDJSON_CONCEPT_DISABLED)
8692
#if __cpp_concepts >= 201907L
8793
#include <utility>
88-
#define SIMDJSON_SUPPORTS_DESERIALIZATION 1
94+
#define SIMDJSON_SUPPORTS_CONCEPTS 1
8995
#else
90-
#define SIMDJSON_SUPPORTS_DESERIALIZATION 0
96+
#define SIMDJSON_SUPPORTS_CONCEPTS 0
9197
#endif
9298
#else // defined(__cpp_concepts) && !defined(SIMDJSON_CONCEPT_DISABLED)
93-
#define SIMDJSON_SUPPORTS_DESERIALIZATION 0
99+
#define SIMDJSON_SUPPORTS_CONCEPTS 0
94100
#endif // defined(__cpp_concepts) && !defined(SIMDJSON_CONCEPT_DISABLED)
95101

102+
// copy SIMDJSON_SUPPORTS_CONCEPTS to SIMDJSON_SUPPORTS_DESERIALIZATION.
103+
#if SIMDJSON_SUPPORTS_CONCEPTS
104+
#define SIMDJSON_SUPPORTS_DESERIALIZATION 1
105+
#else
106+
#define SIMDJSON_SUPPORTS_DESERIALIZATION 0
107+
#endif
108+
109+
96110
#if !defined(SIMDJSON_CONSTEVAL)
97111
#if defined(__cpp_consteval) && __cpp_consteval >= 201811L && defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L
98112
#define SIMDJSON_CONSTEVAL 1

include/simdjson/concepts.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef SIMDJSON_CONCEPTS_H
22
#define SIMDJSON_CONCEPTS_H
3-
#if SIMDJSON_SUPPORTS_DESERIALIZATION
3+
#if SIMDJSON_SUPPORTS_CONCEPTS
44

55
#include <concepts>
66
#include <type_traits>
@@ -32,7 +32,9 @@ SIMDJSON_IMPL_CONCEPT(op_append, operator+=)
3232
#undef SIMDJSON_IMPL_CONCEPT
3333
} // namespace details
3434

35-
35+
template <typename T>
36+
concept is_pair = requires { typename T::first_type; typename T::second_type; } &&
37+
std::same_as<T, std::pair<typename T::first_type, typename T::second_type>>;
3638
template <typename T>
3739
concept string_view_like = std::is_convertible_v<T, std::string_view> &&
3840
!std::is_convertible_v<T, const char*>;
@@ -126,5 +128,5 @@ concept optional_type = requires(std::remove_cvref_t<T> obj) {
126128

127129
} // namespace concepts
128130
} // namespace simdjson
129-
#endif // SIMDJSON_SUPPORTS_DESERIALIZATION
131+
#endif // SIMDJSON_SUPPORTS_CONCEPTS
130132
#endif // SIMDJSON_CONCEPTS_H

include/simdjson/convert-inl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#define SIMDJSON_CONVERT_INL_H
44

55
#include "simdjson/convert.h"
6-
#if SIMDJSON_SUPPORTS_DESERIALIZATION
6+
#if SIMDJSON_SUPPORTS_CONCEPTS
77
namespace simdjson {
88
namespace convert {
99
namespace internal {
@@ -119,5 +119,5 @@ inline auto to_adaptor<T>::operator()(ondemand::parser &parser, padded_string_vi
119119
} // namespace internal
120120
} // namespace convert
121121
} // namespace simdjson
122-
#endif // SIMDJSON_SUPPORTS_DESERIALIZATION
122+
#endif // SIMDJSON_SUPPORTS_CONCEPTS
123123
#endif // SIMDJSON_CONVERT_INL_H

include/simdjson/convert.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "simdjson/ondemand.h"
66
#include <optional>
77

8-
#if SIMDJSON_SUPPORTS_DESERIALIZATION
8+
#if SIMDJSON_SUPPORTS_CONCEPTS
99

1010

1111
namespace simdjson {
@@ -107,5 +107,5 @@ static constexpr convert::internal::to_adaptor<> from{};
107107

108108
} // namespace simdjson
109109

110-
#endif // SIMDJSON_SUPPORTS_DESERIALIZATION
110+
#endif // SIMDJSON_SUPPORTS_CONCEPTS
111111
#endif // SIMDJSON_CONVERT_H

include/simdjson/dom/array-inl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,13 @@ inline bool array::iterator::operator>(const array::iterator& other) const noexc
290290

291291
#include "simdjson/dom/element-inl.h"
292292

293-
#if defined(__cpp_lib_ranges)
293+
#if SIMDJSON_SUPPORTS_RANGES
294294
static_assert(std::ranges::view<simdjson::dom::array>);
295295
static_assert(std::ranges::sized_range<simdjson::dom::array>);
296296
#if SIMDJSON_EXCEPTIONS
297297
static_assert(std::ranges::view<simdjson::simdjson_result<simdjson::dom::array>>);
298298
static_assert(std::ranges::sized_range<simdjson::simdjson_result<simdjson::dom::array>>);
299299
#endif // SIMDJSON_EXCEPTIONS
300-
#endif // defined(__cpp_lib_ranges)
300+
#endif // SIMDJSON_SUPPORTS_RANGES
301301

302302
#endif // SIMDJSON_ARRAY_INL_H

include/simdjson/dom/array.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,7 @@ struct simdjson_result<dom::array> : public internal::simdjson_result_base<dom::
206206

207207
} // namespace simdjson
208208

209-
#if defined(__cpp_lib_ranges)
210-
#include <ranges>
211-
209+
#if SIMDJSON_SUPPORTS_RANGES
212210
namespace std {
213211
namespace ranges {
214212
template<>
@@ -219,6 +217,6 @@ inline constexpr bool enable_view<simdjson::simdjson_result<simdjson::dom::array
219217
#endif // SIMDJSON_EXCEPTIONS
220218
} // namespace ranges
221219
} // namespace std
222-
#endif // defined(__cpp_lib_ranges)
220+
#endif // SIMDJSON_SUPPORTS_RANGES
223221

224222
#endif // SIMDJSON_DOM_ARRAY_H

0 commit comments

Comments
 (0)