mirror of
https://github.com/simdjson/simdjson
synced 2026-06-08 17:27:07 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b2932d1b8f | |||
| a7811090ef | |||
| 3320885fac | |||
| 786c68b158 | |||
| 703ef54bd9 | |||
| 2526068e2f | |||
| ddc7b8c7dd | |||
| d8f90bdd14 | |||
| e38a4923e5 | |||
| e6dfa2e0ed |
@@ -31,4 +31,4 @@ jobs:
|
||||
echo -e '#include <simdjson.h>\nint main(int argc,char**argv) {simdjson::dom::parser parser;simdjson::dom::element tweets = parser.load(argv[1]); }' > tmp.cpp && c++ -Idestination/include -Ldestination/lib -std=c++17 -Wl,-rpath,destination/lib -o linkandrun tmp.cpp -lsimdjson && ./linkandrun jsonexamples/twitter.json &&
|
||||
mkdir testfindpackage &&
|
||||
cd testfindpackage &&
|
||||
echo -e 'cmake_minimum_required(VERSION 3.1)\nproject(simdjsontester)\nset(CMAKE_CXX_STANDARD 17)\nfind_package(simdjson REQUIRED)'> CMakeLists.txt && mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX:PATH=../destination .. && cmake --build .
|
||||
echo -e 'cmake_minimum_required(VERSION 3.14)\nproject(simdjsontester)\nset(CMAKE_CXX_STANDARD 17)\nfind_package(simdjson REQUIRED)'> CMakeLists.txt && mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX:PATH=../destination .. && cmake --build .
|
||||
|
||||
@@ -31,4 +31,4 @@ jobs:
|
||||
echo -e '#include <simdjson.h>\nint main(int argc,char**argv) {simdjson::dom::parser parser;simdjson::dom::element tweets = parser.load(argv[1]); }' > tmp.cpp && c++ -Idestination/include -Ldestination/lib -std=c++17 -Wl,-rpath,destination/lib -o linkandrun tmp.cpp -lsimdjson && ./linkandrun jsonexamples/twitter.json &&
|
||||
mkdir testfindpackage &&
|
||||
cd testfindpackage &&
|
||||
echo -e 'cmake_minimum_required(VERSION 3.1)\nproject(simdjsontester)\nset(CMAKE_CXX_STANDARD 17)\nfind_package(simdjson REQUIRED)'> CMakeLists.txt && mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX:PATH=../destination .. && cmake --build .
|
||||
echo -e 'cmake_minimum_required(VERSION 3.14)\nproject(simdjsontester)\nset(CMAKE_CXX_STANDARD 17)\nfind_package(simdjson REQUIRED)'> CMakeLists.txt && mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX:PATH=../destination .. && cmake --build .
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.14)
|
||||
project(
|
||||
simdjson
|
||||
# The version number is modified by tools/release.py
|
||||
VERSION 4.0.3
|
||||
VERSION 4.0.6
|
||||
DESCRIPTION "Parsing gigabytes of JSON per second"
|
||||
HOMEPAGE_URL "https://simdjson.org/"
|
||||
LANGUAGES CXX C
|
||||
|
||||
@@ -38,7 +38,7 @@ PROJECT_NAME = simdjson
|
||||
# could be handy for archiving the generated documentation or if some version
|
||||
# control system is used.
|
||||
|
||||
PROJECT_NUMBER = "4.0.3"
|
||||
PROJECT_NUMBER = "4.0.6"
|
||||
|
||||
# Using the PROJECT_BRIEF tag one can provide an optional one line description
|
||||
# for a project that appears at the top of each page and should give viewer a
|
||||
|
||||
@@ -62,6 +62,7 @@ Real-world usage
|
||||
- [WasmEdge](https://wasmedge.org)
|
||||
- [RonDB](https://github.com/logicalclocks/rondb)
|
||||
- [GreptimeDB](https://github.com/GreptimeTeam/greptimedb)
|
||||
- [mamba](https://github.com/mamba-org/mamba)
|
||||
|
||||
|
||||
If you are planning to use simdjson in a product, please work from one of our releases.
|
||||
@@ -120,7 +121,6 @@ Some users may want to browse code along with the compiled assembly. You want to
|
||||
* [C++26 reflection example](https://godbolt.org/z/K3Px64TqK)
|
||||
* [simdjson examples with errors handled through exceptions](https://godbolt.org/z/7G5qE4sr9)
|
||||
* [simdjson examples with errors without exceptions](https://godbolt.org/z/e9dWb9E4v)
|
||||
|
||||
|
||||
Performance results
|
||||
-------------------
|
||||
|
||||
@@ -174,6 +174,10 @@ else()
|
||||
-Werror -Wall -Wextra -Weffc++ -Wsign-compare -Wshadow -Wwrite-strings
|
||||
-Wpointer-arith -Winit-self -Wconversion -Wno-sign-conversion
|
||||
)
|
||||
if(CMAKE_CXX_STANDARD VERSION_GREATER_EQUAL 20)
|
||||
target_compile_options(simdjson-internal-flags INTERFACE -Wctad-maybe-unsupported)
|
||||
endif()
|
||||
|
||||
endif()
|
||||
|
||||
option(SIMDJSON_GLIBCXX_ASSERTIONS "Set _GLIBCXX_ASSERTIONS" OFF)
|
||||
|
||||
@@ -1400,6 +1400,9 @@ maps it to parsing code. We call the default constructor,
|
||||
and then assign values to the public members.
|
||||
|
||||
|
||||
If a key is missing in the JSON document, an error is generated (`NO_SUCH_FIELD`),
|
||||
except if the attribute is of a type like `std::optional` (`simdjson::concepts::optional_type`).
|
||||
|
||||
#### Special cases
|
||||
|
||||
However, there are instances where the construction cannot
|
||||
@@ -1477,6 +1480,12 @@ type without a document instance like so:
|
||||
Car car = simdjson::from(json);
|
||||
```
|
||||
|
||||
You can also use the `simdjson::from` syntax without exceptions, like so:
|
||||
```cpp
|
||||
Car car;
|
||||
simdjson::error_code err = simdjson::from(json_car).get(car);
|
||||
```
|
||||
|
||||
You can also use the `simdjson::from` syntax to iterate over an array.
|
||||
|
||||
```cpp
|
||||
|
||||
+99
-10
@@ -34,12 +34,13 @@ It has the following methods to add content to the string:
|
||||
- `append_null()`: Appends the string "null" to the JSON buffer.
|
||||
- `clear()`: Clears the contents of the JSON buffer, resetting the position to 0 while retaining the allocated capacity.
|
||||
- `escape_and_append(std::string_view input)`: Appends a string view to the JSON buffer after escaping special characters (e.g., quotes, backslashes) as required by JSON.
|
||||
- `escape_and_append_with_quotes(std::string_view input)` Appends a string view surrounded by double quotes (e.g., "input") to the JSON buffer after escaping special characters.
|
||||
Parameters:
|
||||
- `escape_and_append_with_quotes(std::string_view input)` Appends a string view surrounded by double quotes (e.g., "input") to the JSON buffer after escaping special characters. For constant strings, you may also do `escape_and_append_with_quotes<"mystring">()`.
|
||||
- `escape_and_append_with_quotes(char input)`: Appends a single character surrounded by double quotes (e.g., "c") to the JSON buffer after escaping it if necessary.
|
||||
- `append_raw(const char *c)`: Appends a null-terminated C string directly to the JSON buffer without escaping.
|
||||
- `append_raw(std::string_view input)`: Appends a string view directly to the JSON buffer without escaping.
|
||||
- `append_raw(const char *str, size_t len)`: Appends a specified number of characters from a C string directly to the JSON
|
||||
- `append_key_value(key,value)`: Appends a key and a value (`"json":somevalue`)
|
||||
- `append_key_value<"mykey">(value)`: Appends a key and a value (`"json":somevalue`), useful when the key is a compile-time constant (C++20).
|
||||
|
||||
After writing the content, if you have reasons to believe that the content might violate UTF-8 conventions, you can check it as follows:
|
||||
|
||||
@@ -52,7 +53,7 @@ Once you are satisfied, you can recover the string as follows:
|
||||
|
||||
- `operator std::string()`: Converts the JSON buffer to an std::string. (Might throw if an error occurred.)
|
||||
- `operator std::string_view()`: Converts the JSON buffer to an std::string_view. (Might throw if an error occurred.)
|
||||
- `view()`: Returns a view of the written JSON buffer as a `simdjson_result<std::string_view>`.
|
||||
- `view()`: Returns a view of the written JSON buffer as a `simdjson_result<std::string_view>` (C++20).
|
||||
|
||||
The later method (`view()`) is recommended. For performance reasons, we expect you to explicitly call `validate_unicode()` as needed (e.g., prior to calling `view()`).
|
||||
|
||||
@@ -131,20 +132,20 @@ In all cases, the `std::string_view` instance depends the corresponding `string_
|
||||
|
||||
|
||||
|
||||
If you have C++20, you can simplify the code, as the `std::vector<double>` is automatically
|
||||
supported.
|
||||
If you have C++20, you can simplify the code, as the `std::vector<double>` is automatically supported. Further, we can pass the keys (which are compile-time
|
||||
constant) as template parameter (for improved performance).
|
||||
|
||||
```cpp
|
||||
Car c = {"Toyota", "Corolla", 2017, {30.0,30.2,30.513,30.79}};
|
||||
simdjson::builder::string_builder sb;
|
||||
sb.start_object();
|
||||
sb.append_key_value("make", c.make);
|
||||
sb.append_key_value<"make">(c.make);
|
||||
sb.append_comma();
|
||||
sb.append_key_value("model", c.model);
|
||||
sb.append_key_value<"model">(c.model);
|
||||
sb.append_comma();
|
||||
sb.append_key_value("year", c.year);
|
||||
sb.append_key_value<"year">(c.year);
|
||||
sb.append_comma();
|
||||
sb.append_key_value("tire_pressure", c.tire_pressure);
|
||||
sb.append_key_value<"tire_pressure">(c.tire_pressure);
|
||||
sb.end_object();
|
||||
std::string_view p = sb.view();
|
||||
```
|
||||
@@ -175,9 +176,52 @@ std::vector<std::vector<double>> c = {{1.0, 2.0}, {3.0, 4.0}};
|
||||
std::string json = simdjson::to_json(c);
|
||||
```
|
||||
|
||||
We also have an overload for when you want to reuse the same `std::string` instance:
|
||||
|
||||
|
||||
```cpp
|
||||
std::vector<std::vector<double>> c = {{1.0, 2.0}, {3.0, 4.0}};
|
||||
std::string json;
|
||||
auto error = simdjson::to_json(c, json);
|
||||
if(error) { /* there was an error */ }
|
||||
```
|
||||
|
||||
|
||||
We do recommend that you create and reuse the `string_builder` instance for performance
|
||||
reasons.
|
||||
|
||||
You can also add custom serialization functions using a `tag_invoke` function.
|
||||
For example, the following
|
||||
function will allow you to serialize instances of the type `Car`.
|
||||
|
||||
```cpp
|
||||
#include <simdjson>
|
||||
|
||||
struct Car {
|
||||
std::string make;
|
||||
std::string model;
|
||||
int64_t year;
|
||||
std::vector<float> tire_pressure;
|
||||
};
|
||||
|
||||
namespace simdjson {
|
||||
|
||||
template <typename builder_type>
|
||||
void tag_invoke(serialize_tag, builder_type &builder, const Car& car) {
|
||||
builder.start_object();
|
||||
builder.append_key_value("make", car.make);
|
||||
builder.append_comma();
|
||||
builder.append_key_value("model", car.model);
|
||||
builder.append_comma();
|
||||
builder.append_key_value("year", car.year);
|
||||
builder.append_comma();
|
||||
builder.append_key_value("tire_pressure", car.tire_pressure);
|
||||
builder.end_object();
|
||||
}
|
||||
|
||||
} // namespace simdjson
|
||||
```
|
||||
|
||||
C++26 static reflection
|
||||
------------------------
|
||||
|
||||
@@ -239,7 +283,17 @@ with the `simdjson::to_json` template function.
|
||||
If you know the output size, in bytes, of your JSON string, you may
|
||||
pass it as a second parameter (e.g., `simdjson::to_json(c, 31123)`).
|
||||
|
||||
Sometimes you may want to reuse the same `std::string` instance. We
|
||||
have an overload for this purpose:
|
||||
|
||||
```cpp
|
||||
Car c = {"Toyota", "Corolla", 2017, {30.0,30.2,30.513,30.79}};
|
||||
std::string s;
|
||||
auto error = simdjson::to_json(c, s);
|
||||
if(error) { /* there was an error */ }
|
||||
```
|
||||
|
||||
You can then also add a third parameter for the expected output size in bytes.
|
||||
|
||||
### Without `string_buffer` instance but with explicit error handling
|
||||
|
||||
@@ -248,9 +302,44 @@ pattern:
|
||||
|
||||
```cpp
|
||||
std::string json;
|
||||
if(simdjson::to(c).get(json)) {
|
||||
if(simdjson::to_json(c).get(json)) {
|
||||
// there was an error
|
||||
} else {
|
||||
// json contain the serialized JSON
|
||||
}
|
||||
```
|
||||
|
||||
### Customization
|
||||
|
||||
If you want to serialize a value in a custome way, you can do it with a
|
||||
`tag_invoke` specialization like the following example which will map
|
||||
the year attribute to a string.
|
||||
|
||||
|
||||
```cpp
|
||||
#include <simdjson>
|
||||
|
||||
struct Car {
|
||||
std::string make;
|
||||
std::string model;
|
||||
int64_t year;
|
||||
std::vector<float> tire_pressure;
|
||||
};
|
||||
|
||||
namespace simdjson {
|
||||
|
||||
template <typename builder_type>
|
||||
void tag_invoke(serialize_tag, builder_type &builder, const Car& car) {
|
||||
builder.start_object();
|
||||
builder.append_key_value("make", car.make);
|
||||
builder.append_comma();
|
||||
builder.append_key_value("model", car.model);
|
||||
builder.append_comma();
|
||||
builder.append_key_value("year", std::to_string(car.year));
|
||||
builder.append_comma();
|
||||
builder.append_key_value("tire_pressure", car.tire_pressure);
|
||||
builder.end_object();
|
||||
}
|
||||
|
||||
} // namespace simdjson
|
||||
```
|
||||
@@ -122,11 +122,32 @@ concept optional_type = requires(std::remove_cvref_t<T> obj) {
|
||||
} -> std::convertible_to<typename std::remove_cvref_t<T>::value_type>;
|
||||
};
|
||||
{ static_cast<bool>(obj) } -> std::same_as<bool>; // convertible to bool
|
||||
{ obj.reset() } noexcept -> std::same_as<void>;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace concepts
|
||||
|
||||
|
||||
/**
|
||||
* We use tag_invoke as our customization point mechanism.
|
||||
*/
|
||||
template <typename Tag, typename... Args>
|
||||
concept tag_invocable = requires(Tag tag, Args... args) {
|
||||
tag_invoke(std::forward<Tag>(tag), std::forward<Args>(args)...);
|
||||
};
|
||||
|
||||
template <typename Tag, typename... Args>
|
||||
concept nothrow_tag_invocable =
|
||||
tag_invocable<Tag, Args...> && requires(Tag tag, Args... args) {
|
||||
{
|
||||
tag_invoke(std::forward<Tag>(tag), std::forward<Args>(args)...)
|
||||
} noexcept;
|
||||
};
|
||||
|
||||
} // namespace simdjson
|
||||
#endif // SIMDJSON_SUPPORTS_CONCEPTS
|
||||
#endif // SIMDJSON_CONCEPTS_H
|
||||
|
||||
@@ -58,6 +58,12 @@ inline simdjson_result<T> auto_parser<parser_type>::result() noexcept(is_nothrow
|
||||
return m_doc.get<T>();
|
||||
}
|
||||
|
||||
template <typename parser_type>
|
||||
template <typename T>
|
||||
simdjson_warn_unused simdjson_inline error_code auto_parser<parser_type>::get(T &value) && noexcept(is_nothrow_gettable<T>) {
|
||||
return result<T>().get(value);
|
||||
}
|
||||
|
||||
template <typename parser_type>
|
||||
inline simdjson_result<ondemand::array> auto_parser<parser_type>::array() noexcept {
|
||||
return result<ondemand::array>();
|
||||
@@ -73,12 +79,7 @@ inline simdjson_result<ondemand::number> auto_parser<parser_type>::number() noex
|
||||
return result<ondemand::number>();
|
||||
}
|
||||
|
||||
template <typename parser_type>
|
||||
template <typename T>
|
||||
inline auto_parser<parser_type>::operator simdjson_result<T>() noexcept(is_nothrow_gettable<T>) {
|
||||
return result<T>();
|
||||
}
|
||||
|
||||
#if SIMDJSON_EXCEPTIONS
|
||||
template <typename parser_type>
|
||||
template <typename T>
|
||||
inline auto_parser<parser_type>::operator T() noexcept(false) {
|
||||
@@ -87,6 +88,7 @@ inline auto_parser<parser_type>::operator T() noexcept(false) {
|
||||
}
|
||||
return m_doc.get<T>();
|
||||
}
|
||||
#endif // SIMDJSON_EXCEPTIONS
|
||||
|
||||
template <typename parser_type>
|
||||
template <typename T>
|
||||
@@ -109,7 +111,7 @@ inline T to_adaptor<T>::operator()(simdjson_result<ondemand::value> &val) const
|
||||
|
||||
template <typename T>
|
||||
inline auto to_adaptor<T>::operator()(padded_string_view const str) const noexcept {
|
||||
return auto_parser{str};
|
||||
return auto_parser<ondemand::parser *>{str};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -119,7 +121,7 @@ inline auto to_adaptor<T>::operator()(ondemand::parser &parser, padded_string_vi
|
||||
|
||||
template <typename T>
|
||||
inline auto to_adaptor<T>::operator()(std::string str) const noexcept {
|
||||
return auto_parser{pad_with_reserve(str)};
|
||||
return auto_parser<ondemand::parser *>{pad_with_reserve(str)};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
||||
@@ -46,16 +46,19 @@ public:
|
||||
|
||||
template <typename T>
|
||||
simdjson_warn_unused simdjson_inline simdjson_result<T> result() noexcept(is_nothrow_gettable<T>);
|
||||
template <typename T>
|
||||
simdjson_warn_unused simdjson_inline error_code get(T &value) && noexcept(is_nothrow_gettable<T>);
|
||||
|
||||
|
||||
simdjson_warn_unused simdjson_inline simdjson_result<ondemand::array> array() noexcept;
|
||||
simdjson_warn_unused simdjson_inline simdjson_result<ondemand::object> object() noexcept;
|
||||
simdjson_warn_unused simdjson_inline simdjson_result<ondemand::number> number() noexcept;
|
||||
|
||||
template <typename T>
|
||||
simdjson_warn_unused simdjson_inline explicit(false) operator simdjson_result<T>() noexcept(is_nothrow_gettable<T>);
|
||||
|
||||
#if SIMDJSON_EXCEPTIONS
|
||||
template <typename T>
|
||||
simdjson_warn_unused simdjson_inline explicit(false) operator T() noexcept(false);
|
||||
#endif // SIMDJSON_EXCEPTIONS
|
||||
|
||||
template <typename T>
|
||||
simdjson_warn_unused simdjson_inline std::optional<T> optional() noexcept(is_nothrow_gettable<T>);
|
||||
@@ -78,6 +81,8 @@ struct to_adaptor {
|
||||
auto operator()(std::string str) const noexcept;
|
||||
auto operator()(ondemand::parser &parser, std::string str) const noexcept;
|
||||
};
|
||||
// deduction guide
|
||||
auto_parser(padded_string_view const str) -> auto_parser<ondemand::parser*>;
|
||||
} // namespace internal
|
||||
} // namespace convert
|
||||
|
||||
|
||||
@@ -7,55 +7,8 @@
|
||||
#include "simdjson/generic/ondemand/array.h"
|
||||
#endif // SIMDJSON_CONDITIONAL_INCLUDE
|
||||
|
||||
#include <concepts>
|
||||
namespace simdjson {
|
||||
|
||||
namespace tag_invoke_fn_ns {
|
||||
void tag_invoke();
|
||||
|
||||
struct tag_invoke_fn {
|
||||
template <typename Tag, typename... Args>
|
||||
requires requires(Tag tag, Args &&...args) {
|
||||
tag_invoke(std::forward<Tag>(tag), std::forward<Args>(args)...);
|
||||
}
|
||||
constexpr auto operator()(Tag tag, Args &&...args) const
|
||||
noexcept(noexcept(tag_invoke(std::forward<Tag>(tag),
|
||||
std::forward<Args>(args)...)))
|
||||
-> decltype(tag_invoke(std::forward<Tag>(tag),
|
||||
std::forward<Args>(args)...)) {
|
||||
return tag_invoke(std::forward<Tag>(tag), std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
} // namespace tag_invoke_fn_ns
|
||||
|
||||
inline namespace tag_invoke_ns {
|
||||
inline constexpr tag_invoke_fn_ns::tag_invoke_fn tag_invoke = {};
|
||||
} // namespace tag_invoke_ns
|
||||
|
||||
template <typename Tag, typename... Args>
|
||||
concept tag_invocable = requires(Tag tag, Args... args) {
|
||||
tag_invoke(std::forward<Tag>(tag), std::forward<Args>(args)...);
|
||||
};
|
||||
|
||||
template <typename Tag, typename... Args>
|
||||
concept nothrow_tag_invocable =
|
||||
tag_invocable<Tag, Args...> && requires(Tag tag, Args... args) {
|
||||
{
|
||||
tag_invoke(std::forward<Tag>(tag), std::forward<Args>(args)...)
|
||||
} noexcept;
|
||||
};
|
||||
|
||||
template <typename Tag, typename... Args>
|
||||
using tag_invoke_result =
|
||||
std::invoke_result<decltype(tag_invoke), Tag, Args...>;
|
||||
|
||||
template <typename Tag, typename... Args>
|
||||
using tag_invoke_result_t =
|
||||
std::invoke_result_t<decltype(tag_invoke), Tag, Args...>;
|
||||
|
||||
template <auto &Tag> using tag_t = std::decay_t<decltype(Tag)>;
|
||||
|
||||
|
||||
struct deserialize_tag;
|
||||
|
||||
/// These types are deserializable in a built-in way
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
#if SIMDJSON_STATIC_REFLECTION
|
||||
|
||||
#include <charconv>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <meta>
|
||||
#include <memory>
|
||||
@@ -39,7 +38,7 @@ concept container_but_not_string =
|
||||
!std::is_same_v<T, std::string_view> && !std::is_same_v<T, const char *>;
|
||||
|
||||
template <class T>
|
||||
requires(container_but_not_string<T>)
|
||||
requires(container_but_not_string<T> && !require_custom_serialization<T>)
|
||||
constexpr void atom(string_builder &b, const T &t) {
|
||||
if (t.size() == 0) {
|
||||
b.append_raw("[]");
|
||||
@@ -64,6 +63,7 @@ constexpr void atom(string_builder &b, const T &t) {
|
||||
}
|
||||
|
||||
template <concepts::string_view_keyed_map T>
|
||||
requires(!require_custom_serialization<T>)
|
||||
constexpr void atom(string_builder &b, const T &m) {
|
||||
if (m.empty()) {
|
||||
b.append_raw("{}");
|
||||
@@ -91,20 +91,6 @@ constexpr void atom(string_builder &b, const number_type t) {
|
||||
b.append(t);
|
||||
}
|
||||
|
||||
// Support for std::chrono::duration - serialize as milliseconds
|
||||
template <typename Rep, typename Period>
|
||||
void atom(string_builder &b, const std::chrono::duration<Rep, Period> &duration) {
|
||||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
||||
b.append(ms);
|
||||
}
|
||||
|
||||
// Support for std::chrono::time_point - serialize as milliseconds since epoch
|
||||
template <typename Clock, typename Duration>
|
||||
void atom(string_builder &b, const std::chrono::time_point<Clock, Duration> &time_point) {
|
||||
auto ms_since_epoch = std::chrono::duration_cast<std::chrono::milliseconds>(time_point.time_since_epoch()).count();
|
||||
b.append(ms_since_epoch);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
requires(std::is_class_v<T> && !container_but_not_string<T> &&
|
||||
!concepts::string_view_keyed_map<T> &&
|
||||
@@ -114,7 +100,7 @@ template <class T>
|
||||
!std::is_same_v<T, std::string> &&
|
||||
!std::is_same_v<T, std::string_view> &&
|
||||
!std::is_same_v<T, const char*> &&
|
||||
!std::is_same_v<T, char>)
|
||||
!std::is_same_v<T, char> && !require_custom_serialization<T>)
|
||||
constexpr void atom(string_builder &b, const T &t) {
|
||||
int i = 0;
|
||||
b.append('{');
|
||||
@@ -132,6 +118,7 @@ constexpr void atom(string_builder &b, const T &t) {
|
||||
|
||||
// Support for optional types (std::optional, etc.)
|
||||
template <concepts::optional_type T>
|
||||
requires(!require_custom_serialization<T>)
|
||||
constexpr void atom(string_builder &b, const T &opt) {
|
||||
if (opt) {
|
||||
atom(b, opt.value());
|
||||
@@ -142,6 +129,7 @@ constexpr void atom(string_builder &b, const T &opt) {
|
||||
|
||||
// Support for smart pointers (std::unique_ptr, std::shared_ptr, etc.)
|
||||
template <concepts::smart_pointer T>
|
||||
requires(!require_custom_serialization<T>)
|
||||
constexpr void atom(string_builder &b, const T &ptr) {
|
||||
if (ptr) {
|
||||
atom(b, *ptr);
|
||||
@@ -152,7 +140,7 @@ constexpr void atom(string_builder &b, const T &ptr) {
|
||||
|
||||
// Support for enums - serialize as string representation using expand approach from P2996R12
|
||||
template <typename T>
|
||||
requires(std::is_enum_v<T>)
|
||||
requires(std::is_enum_v<T> && !require_custom_serialization<T>)
|
||||
void atom(string_builder &b, const T &e) {
|
||||
#if SIMDJSON_STATIC_REFLECTION
|
||||
constexpr auto enumerators = std::define_static_array(std::meta::enumerators_of(^^T));
|
||||
@@ -176,7 +164,7 @@ template <concepts::appendable_containers T>
|
||||
requires(!container_but_not_string<T> && !concepts::string_view_keyed_map<T> &&
|
||||
!concepts::optional_type<T> && !concepts::smart_pointer<T> &&
|
||||
!std::is_same_v<T, std::string> &&
|
||||
!std::is_same_v<T, std::string_view> && !std::is_same_v<T, const char*>)
|
||||
!std::is_same_v<T, std::string_view> && !std::is_same_v<T, const char*> && !require_custom_serialization<T>)
|
||||
constexpr void atom(string_builder &b, const T &container) {
|
||||
if (container.empty()) {
|
||||
b.append_raw("[]");
|
||||
@@ -211,11 +199,13 @@ void append(string_builder &b, const T &t) {
|
||||
}
|
||||
|
||||
template <concepts::optional_type T>
|
||||
requires(!require_custom_serialization<T>)
|
||||
void append(string_builder &b, const T &t) {
|
||||
atom(b, t);
|
||||
}
|
||||
|
||||
template <concepts::smart_pointer T>
|
||||
requires(!require_custom_serialization<T>)
|
||||
void append(string_builder &b, const T &t) {
|
||||
atom(b, t);
|
||||
}
|
||||
@@ -224,12 +214,13 @@ template <concepts::appendable_containers T>
|
||||
requires(!container_but_not_string<T> && !concepts::string_view_keyed_map<T> &&
|
||||
!concepts::optional_type<T> && !concepts::smart_pointer<T> &&
|
||||
!std::is_same_v<T, std::string> &&
|
||||
!std::is_same_v<T, std::string_view> && !std::is_same_v<T, const char*>)
|
||||
!std::is_same_v<T, std::string_view> && !std::is_same_v<T, const char*> && !require_custom_serialization<T>)
|
||||
void append(string_builder &b, const T &t) {
|
||||
atom(b, t);
|
||||
}
|
||||
|
||||
template <concepts::string_view_keyed_map T>
|
||||
requires(!require_custom_serialization<T>)
|
||||
void append(string_builder &b, const T &t) {
|
||||
atom(b, t);
|
||||
}
|
||||
@@ -244,7 +235,7 @@ template <class Z>
|
||||
!std::is_same_v<Z, std::string> &&
|
||||
!std::is_same_v<Z, std::string_view> &&
|
||||
!std::is_same_v<Z, const char*> &&
|
||||
!std::is_same_v<Z, char>)
|
||||
!std::is_same_v<Z, char> && !require_custom_serialization<Z>)
|
||||
void append(string_builder &b, const Z &z) {
|
||||
int i = 0;
|
||||
b.append('{');
|
||||
@@ -262,7 +253,7 @@ void append(string_builder &b, const Z &z) {
|
||||
|
||||
// works for container
|
||||
template <class Z>
|
||||
requires(container_but_not_string<Z>)
|
||||
requires(container_but_not_string<Z> && !require_custom_serialization<Z>)
|
||||
void append(string_builder &b, const Z &z) {
|
||||
if (z.size() == 0) {
|
||||
b.append_raw("[]");
|
||||
@@ -278,7 +269,14 @@ void append(string_builder &b, const Z &z) {
|
||||
}
|
||||
|
||||
template <class Z>
|
||||
simdjson_result<std::string> to_json_string(const Z &z, size_t initial_capacity = 1024) {
|
||||
requires (require_custom_serialization<Z>)
|
||||
void append(string_builder &b, const Z &z) {
|
||||
b.append(z);
|
||||
}
|
||||
|
||||
|
||||
template <class Z>
|
||||
simdjson_warn_unused simdjson_result<std::string> to_json_string(const Z &z, size_t initial_capacity = string_builder::DEFAULT_INITIAL_CAPACITY) {
|
||||
string_builder b(initial_capacity);
|
||||
append(b, z);
|
||||
std::string_view s;
|
||||
@@ -287,8 +285,8 @@ simdjson_result<std::string> to_json_string(const Z &z, size_t initial_capacity
|
||||
}
|
||||
|
||||
template <class Z>
|
||||
simdjson_error to_json(const Z &z, std::string &s) {
|
||||
string_builder b;
|
||||
simdjson_warn_unused simdjson_error to_json(const Z &z, std::string &s, size_t initial_capacity = string_builder::DEFAULT_INITIAL_CAPACITY) {
|
||||
string_builder b(initial_capacity);
|
||||
append(b, z);
|
||||
std::string_view view;
|
||||
if(auto e = b.view().get(view); e) { return e; }
|
||||
@@ -305,9 +303,13 @@ string_builder& operator<<(string_builder& b, const Z& z) {
|
||||
} // namespace SIMDJSON_IMPLEMENTATION
|
||||
// Alias the function template to 'to' in the global namespace
|
||||
template <class Z>
|
||||
simdjson_result<std::string> to_json(const Z &z, size_t initial_capacity = 1024) {
|
||||
simdjson_warn_unused simdjson_result<std::string> to_json(const Z &z, size_t initial_capacity = SIMDJSON_IMPLEMENTATION::builder::string_builder::DEFAULT_INITIAL_CAPACITY) {
|
||||
return SIMDJSON_IMPLEMENTATION::builder::to_json_string(z, initial_capacity);
|
||||
}
|
||||
template <class Z>
|
||||
simdjson_warn_unused simdjson_error to_json(const Z &z, std::string &s, size_t initial_capacity = SIMDJSON_IMPLEMENTATION::builder::string_builder::DEFAULT_INITIAL_CAPACITY) {
|
||||
return SIMDJSON_IMPLEMENTATION::builder::to_json(z, s, initial_capacity);
|
||||
}
|
||||
} // namespace simdjson
|
||||
|
||||
#endif // SIMDJSON_STATIC_REFLECTION
|
||||
|
||||
@@ -360,6 +360,7 @@ static const char decimal_table[200] = {
|
||||
};
|
||||
} // namespace internal
|
||||
|
||||
|
||||
template <typename number_type, typename>
|
||||
simdjson_inline void string_builder::append(number_type v) noexcept {
|
||||
static_assert(std::is_same<number_type, bool>::value ||
|
||||
@@ -475,6 +476,12 @@ simdjson_inline void string_builder::escape_and_append_with_quotes(const char* i
|
||||
std::string_view cinput(input);
|
||||
escape_and_append_with_quotes(cinput);
|
||||
}
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
template<internal::fixed_string key>
|
||||
simdjson_inline void string_builder::escape_and_append_with_quotes() noexcept {
|
||||
escape_and_append_with_quotes(internal::string_constant<key>::value);
|
||||
}
|
||||
#endif
|
||||
|
||||
simdjson_inline void string_builder::append_raw(const char *c) noexcept {
|
||||
size_t len = std::strlen(c);
|
||||
@@ -499,6 +506,7 @@ simdjson_inline void string_builder::append_raw(const char *str,
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
// Support for optional types (std::optional, etc.)
|
||||
template <concepts::optional_type T>
|
||||
requires(!require_custom_serialization<T>)
|
||||
simdjson_inline void string_builder::append(const T &opt) {
|
||||
if (opt) {
|
||||
append(*opt);
|
||||
@@ -506,9 +514,17 @@ simdjson_inline void string_builder::append(const T &opt) {
|
||||
append_null();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
requires(require_custom_serialization<T>)
|
||||
simdjson_inline void string_builder::append(const T &val) {
|
||||
serialize(*this, val);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires(std::is_convertible<T, std::string_view>::value ||
|
||||
std::is_same<T, const char*>::value )
|
||||
std::is_same<T, const char*>::value)
|
||||
simdjson_inline void string_builder::append(const T &value) {
|
||||
escape_and_append_with_quotes(value);
|
||||
}
|
||||
@@ -654,6 +670,25 @@ simdjson_inline void string_builder::append_key_value(key_type key, value_type v
|
||||
}
|
||||
}
|
||||
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
template<internal::fixed_string key, typename value_type>
|
||||
simdjson_inline void string_builder::append_key_value(value_type value) noexcept {
|
||||
escape_and_append_with_quotes<key>();
|
||||
append_colon();
|
||||
SIMDJSON_IF_CONSTEXPR(std::is_same<value_type, std::nullptr_t>::value) {
|
||||
append_null();
|
||||
} else SIMDJSON_IF_CONSTEXPR(std::is_same<value_type, char>::value) {
|
||||
escape_and_append_with_quotes(value);
|
||||
} else SIMDJSON_IF_CONSTEXPR(std::is_convertible<value_type, std::string_view>::value) {
|
||||
escape_and_append_with_quotes(value);
|
||||
} else SIMDJSON_IF_CONSTEXPR(std::is_same<value_type, const char*>::value) {
|
||||
escape_and_append_with_quotes(value);
|
||||
} else {
|
||||
append(value);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace builder
|
||||
} // namespace SIMDJSON_IMPLEMENTATION
|
||||
} // namespace simdjson
|
||||
|
||||
@@ -10,9 +10,62 @@
|
||||
#endif // SIMDJSON_CONDITIONAL_INCLUDE
|
||||
|
||||
namespace simdjson {
|
||||
|
||||
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
|
||||
namespace SIMDJSON_IMPLEMENTATION {
|
||||
namespace builder {
|
||||
class string_builder;
|
||||
}}
|
||||
|
||||
template <typename T, typename = void>
|
||||
struct has_custom_serialization : std::false_type {};
|
||||
|
||||
inline constexpr struct serialize_tag {
|
||||
template <typename T>
|
||||
requires custom_deserializable<T>
|
||||
constexpr void operator()(SIMDJSON_IMPLEMENTATION::builder::string_builder& b, T& obj) const{
|
||||
return tag_invoke(*this, b, obj);
|
||||
}
|
||||
|
||||
|
||||
} serialize{};
|
||||
template <typename T>
|
||||
struct has_custom_serialization<T, std::void_t<
|
||||
decltype(tag_invoke(serialize, std::declval<SIMDJSON_IMPLEMENTATION::builder::string_builder&>(), std::declval<T&>()))
|
||||
>> : std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
constexpr bool require_custom_serialization = has_custom_serialization<T>::value;
|
||||
#else
|
||||
struct has_custom_serialization : std::false_type {};
|
||||
#endif // SIMDJSON_SUPPORTS_CONCEPTS
|
||||
|
||||
namespace SIMDJSON_IMPLEMENTATION {
|
||||
namespace builder {
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
// Helper to create string constants
|
||||
namespace internal {
|
||||
template <std::size_t N>
|
||||
struct fixed_string {
|
||||
constexpr fixed_string(const char (&str)[N]) {
|
||||
for (std::size_t i = 0; i < N; ++i) {
|
||||
data[i] = str[i];
|
||||
}
|
||||
}
|
||||
char data[N];
|
||||
constexpr std::string_view view() const { return {data, N - 1}; }
|
||||
};
|
||||
template <std::size_t N>
|
||||
fixed_string(const char (&)[N]) -> fixed_string<N>;
|
||||
|
||||
template <fixed_string str>
|
||||
struct string_constant {
|
||||
static constexpr std::string_view value = str.view();
|
||||
};
|
||||
} // namespace internal
|
||||
#endif // SIMDJSON_SUPPORTS_CONCEPTS
|
||||
/**
|
||||
* A builder for JSON strings representing documents. This is a low-level
|
||||
* builder that is not meant to be used directly by end-users. Though it
|
||||
@@ -24,7 +77,9 @@ namespace builder {
|
||||
*/
|
||||
class string_builder {
|
||||
public:
|
||||
simdjson_inline string_builder(size_t initial_capacity = 1024);
|
||||
simdjson_inline string_builder(size_t initial_capacity = DEFAULT_INITIAL_CAPACITY);
|
||||
|
||||
static constexpr size_t DEFAULT_INITIAL_CAPACITY = 1024;
|
||||
|
||||
/**
|
||||
* Append number (includes Booleans). Booleans are mapped to the strings
|
||||
@@ -33,7 +88,7 @@ public:
|
||||
* represents the number.
|
||||
*/
|
||||
template<typename number_type,
|
||||
typename = typename std::enable_if<std::is_arithmetic<number_type>::value>::type>
|
||||
typename = typename std::enable_if<std::is_arithmetic<number_type>::value>::type>
|
||||
simdjson_inline void append(number_type v) noexcept;
|
||||
|
||||
/**
|
||||
@@ -62,7 +117,10 @@ public:
|
||||
* There is no UTF-8 validation.
|
||||
*/
|
||||
simdjson_inline void escape_and_append_with_quotes(std::string_view input) noexcept;
|
||||
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
template<internal::fixed_string key>
|
||||
simdjson_inline void escape_and_append_with_quotes() noexcept;
|
||||
#endif
|
||||
/**
|
||||
* Append the character surrounded by double quotes, after escaping it.
|
||||
* There is no UTF-8 validation.
|
||||
@@ -116,13 +174,21 @@ public:
|
||||
* The key is escaped and surrounded by double quotes.
|
||||
* The value is escaped if it is a string.
|
||||
*/
|
||||
template<typename key_type, typename value_type>
|
||||
template<typename key_type, typename value_type>
|
||||
simdjson_inline void append_key_value(key_type key, value_type value) noexcept;
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
template<internal::fixed_string key, typename value_type>
|
||||
simdjson_inline void append_key_value(value_type value) noexcept;
|
||||
|
||||
// Support for optional types (std::optional, etc.)
|
||||
template <concepts::optional_type T>
|
||||
requires(!require_custom_serialization<T>)
|
||||
simdjson_inline void append(const T &opt);
|
||||
|
||||
template <typename T>
|
||||
requires(require_custom_serialization<T>)
|
||||
simdjson_inline void append(const T &val);
|
||||
|
||||
// Support for string-like types
|
||||
template <typename T>
|
||||
requires(std::is_convertible<T, std::string_view>::value ||
|
||||
@@ -232,7 +298,7 @@ private:
|
||||
#if !SIMDJSON_STATIC_REFLECTION
|
||||
// fallback implementation until we have static reflection
|
||||
template <class Z>
|
||||
simdjson_result<std::string> to_json(const Z &z, size_t initial_capacity = 1024) {
|
||||
simdjson_warn_unused simdjson_result<std::string> to_json(const Z &z, size_t initial_capacity = simdjson::SIMDJSON_IMPLEMENTATION::builder::string_builder::DEFAULT_INITIAL_CAPACITY) {
|
||||
simdjson::SIMDJSON_IMPLEMENTATION::builder::string_builder b(initial_capacity);
|
||||
b.append(z);
|
||||
std::string_view s;
|
||||
@@ -240,8 +306,20 @@ simdjson_result<std::string> to_json(const Z &z, size_t initial_capacity = 1024)
|
||||
if(e) { return e; }
|
||||
return std::string(s);
|
||||
}
|
||||
template <class Z>
|
||||
simdjson_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) {
|
||||
simdjson::SIMDJSON_IMPLEMENTATION::builder::string_builder b(initial_capacity);
|
||||
b.append(z);
|
||||
std::string_view sv;
|
||||
auto e = b.view().get(sv);
|
||||
if(e) { return e; }
|
||||
s.assign(sv.data(), sv.size());
|
||||
return simdjson::SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
#endif // SIMDJSON_SUPPORTS_CONCEPTS
|
||||
|
||||
} // namespace simdjson
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#include "simdjson/generic/ondemand/base.h"
|
||||
#endif // SIMDJSON_CONDITIONAL_INCLUDE
|
||||
|
||||
#include <chrono>
|
||||
#include <concepts>
|
||||
#include <limits>
|
||||
#if SIMDJSON_STATIC_REFLECTION
|
||||
@@ -17,15 +16,12 @@
|
||||
#endif
|
||||
|
||||
namespace simdjson {
|
||||
template <typename T>
|
||||
constexpr bool require_custom_serialization = false;
|
||||
|
||||
//////////////////////////////
|
||||
// Number deserialization
|
||||
//////////////////////////////
|
||||
|
||||
template <std::unsigned_integral T>
|
||||
requires(!require_custom_serialization<T>)
|
||||
error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept {
|
||||
using limits = std::numeric_limits<T>;
|
||||
|
||||
@@ -39,7 +35,6 @@ error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept {
|
||||
}
|
||||
|
||||
template <std::floating_point T>
|
||||
requires(!require_custom_serialization<T>)
|
||||
error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept {
|
||||
double x;
|
||||
SIMDJSON_TRY(val.get_double().get(x));
|
||||
@@ -47,27 +42,7 @@ error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
// Support for std::chrono::duration - deserialize from milliseconds
|
||||
template <typename Rep, typename Period, typename ValT>
|
||||
error_code tag_invoke(deserialize_tag, ValT &val, std::chrono::duration<Rep, Period> &out) noexcept {
|
||||
int64_t ms;
|
||||
SIMDJSON_TRY(val.get_int64().get(ms));
|
||||
out = std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(std::chrono::milliseconds(ms));
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
// Support for std::chrono::time_point - deserialize from milliseconds since epoch
|
||||
template <typename Clock, typename Duration, typename ValT>
|
||||
error_code tag_invoke(deserialize_tag, ValT &val, std::chrono::time_point<Clock, Duration> &out) noexcept {
|
||||
int64_t ms_since_epoch;
|
||||
SIMDJSON_TRY(val.get_int64().get(ms_since_epoch));
|
||||
auto duration = std::chrono::milliseconds(ms_since_epoch);
|
||||
out = std::chrono::time_point<Clock, Duration>(std::chrono::duration_cast<Duration>(duration));
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
template <std::signed_integral T>
|
||||
requires(!require_custom_serialization<T>)
|
||||
error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept {
|
||||
using limits = std::numeric_limits<T>;
|
||||
|
||||
@@ -97,7 +72,6 @@ error_code tag_invoke(deserialize_tag, auto &val, char &out) noexcept {
|
||||
|
||||
// any string-like type (can be constructed from std::string_view)
|
||||
template <concepts::constructible_from_string_view T, typename ValT>
|
||||
requires(!require_custom_serialization<T>)
|
||||
error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(std::is_nothrow_constructible_v<T, std::string_view>) {
|
||||
std::string_view str;
|
||||
SIMDJSON_TRY(val.get_string().get(str));
|
||||
@@ -114,7 +88,6 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(std::is_nothr
|
||||
* doc.get<std::vector<int>>().
|
||||
*/
|
||||
template <concepts::appendable_containers T, typename ValT>
|
||||
requires(!require_custom_serialization<T>)
|
||||
error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) {
|
||||
using value_type = typename std::remove_cvref_t<T>::value_type;
|
||||
static_assert(
|
||||
@@ -160,7 +133,6 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) {
|
||||
* string-keyed types.
|
||||
*/
|
||||
template <concepts::string_view_keyed_map T, typename ValT>
|
||||
requires(!require_custom_serialization<T>)
|
||||
error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) {
|
||||
using value_type = typename std::remove_cvref_t<T>::mapped_type;
|
||||
static_assert(
|
||||
@@ -242,7 +214,6 @@ error_code tag_invoke(deserialize_tag, SIMDJSON_IMPLEMENTATION::ondemand::docume
|
||||
* @return status of the conversion
|
||||
*/
|
||||
template <concepts::smart_pointer T, typename ValT>
|
||||
requires(!require_custom_serialization<T>)
|
||||
error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(nothrow_deserializable<typename std::remove_cvref_t<T>::element_type, ValT>) {
|
||||
using element_type = typename std::remove_cvref_t<T>::element_type;
|
||||
|
||||
@@ -268,7 +239,6 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(nothrow_deser
|
||||
* This CPO (Customization Point Object) will help deserialize into optional types.
|
||||
*/
|
||||
template <concepts::optional_type T>
|
||||
requires(!require_custom_serialization<T>)
|
||||
error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deserializable<typename std::remove_cvref_t<T>::value_type, decltype(val)>) {
|
||||
using value_type = typename std::remove_cvref_t<T>::value_type;
|
||||
|
||||
@@ -294,7 +264,7 @@ error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deser
|
||||
template <typename T>
|
||||
constexpr bool user_defined_type = (std::is_class_v<T>
|
||||
&& !std::is_same_v<T, std::string> && !std::is_same_v<T, std::string_view> && !concepts::optional_type<T> &&
|
||||
!concepts::appendable_containers<T> && !require_custom_serialization<T>);
|
||||
!concepts::appendable_containers<T>);
|
||||
|
||||
|
||||
template <typename T, typename ValT>
|
||||
@@ -306,18 +276,26 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept {
|
||||
} else {
|
||||
SIMDJSON_TRY(val.get_object().get(obj));
|
||||
}
|
||||
error_code e = simdjson::SUCCESS;
|
||||
template for (constexpr auto mem : std::define_static_array(std::meta::nonstatic_data_members_of(^^T, std::meta::access_context::unchecked()))) {
|
||||
if constexpr (!std::meta::is_const(mem) && std::meta::is_public(mem)) {
|
||||
constexpr std::string_view key = std::define_static_string(std::meta::identifier_of(mem));
|
||||
// Note: removed static assert as optional types are now handled generically
|
||||
// as long we are succesful or the field is not found, we continue
|
||||
if(e == simdjson::SUCCESS || e == simdjson::NO_SUCH_FIELD) {
|
||||
e = obj[key].get(out.[:mem:]);
|
||||
if constexpr (concepts::optional_type<decltype(out.[:mem:])>) {
|
||||
// for optional members, it's ok if the key is missing
|
||||
auto error = obj[key].get(out.[:mem:]);
|
||||
if (error && error != NO_SUCH_FIELD) {
|
||||
if(error == NO_SUCH_FIELD) {
|
||||
out.[:mem:].reset();
|
||||
continue;
|
||||
}
|
||||
return error;
|
||||
}
|
||||
} else {
|
||||
// for non-optional members, the key must be present
|
||||
SIMDJSON_TRY(obj[key].get(out.[:mem:]));
|
||||
}
|
||||
}
|
||||
};
|
||||
return e;
|
||||
return simdjson::SUCCESS;
|
||||
}
|
||||
|
||||
// Support for enum deserialization - deserialize from string representation using expand approach from P2996R12
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#define SIMDJSON_SIMDJSON_VERSION_H
|
||||
|
||||
/** The version of simdjson being used (major.minor.revision) */
|
||||
#define SIMDJSON_VERSION "4.0.3"
|
||||
#define SIMDJSON_VERSION "4.0.6"
|
||||
|
||||
namespace simdjson {
|
||||
enum {
|
||||
@@ -19,7 +19,7 @@ enum {
|
||||
/**
|
||||
* The revision (major.minor.REVISION) of simdjson being used.
|
||||
*/
|
||||
SIMDJSON_VERSION_REVISION = 3
|
||||
SIMDJSON_VERSION_REVISION = 6
|
||||
};
|
||||
} // namespace simdjson
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* auto-generated on 2025-09-15 19:47:29 -0600. version 4.0.3 Do not edit! */
|
||||
/* auto-generated on 2025-09-20 22:23:09 -0600. version 4.0.6 Do not edit! */
|
||||
/* including simdjson.cpp: */
|
||||
/* begin file simdjson.cpp */
|
||||
#define SIMDJSON_SRC_SIMDJSON_CPP
|
||||
@@ -3009,11 +3009,32 @@ concept optional_type = requires(std::remove_cvref_t<T> obj) {
|
||||
} -> std::convertible_to<typename std::remove_cvref_t<T>::value_type>;
|
||||
};
|
||||
{ static_cast<bool>(obj) } -> std::same_as<bool>; // convertible to bool
|
||||
{ obj.reset() } noexcept -> std::same_as<void>;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace concepts
|
||||
|
||||
|
||||
/**
|
||||
* We use tag_invoke as our customization point mechanism.
|
||||
*/
|
||||
template <typename Tag, typename... Args>
|
||||
concept tag_invocable = requires(Tag tag, Args... args) {
|
||||
tag_invoke(std::forward<Tag>(tag), std::forward<Args>(args)...);
|
||||
};
|
||||
|
||||
template <typename Tag, typename... Args>
|
||||
concept nothrow_tag_invocable =
|
||||
tag_invocable<Tag, Args...> && requires(Tag tag, Args... args) {
|
||||
{
|
||||
tag_invoke(std::forward<Tag>(tag), std::forward<Args>(args)...)
|
||||
} noexcept;
|
||||
};
|
||||
|
||||
} // namespace simdjson
|
||||
#endif // SIMDJSON_SUPPORTS_CONCEPTS
|
||||
#endif // SIMDJSON_CONCEPTS_H
|
||||
|
||||
+1337
-661
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -1,7 +1,8 @@
|
||||
# All remaining tests link with simdjson proper
|
||||
include_directories(..)
|
||||
add_cpp_test(builder_string_builder_tests LABELS ondemand acceptance per_implementation)
|
||||
add_cpp_test(builder_string_builder_tests LABELS ondemand acceptance per_implementation)
|
||||
if(SIMDJSON_STATIC_REFLECTION)
|
||||
add_cpp_test(static_reflection_custom_tests LABELS ondemand acceptance per_implementation)
|
||||
add_cpp_test(static_reflection_builder_tests LABELS ondemand acceptance per_implementation)
|
||||
add_cpp_test(static_reflection_comprehensive_tests LABELS ondemand acceptance per_implementation)
|
||||
add_cpp_test(static_reflection_edge_cases_tests LABELS ondemand acceptance per_implementation)
|
||||
|
||||
@@ -331,8 +331,6 @@ namespace builder_tests {
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void serialize_car(const Car& car, simdjson::builder::string_builder& builder) {
|
||||
// start of JSON
|
||||
builder.start_object();
|
||||
@@ -369,7 +367,43 @@ namespace builder_tests {
|
||||
builder.end_object();
|
||||
}
|
||||
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
void serialize_car_template(const Car& car, simdjson::builder::string_builder& builder) {
|
||||
// start of JSON
|
||||
builder.start_object();
|
||||
|
||||
// "make"
|
||||
builder.append_key_value<"make">(car.make);
|
||||
builder.append_comma();
|
||||
|
||||
// "model"
|
||||
builder.append_key_value<"model">(car.model);
|
||||
|
||||
builder.append_comma();
|
||||
|
||||
// "year"
|
||||
builder.append_key_value<"year">(car.year);
|
||||
|
||||
builder.append_comma();
|
||||
|
||||
// "tire_pressure"
|
||||
builder.escape_and_append_with_quotes<"tire_pressure">();
|
||||
builder.append_colon();
|
||||
builder.start_array();
|
||||
// vector tire_pressure
|
||||
for (size_t i = 0; i < car.tire_pressure.size(); ++i) {
|
||||
builder.append(car.tire_pressure[i]);
|
||||
if (i < car.tire_pressure.size() - 1) {
|
||||
builder.append_comma();
|
||||
}
|
||||
}
|
||||
// end of array
|
||||
builder.end_array();
|
||||
|
||||
// end of object
|
||||
builder.end_object();
|
||||
}
|
||||
#endif
|
||||
bool car_test() {
|
||||
TEST_START();
|
||||
simdjson::builder::string_builder sb;
|
||||
@@ -381,7 +415,18 @@ namespace builder_tests {
|
||||
ASSERT_EQUAL(p, "{\"make\":\"Toyota\",\"model\":\"Corolla\",\"year\":2017,\"tire_pressure\":[30.0,30.2,30.513,30.79]}");
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
bool car_test_template() {
|
||||
TEST_START();
|
||||
simdjson::builder::string_builder sb;
|
||||
Car c = {"Toyota", "Corolla", 2017, {30.0,30.2,30.513,30.79}};
|
||||
serialize_car_template(c, sb);
|
||||
std::string_view p;
|
||||
auto result = sb.view().get(p);
|
||||
ASSERT_SUCCESS(result);
|
||||
ASSERT_EQUAL(p, "{\"make\":\"Toyota\",\"model\":\"Corolla\",\"year\":2017,\"tire_pressure\":[30.0,30.2,30.513,30.79]}");
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
bool serialize_optional() {
|
||||
TEST_START();
|
||||
simdjson::builder::string_builder sb;
|
||||
@@ -399,7 +444,7 @@ namespace builder_tests {
|
||||
ASSERT_EQUAL(p, "null");
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if SIMDJSON_SUPPORTS_RANGES && SIMDJSON_SUPPORTS_CONCEPTS
|
||||
void serialize_car_simple(const Car& car, simdjson::builder::string_builder& builder) {
|
||||
@@ -474,6 +519,20 @@ namespace builder_tests {
|
||||
ASSERT_EQUAL(s, "[[1.0,2.0],[3.0,4.0]]");
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
bool double_double_test_to_string() {
|
||||
TEST_START();
|
||||
std::vector<std::vector<double>> c = {{1.0, 2.0}, {3.0, 4.0}};
|
||||
simdjson::builder::string_builder sb;
|
||||
sb.append(c);
|
||||
std::string_view p;
|
||||
auto result = sb.view().get(p);
|
||||
ASSERT_SUCCESS(result);
|
||||
ASSERT_EQUAL(p, "[[1.0,2.0],[3.0,4.0]]");
|
||||
std::string s;
|
||||
simdjson::simdjson_error ec = simdjson::to_json(c,s);
|
||||
ASSERT_EQUAL(s, "[[1.0,2.0],[3.0,4.0]]");
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
#if SIMDJSON_EXCEPTIONS
|
||||
bool car_test_simple_complete_exceptions() {
|
||||
TEST_START();
|
||||
@@ -522,6 +581,7 @@ namespace builder_tests {
|
||||
#if SIMDJSON_SUPPORTS_RANGES && SIMDJSON_SUPPORTS_CONCEPTS
|
||||
map_test() &&
|
||||
double_double_test() &&
|
||||
double_double_test_to_string() &&
|
||||
car_test_simple() &&
|
||||
car_test_simple_complete() &&
|
||||
#if SIMDJSON_EXCEPTIONS
|
||||
@@ -529,7 +589,8 @@ namespace builder_tests {
|
||||
#endif
|
||||
#endif
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
serialize_optional() &&
|
||||
car_test_template() &&
|
||||
serialize_optional() &&
|
||||
#endif
|
||||
append_char() &&
|
||||
append_integer() &&
|
||||
|
||||
@@ -1,176 +0,0 @@
|
||||
#include "simdjson.h"
|
||||
#include "test_builder.h"
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <iostream>
|
||||
|
||||
using namespace simdjson;
|
||||
|
||||
namespace builder_tests {
|
||||
|
||||
struct Meeting {
|
||||
std::string title;
|
||||
std::chrono::system_clock::time_point start_time;
|
||||
std::vector<std::string> attendees;
|
||||
std::optional<std::string> location;
|
||||
bool is_recurring;
|
||||
};
|
||||
|
||||
bool test_meeting_roundtrip() {
|
||||
TEST_START();
|
||||
#if SIMDJSON_STATIC_REFLECTION
|
||||
// Create a Meeting instance with current time
|
||||
auto now = std::chrono::system_clock::now();
|
||||
Meeting original{
|
||||
.title = "CppCon Planning",
|
||||
.start_time = now,
|
||||
.attendees = {"Alice", "Bob", "Charlie"},
|
||||
.location = "Denver",
|
||||
.is_recurring = true
|
||||
};
|
||||
|
||||
// Serialize to JSON
|
||||
auto json_result = builder::to_json_string(original);
|
||||
ASSERT_SUCCESS(json_result);
|
||||
std::string json = json_result.value();
|
||||
|
||||
std::cout << "Serialized JSON: " << json << std::endl;
|
||||
|
||||
// Parse back from JSON
|
||||
ondemand::parser parser;
|
||||
auto doc_result = parser.iterate(pad(json));
|
||||
ASSERT_SUCCESS(doc_result);
|
||||
|
||||
auto get_result = doc_result.value().get<Meeting>();
|
||||
ASSERT_SUCCESS(get_result);
|
||||
|
||||
Meeting deserialized = std::move(get_result.value());
|
||||
|
||||
// Verify round-trip
|
||||
ASSERT_EQUAL(deserialized.title, original.title);
|
||||
ASSERT_EQUAL(deserialized.is_recurring, original.is_recurring);
|
||||
ASSERT_TRUE(deserialized.location.has_value());
|
||||
ASSERT_EQUAL(deserialized.location.value(), original.location.value());
|
||||
ASSERT_EQUAL(deserialized.attendees.size(), original.attendees.size());
|
||||
for(size_t i = 0; i < original.attendees.size(); i++) {
|
||||
ASSERT_EQUAL(deserialized.attendees[i], original.attendees[i]);
|
||||
}
|
||||
|
||||
// Check that the time point is correctly deserialized with millisecond precision
|
||||
auto original_ms = std::chrono::duration_cast<std::chrono::milliseconds>(original.start_time.time_since_epoch()).count();
|
||||
auto deserialized_ms = std::chrono::duration_cast<std::chrono::milliseconds>(deserialized.start_time.time_since_epoch()).count();
|
||||
|
||||
std::cout << "Original time: " << original_ms << "ms since epoch" << std::endl;
|
||||
std::cout << "Deserialized time: " << deserialized_ms << "ms since epoch" << std::endl;
|
||||
|
||||
// Verify exact equality at millisecond precision
|
||||
ASSERT_EQUAL(deserialized_ms, original_ms);
|
||||
|
||||
#endif
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
|
||||
bool test_duration_roundtrip() {
|
||||
TEST_START();
|
||||
#if SIMDJSON_STATIC_REFLECTION
|
||||
struct Task {
|
||||
std::string name;
|
||||
std::chrono::milliseconds duration_ms;
|
||||
std::chrono::seconds duration_s;
|
||||
std::chrono::minutes duration_min;
|
||||
};
|
||||
|
||||
Task original{
|
||||
.name = "Build Project",
|
||||
.duration_ms = std::chrono::milliseconds(5432),
|
||||
.duration_s = std::chrono::seconds(300),
|
||||
.duration_min = std::chrono::minutes(45)
|
||||
};
|
||||
|
||||
// Serialize to JSON
|
||||
auto json_result = builder::to_json_string(original);
|
||||
ASSERT_SUCCESS(json_result);
|
||||
std::string json = json_result.value();
|
||||
|
||||
std::cout << "Duration test JSON: " << json << std::endl;
|
||||
|
||||
// Parse back from JSON
|
||||
ondemand::parser parser;
|
||||
auto doc_result = parser.iterate(pad(json));
|
||||
ASSERT_SUCCESS(doc_result);
|
||||
|
||||
auto get_result = doc_result.value().get<Task>();
|
||||
ASSERT_SUCCESS(get_result);
|
||||
|
||||
Task deserialized = std::move(get_result.value());
|
||||
|
||||
// Verify round-trip
|
||||
ASSERT_EQUAL(deserialized.name, original.name);
|
||||
ASSERT_EQUAL(deserialized.duration_ms.count(), original.duration_ms.count());
|
||||
ASSERT_EQUAL(deserialized.duration_s.count(), original.duration_s.count());
|
||||
ASSERT_EQUAL(deserialized.duration_min.count(), original.duration_min.count());
|
||||
|
||||
#endif
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
|
||||
bool test_different_clock_types() {
|
||||
TEST_START();
|
||||
#if SIMDJSON_STATIC_REFLECTION
|
||||
struct Schedule {
|
||||
std::string event;
|
||||
std::chrono::system_clock::time_point system_time;
|
||||
std::chrono::steady_clock::time_point steady_time;
|
||||
};
|
||||
|
||||
Schedule original{
|
||||
.event = "Conference",
|
||||
.system_time = std::chrono::system_clock::now(),
|
||||
.steady_time = std::chrono::steady_clock::now()
|
||||
};
|
||||
|
||||
// Serialize to JSON
|
||||
auto json_result = builder::to_json_string(original);
|
||||
ASSERT_SUCCESS(json_result);
|
||||
std::string json = json_result.value();
|
||||
|
||||
std::cout << "Clock types test JSON: " << json << std::endl;
|
||||
|
||||
// Parse back from JSON
|
||||
ondemand::parser parser;
|
||||
auto doc_result = parser.iterate(pad(json));
|
||||
ASSERT_SUCCESS(doc_result);
|
||||
|
||||
auto get_result = doc_result.value().get<Schedule>();
|
||||
ASSERT_SUCCESS(get_result);
|
||||
|
||||
Schedule deserialized = std::move(get_result.value());
|
||||
|
||||
// Verify round-trip - check millisecond precision
|
||||
ASSERT_EQUAL(deserialized.event, original.event);
|
||||
|
||||
auto orig_sys_ms = std::chrono::duration_cast<std::chrono::milliseconds>(original.system_time.time_since_epoch()).count();
|
||||
auto deser_sys_ms = std::chrono::duration_cast<std::chrono::milliseconds>(deserialized.system_time.time_since_epoch()).count();
|
||||
ASSERT_EQUAL(deser_sys_ms, orig_sys_ms);
|
||||
|
||||
auto orig_steady_ms = std::chrono::duration_cast<std::chrono::milliseconds>(original.steady_time.time_since_epoch()).count();
|
||||
auto deser_steady_ms = std::chrono::duration_cast<std::chrono::milliseconds>(deserialized.steady_time.time_since_epoch()).count();
|
||||
ASSERT_EQUAL(deser_steady_ms, orig_steady_ms);
|
||||
|
||||
#endif
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
|
||||
bool run() {
|
||||
return test_meeting_roundtrip() &&
|
||||
test_duration_roundtrip() &&
|
||||
test_different_clock_types();
|
||||
}
|
||||
|
||||
} // namespace builder_tests
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
return builder_tests::run() ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
|
||||
|
||||
|
||||
#include "simdjson.h"
|
||||
#include "test_builder.h"
|
||||
#include <charconv>
|
||||
#include <string>
|
||||
|
||||
using namespace simdjson;
|
||||
|
||||
// Suppose that we want to serialize/deserialize Car using
|
||||
// strings for the year
|
||||
struct Car {
|
||||
std::string make;
|
||||
std::string model;
|
||||
int64_t year;
|
||||
std::vector<float> tire_pressure;
|
||||
};
|
||||
|
||||
|
||||
|
||||
namespace simdjson {
|
||||
// This tag_invoke MUST be inside simdjson namespace
|
||||
template <typename simdjson_value>
|
||||
auto tag_invoke(deserialize_tag, simdjson_value &val, Car& car) {
|
||||
ondemand::object obj;
|
||||
auto error = val.get_object().get(obj);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
if ((error = obj["make"].get_string(car.make))) {
|
||||
return error;
|
||||
}
|
||||
if ((error = obj["model"].get_string(car.model))) {
|
||||
return error;
|
||||
}
|
||||
std::string_view year_str;
|
||||
if ((error = obj["year"].get(year_str))) {
|
||||
return error;
|
||||
}
|
||||
int64_t year_value;
|
||||
auto [ptr, ec] = std::from_chars(year_str.data(), year_str.data() + year_str.size(), year_value);
|
||||
if (ec != std::errc{}) {
|
||||
return INCORRECT_TYPE;
|
||||
}
|
||||
car.year = year_value;
|
||||
if ((error = obj["tire_pressure"].get<std::vector<float>>().get(
|
||||
car.tire_pressure))) {
|
||||
return error;
|
||||
}
|
||||
return simdjson::SUCCESS;
|
||||
}
|
||||
|
||||
template <typename builder_type>
|
||||
void tag_invoke(serialize_tag, builder_type &builder, const Car& car) {
|
||||
builder.start_object();
|
||||
builder.append_key_value("make", car.make);
|
||||
builder.append_comma();
|
||||
builder.append_key_value("model", car.model);
|
||||
builder.append_comma();
|
||||
builder.append_key_value("year", std::to_string(car.year));
|
||||
builder.append_comma();
|
||||
builder.append_key_value("tire_pressure", car.tire_pressure);
|
||||
builder.end_object();
|
||||
}
|
||||
|
||||
} // namespace simdjson
|
||||
namespace custom_tests {
|
||||
|
||||
bool test_car_deserialization() {
|
||||
TEST_START();
|
||||
auto json = R"({"make":"Toyota","model":"Camry","year":"2018","tire_pressure":[30.5,30.6,30.7,30.8]})"_padded;
|
||||
ondemand::parser parser;
|
||||
auto doc_result = parser.iterate(json);
|
||||
ASSERT_SUCCESS(doc_result);
|
||||
Car car;
|
||||
ASSERT_SUCCESS(doc_result.get(car));
|
||||
if (car.make != "Toyota" || car.model != "Camry" || std::to_string(car.year) != "2018" || car.tire_pressure.size() != 4 || car.tire_pressure[0] != 30.5f) {
|
||||
return false;
|
||||
}
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
bool test_car_serialization() {
|
||||
TEST_START();
|
||||
Car car{"Toyota", "Camry", 2018, {30.5f,30.6f,30.7f,30.8f}};
|
||||
simdjson::builder::string_builder builder;
|
||||
builder.append(car);
|
||||
std::string_view json;
|
||||
ASSERT_SUCCESS(builder.view().get(json));
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
bool test_car_roundtrip() {
|
||||
TEST_START();
|
||||
Car car{"Toyota", "Camry", 2018, {30.5f,30.6f,30.7f,30.8f}};
|
||||
simdjson::builder::string_builder builder;
|
||||
builder.append(car);
|
||||
std::string json;
|
||||
ASSERT_SUCCESS(builder.view().get(json));
|
||||
ondemand::parser parser;
|
||||
auto doc_result = parser.iterate(json);
|
||||
ASSERT_SUCCESS(doc_result);
|
||||
Car carback;
|
||||
ASSERT_SUCCESS(doc_result.get(carback));
|
||||
|
||||
if (carback.make != car.make || carback.model != car.model || carback.year != car.year || carback.tire_pressure != car.tire_pressure) {
|
||||
return false;
|
||||
}
|
||||
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
|
||||
bool test_car_roundtrip_to_json() {
|
||||
TEST_START();
|
||||
Car car{"Toyota", "Camry", 2018, {30.5f,30.6f,30.7f,30.8f}};
|
||||
std::string json;
|
||||
ASSERT_SUCCESS(simdjson::to_json(car).get(json));
|
||||
simdjson::builder::string_builder builder;
|
||||
ondemand::parser parser;
|
||||
auto doc_result = parser.iterate(json);
|
||||
ASSERT_SUCCESS(doc_result);
|
||||
Car carback;
|
||||
ASSERT_SUCCESS(doc_result.get(carback));
|
||||
|
||||
if (carback.make != car.make || carback.model != car.model || carback.year != car.year || carback.tire_pressure != car.tire_pressure) {
|
||||
return false;
|
||||
}
|
||||
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
bool run() {
|
||||
return test_car_deserialization() && test_car_serialization() && test_car_roundtrip() && test_car_roundtrip_to_json();
|
||||
}
|
||||
|
||||
} // namespace builder_tests
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
return test_main(argc, argv, custom_tests::run);
|
||||
}
|
||||
@@ -123,7 +123,7 @@ endif()
|
||||
|
||||
# Add the tests if we're on:
|
||||
# 1. Visual Studio 2022 v17.6 or later
|
||||
# 2. GCC v14.0.0 or later (GCC v13.0.0 cannot handle pipe oprator of lambda)
|
||||
# 2. GCC v14.0.0 or later (GCC v13.0.0 cannot handle pipe operator of lambda)
|
||||
# 3. Clang v15.0.0 or later (certain version C++ headers occur error when compiling)
|
||||
if(
|
||||
(MSVC AND MSVC_VERSION LESS 1930) OR
|
||||
|
||||
@@ -62,57 +62,6 @@ struct complicated_weather_data {
|
||||
#ifdef __cpp_lib_ranges
|
||||
|
||||
namespace convert_tests {
|
||||
#if SIMDJSON_EXCEPTIONS && SIMDJSON_SUPPORTS_CONCEPTS
|
||||
struct Car {
|
||||
std::string make{};
|
||||
std::string model{};
|
||||
int year{};
|
||||
std::vector<double> tire_pressure{};
|
||||
|
||||
friend simdjson::error_code tag_invoke(simdjson::deserialize_tag, auto &val,
|
||||
Car &car) {
|
||||
simdjson::ondemand::object obj;
|
||||
auto error = val.get_object().get(obj);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
// Instead of repeatedly obj["something"], we iterate through the object
|
||||
// which we expect to be faster.
|
||||
for (auto field : obj) {
|
||||
simdjson::ondemand::raw_json_string key;
|
||||
error = field.key().get(key);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
if (key == "make") {
|
||||
error = field.value().get_string(car.make);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
} else if (key == "model") {
|
||||
error = field.value().get_string(car.model);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
} else if (key == "year") {
|
||||
error = field.value().get(car.year);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
} else if (key == "tire_pressure") {
|
||||
error = field.value().get(car.tire_pressure);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
}
|
||||
return simdjson::SUCCESS;
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(simdjson::custom_deserializable<std::unique_ptr<Car>>,
|
||||
"It should be deserializable");
|
||||
|
||||
|
||||
simdjson::padded_string json_car =
|
||||
R"( {
|
||||
@@ -130,22 +79,114 @@ simdjson::padded_string json_cars =
|
||||
"tire_pressure": [ 29.8, 30.0 ] }
|
||||
])"_padded;
|
||||
|
||||
bool simple() {
|
||||
TEST_START();
|
||||
Car car = simdjson::from(json_car);
|
||||
if (car.make != "Toyota" || car.model != "Camry" || car.year != 2018) {
|
||||
return false;
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
struct Car {
|
||||
std::string make{};
|
||||
std::string model{};
|
||||
int year{};
|
||||
std::vector<double> tire_pressure{};
|
||||
|
||||
friend simdjson::error_code tag_invoke(simdjson::deserialize_tag, auto &val,
|
||||
Car &car) {
|
||||
simdjson::ondemand::object obj;
|
||||
auto error = val.get_object().get(obj);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
// Instead of repeatedly obj["something"], we iterate through the object
|
||||
// which we expect to be faster.
|
||||
for (auto field : obj) {
|
||||
simdjson::ondemand::raw_json_string key;
|
||||
error = field.key().get(key);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
if (key == "make") {
|
||||
error = field.value().get_string(car.make);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
} else if (key == "model") {
|
||||
error = field.value().get_string(car.model);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
} else if (key == "year") {
|
||||
error = field.value().get(car.year);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
} else if (key == "tire_pressure") {
|
||||
error = field.value().get(car.tire_pressure);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
}
|
||||
return simdjson::SUCCESS;
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(simdjson::custom_deserializable<std::unique_ptr<Car>>,
|
||||
"It should be deserializable");
|
||||
|
||||
|
||||
bool simple_no_except() {
|
||||
TEST_START();
|
||||
Car car;
|
||||
ASSERT_SUCCESS(simdjson::from(json_car).get(car));
|
||||
if (car.make != "Toyota" || car.model != "Camry" || car.year != 2018) {
|
||||
return false;
|
||||
}
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
|
||||
#endif // SIMDJSON_SUPPORTS_CONCEPTS
|
||||
#if SIMDJSON_EXCEPTIONS && SIMDJSON_SUPPORTS_CONCEPTS
|
||||
|
||||
bool simple() {
|
||||
TEST_START();
|
||||
Car car = simdjson::from(json_car);
|
||||
if (car.make != "Toyota" || car.model != "Camry" || car.year != 2018) {
|
||||
return false;
|
||||
}
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
struct Player {
|
||||
std::string username;
|
||||
int level;
|
||||
double health;
|
||||
};
|
||||
struct BadPlayer {
|
||||
int username; // Oops, should be string!
|
||||
int level;
|
||||
double health;
|
||||
};
|
||||
struct OptionalPlayer {
|
||||
std::string username;
|
||||
std::optional<int> level;
|
||||
double health;
|
||||
};
|
||||
#if SIMDJSON_STATIC_REFLECTION
|
||||
bool missing_key_player() {
|
||||
TEST_START();
|
||||
std::string json = R"({"username":"Alice","health":100.0})";
|
||||
simdjson::padded_string padded(json);
|
||||
Player p;
|
||||
simdjson::ondemand::parser parser;
|
||||
auto doc = parser.iterate(padded);
|
||||
ASSERT_ERROR(doc.get(p), simdjson::NO_SUCH_FIELD);
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
bool missing_key_optional_player() {
|
||||
TEST_START();
|
||||
std::string json = R"({"username":"Alice","health":100.0})";
|
||||
simdjson::padded_string padded(json);
|
||||
OptionalPlayer p;
|
||||
simdjson::ondemand::parser parser;
|
||||
auto doc = parser.iterate(padded);
|
||||
ASSERT_SUCCESS(doc.get(p));
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
bool bad_player() {
|
||||
TEST_START();
|
||||
// username is a string but we declared it as an int
|
||||
@@ -466,7 +507,10 @@ bool test_to_vs_from_equivalence() {
|
||||
bool run() {
|
||||
return
|
||||
#if SIMDJSON_STATIC_REFLECTION
|
||||
meeting_time_test() && meeting_test() && bad_player() && good_player() && complicated_weather_test() &&
|
||||
missing_key_optional_player() && missing_key_player() && meeting_time_test() && meeting_test() && bad_player() && good_player() && complicated_weather_test() &&
|
||||
#endif
|
||||
#if SIMDJSON_SUPPORTS_CONCEPTS
|
||||
simple_no_except() &&
|
||||
#endif
|
||||
#if SIMDJSON_EXCEPTIONS && SIMDJSON_SUPPORTS_CONCEPTS
|
||||
broken() && simple() && simple_optional() && with_parser() && to_array() &&
|
||||
|
||||
Reference in New Issue
Block a user