mirror of
https://github.com/simdjson/simdjson
synced 2026-06-08 17:27:07 +00:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 786c68b158 | |||
| 703ef54bd9 | |||
| 2526068e2f | |||
| ddc7b8c7dd | |||
| d8f90bdd14 | |||
| e38a4923e5 | |||
| e6dfa2e0ed | |||
| 1f369ef210 | |||
| 22dcdc9f1e | |||
| dda2dafa30 | |||
| 72e9d44e10 | |||
| 8aae14931d | |||
| ef3d1ac25f | |||
| 9292480a9b | |||
| 611ea97dbd | |||
| f249e7e128 | |||
| 44fddaa807 | |||
| be16e1ae04 |
@@ -0,0 +1,22 @@
|
||||
name: Ubuntu 24.04 CI (CXX 20, noexcept)
|
||||
|
||||
on: [push, pull_request]
|
||||
jobs:
|
||||
ubuntu-build:
|
||||
if: >-
|
||||
! contains(toJSON(github.event.commits.*.message), '[skip ci]') &&
|
||||
! contains(toJSON(github.event.commits.*.message), '[skip github]')
|
||||
runs-on: ubuntu-24.04
|
||||
strategy:
|
||||
matrix:
|
||||
cxx: [g++-13, clang++-16]
|
||||
steps:
|
||||
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
|
||||
- name: Prepare
|
||||
run: cmake -DSIMDJSON_CXX_STANDARD=20 -DSIMDJSON_EXCEPTIONS=OFF -DSIMDJSON_DEVELOPER_MODE=ON -B build
|
||||
env:
|
||||
CXX: ${{matrix.cxx}}
|
||||
- name: Build
|
||||
run: cmake --build build -j=2
|
||||
- name: Test
|
||||
run: ctest --output-on-failure --test-dir 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 .
|
||||
|
||||
@@ -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.0
|
||||
VERSION 4.0.5
|
||||
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.0"
|
||||
PROJECT_NUMBER = "4.0.5"
|
||||
|
||||
# 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.
|
||||
@@ -117,6 +118,7 @@ Godbolt
|
||||
-------------
|
||||
|
||||
Some users may want to browse code along with the compiled assembly. You want to check out the following lists of examples:
|
||||
* [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)
|
||||
|
||||
|
||||
@@ -6,38 +6,42 @@ CPMAddPackage(
|
||||
EXCLUDE_FROM_ALL YES
|
||||
)
|
||||
|
||||
option(SIMDJSON_USE_RUST "Build the static_reflect benchmark" OFF)
|
||||
|
||||
|
||||
if(NOT WIN32)
|
||||
# We want the check whether Rust is available before trying to build a crate.
|
||||
CPMAddPackage(
|
||||
NAME corrosion
|
||||
GITHUB_REPOSITORY corrosion-rs/corrosion
|
||||
VERSION 0.4.4
|
||||
DOWNLOAD_ONLY ON
|
||||
OPTIONS "Rust_FIND_QUIETLY OFF"
|
||||
)
|
||||
include("${corrosion_SOURCE_DIR}/cmake/FindRust.cmake")
|
||||
endif()
|
||||
|
||||
if(RUST_FOUND)
|
||||
message(STATUS "Rust found: " ${Rust_VERSION} )
|
||||
add_subdirectory("${corrosion_SOURCE_DIR}" "${PROJECT_BINARY_DIR}/_deps/corrosion" EXCLUDE_FROM_ALL)
|
||||
# Important: we want to build in release mode!
|
||||
corrosion_import_crate(MANIFEST_PATH "serde-benchmark/Cargo.toml" NO_LINKER_OVERRIDE PROFILE release)
|
||||
else()
|
||||
message(STATUS "Rust/Cargo is unavailable." )
|
||||
message(STATUS "We will not benchmark serde-benchmark." )
|
||||
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
|
||||
message(STATUS "Under macOS, you may be able to install rust with")
|
||||
message(STATUS "curl https://sh.rustup.rs -sSf | sh")
|
||||
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
||||
message(STATUS "Under Linux, you may be able to install rust with a command such as")
|
||||
message(STATUS "apt-get install cargo" )
|
||||
message(STATUS "or" )
|
||||
message(STATUS "curl https://sh.rustup.rs -sSf | sh")
|
||||
if(SIMDJSON_USER_RUST)
|
||||
if(NOT WIN32)
|
||||
# We want the check whether Rust is available before trying to build a crate.
|
||||
CPMAddPackage(
|
||||
NAME corrosion
|
||||
GITHUB_REPOSITORY corrosion-rs/corrosion
|
||||
VERSION 0.4.4
|
||||
DOWNLOAD_ONLY ON
|
||||
OPTIONS "Rust_FIND_QUIETLY OFF"
|
||||
)
|
||||
include("${corrosion_SOURCE_DIR}/cmake/FindRust.cmake")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(RUST_FOUND)
|
||||
message(STATUS "Rust found: " ${Rust_VERSION} )
|
||||
add_subdirectory("${corrosion_SOURCE_DIR}" "${PROJECT_BINARY_DIR}/_deps/corrosion" EXCLUDE_FROM_ALL)
|
||||
# Important: we want to build in release mode!
|
||||
corrosion_import_crate(MANIFEST_PATH "serde-benchmark/Cargo.toml" NO_LINKER_OVERRIDE PROFILE release)
|
||||
else()
|
||||
message(STATUS "Rust/Cargo is unavailable." )
|
||||
message(STATUS "We will not benchmark serde-benchmark." )
|
||||
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
|
||||
message(STATUS "Under macOS, you may be able to install rust with")
|
||||
message(STATUS "curl https://sh.rustup.rs -sSf | sh")
|
||||
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
||||
message(STATUS "Under Linux, you may be able to install rust with a command such as")
|
||||
message(STATUS "apt-get install cargo" )
|
||||
message(STATUS "or" )
|
||||
message(STATUS "curl https://sh.rustup.rs -sSf | sh")
|
||||
endif()
|
||||
endif()
|
||||
else(SIMDJSON_USER_RUST)
|
||||
message(STATUS "We will not benchmark serde-benchmark." )
|
||||
endif(SIMDJSON_USER_RUST)
|
||||
|
||||
# Add the benchmark executable targets
|
||||
add_subdirectory(twitter_benchmark)
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
add_library(simdjson-internal-flags INTERFACE)
|
||||
if(NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
|
||||
# We default to ON for all targets, so that we can use the library in shared libraries.
|
||||
set_target_properties(simdjson-internal-flags PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||
set_target_properties(simdjson-internal-flags PROPERTIES INTERFACE_POSITION_INDEPENDENT_CODE ON)
|
||||
endif(NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
|
||||
|
||||
option(SIMDJSON_CHECK_EOF "Check for the end of the input buffer. The setting is unnecessary since we require padding of the inputs. You should expect tests to fail with this option turned on." OFF)
|
||||
|
||||
+9
-3
@@ -1395,12 +1395,12 @@ Car c = doc.get<Car>();
|
||||
|
||||
We try to automate the parsing of any given structure or class
|
||||
by looking at its non-static public members. At compile-time,
|
||||
the library looks at a simple structre like `Car` and
|
||||
the library looks at a simple structure like `Car` and
|
||||
maps it to parsing code. We call the default constructor,
|
||||
and then assign values to the public members.
|
||||
|
||||
|
||||
#### Special cases
|
||||
#### Special cases
|
||||
|
||||
However, there are instances where the construction cannot
|
||||
be easily automated. Let us consider a class without any
|
||||
@@ -1450,7 +1450,7 @@ struct complicated_weather_data {
|
||||
};
|
||||
```
|
||||
|
||||
The code might as simple as the following.
|
||||
The code might be as simple as the following.
|
||||
|
||||
```cpp
|
||||
auto padded = R"({"time":["2023-03-15T12:00:00Z"],"temperature":[42]})"_padded;
|
||||
@@ -1477,6 +1477,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
|
||||
|
||||
+33
-11
@@ -34,14 +34,15 @@ 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 writting the content, if you have reasons to believe that the content might violate UTF-8 conventions, you can check it as follows:
|
||||
After writing the content, if you have reasons to believe that the content might violate UTF-8 conventions, you can check it as follows:
|
||||
|
||||
- `validate_unicode()`: Checks if the content in the JSON buffer is valid UTF-8. Returns: true if the content is valid UTF-8, false otherwise.
|
||||
|
||||
@@ -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,6 +176,17 @@ 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.
|
||||
|
||||
@@ -239,7 +251,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,7 +270,7 @@ 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
|
||||
|
||||
+1
-1
@@ -129,7 +129,7 @@ Once you have an element, you can navigate it with idiomatic C++ iterators, oper
|
||||
The strings contain unescaped valid UTF-8 strings: no unmatched surrogate is allowed.
|
||||
Internally, numbers are stored as either 64-bit integers or 64-bit floating-point numbers.
|
||||
Thus it is possible to get the full 64-bit integer range (either signed or unsigned).
|
||||
By default, the string `-0` is parsed as the integer 0 as in Pytho or C++. If you set the macro
|
||||
By default, the string `-0` is parsed as the integer 0 as in Python or C++. If you set the macro
|
||||
`SIMDJSON_MINUS_ZERO_AS_FLOAT` to `1` when building simdjson, you can get that `-0` is mapped to `-0.0`
|
||||
as in JavaScript. You can get the desired effect by building simdjson with cmake setting the
|
||||
`SIMDJSON_MINUS_ZERO_AS_FLOAT` to on: `cmake -B build -D SIMDJSON_MINUS_ZERO_AS_FLOAT=ON`.
|
||||
|
||||
@@ -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,6 @@ 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>();
|
||||
}
|
||||
|
||||
template <typename parser_type>
|
||||
template <typename T>
|
||||
inline auto_parser<parser_type>::operator T() noexcept(false) {
|
||||
@@ -116,6 +116,16 @@ template <typename T>
|
||||
inline auto to_adaptor<T>::operator()(ondemand::parser &parser, padded_string_view const str) const noexcept {
|
||||
return auto_parser<ondemand::parser *>{parser, str};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline auto to_adaptor<T>::operator()(std::string str) const noexcept {
|
||||
return auto_parser{pad_with_reserve(str)};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline auto to_adaptor<T>::operator()(ondemand::parser &parser, std::string str) const noexcept {
|
||||
return auto_parser<ondemand::parser *>{parser, pad_with_reserve(str)};
|
||||
}
|
||||
} // namespace internal
|
||||
} // namespace convert
|
||||
} // namespace simdjson
|
||||
|
||||
@@ -46,14 +46,16 @@ 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>);
|
||||
|
||||
//template <typename T>
|
||||
//simdjson_warn_unused simdjson_inline explicit(false) operator simdjson_result<T>() noexcept(is_nothrow_gettable<T>);
|
||||
template <typename T>
|
||||
simdjson_warn_unused simdjson_inline explicit(false) operator T() noexcept(false);
|
||||
|
||||
@@ -72,6 +74,11 @@ struct to_adaptor {
|
||||
T operator()(simdjson_result<ondemand::value> &val) const noexcept;
|
||||
auto operator()(padded_string_view const str) const noexcept;
|
||||
auto operator()(ondemand::parser &parser, padded_string_view const str) const noexcept;
|
||||
// The std::string is padded with reserve to ensure there is enough space for padding.
|
||||
// Some sanitizers may not like this, so you can use simdjson::pad instead.
|
||||
// simdjson::from(simdjson::pad(str))
|
||||
auto operator()(std::string str) const noexcept;
|
||||
auto operator()(ondemand::parser &parser, std::string str) const noexcept;
|
||||
};
|
||||
} // namespace internal
|
||||
} // namespace convert
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#include <charconv>
|
||||
#include <cstring>
|
||||
#include <experimental/meta>
|
||||
#include <meta>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
@@ -263,7 +263,7 @@ 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) {
|
||||
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;
|
||||
@@ -272,8 +272,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; }
|
||||
@@ -290,9 +290,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
|
||||
|
||||
@@ -284,10 +284,12 @@ simdjson_inline void string_builder::clear() noexcept {
|
||||
|
||||
namespace internal {
|
||||
|
||||
// We could specialize further for 32-bit integers.
|
||||
simdjson_really_inline int int_log2(uint32_t x) { return (63 - leading_zeroes(x | 1)); }
|
||||
|
||||
simdjson_really_inline int fast_digit_count(uint32_t x) {
|
||||
template <typename number_type, typename = typename std::enable_if<
|
||||
std::is_unsigned<number_type>::value>::type>
|
||||
simdjson_really_inline int int_log2(number_type x) { return 63 - leading_zeroes(uint64_t(x) | 1); }
|
||||
|
||||
simdjson_really_inline int fast_digit_count_32(uint32_t x) {
|
||||
static uint64_t table[] = {
|
||||
4294967296, 8589934582, 8589934582, 8589934582, 12884901788,
|
||||
12884901788, 12884901788, 17179868184, 17179868184, 17179868184,
|
||||
@@ -299,9 +301,8 @@ simdjson_really_inline int fast_digit_count(uint32_t x) {
|
||||
return uint32_t((x + table[int_log2(x)]) >> 32);
|
||||
}
|
||||
|
||||
simdjson_really_inline int int_log2(uint64_t x) { return 63 - leading_zeroes(x | 1); }
|
||||
|
||||
simdjson_really_inline int fast_digit_count(uint64_t x) {
|
||||
simdjson_really_inline int fast_digit_count_64(uint64_t x) {
|
||||
static uint64_t table[] = {9,
|
||||
99,
|
||||
999,
|
||||
@@ -332,7 +333,11 @@ simdjson_really_inline size_t digit_count(number_type v) noexcept {
|
||||
static_assert(sizeof(number_type) == 8 || sizeof(number_type) == 4 ||
|
||||
sizeof(number_type) == 2 || sizeof(number_type) == 1,
|
||||
"We only support 8-bit, 16-bit, 32-bit and 64-bit numbers");
|
||||
return fast_digit_count(v);
|
||||
SIMDJSON_IF_CONSTEXPR(sizeof(number_type) <= 4) {
|
||||
return fast_digit_count_32(static_cast<uint32_t>(v));
|
||||
} else {
|
||||
return fast_digit_count_64(static_cast<uint64_t>(v));
|
||||
}
|
||||
}
|
||||
static const char decimal_table[200] = {
|
||||
0x30, 0x30, 0x30, 0x31, 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x30, 0x35,
|
||||
@@ -470,6 +475,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);
|
||||
@@ -649,6 +660,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
|
||||
|
||||
@@ -12,7 +12,26 @@
|
||||
namespace simdjson {
|
||||
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 <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 +43,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
|
||||
@@ -62,7 +83,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,9 +140,12 @@ 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>
|
||||
simdjson_inline void append(const T &opt);
|
||||
@@ -232,7 +259,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,6 +267,16 @@ 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
|
||||
|
||||
|
||||
|
||||
@@ -253,7 +253,9 @@ error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deser
|
||||
using value_type = typename std::remove_cvref_t<T>::value_type;
|
||||
|
||||
// Check if the value is null
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset(); // Set to nullopt
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -286,10 +288,10 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept {
|
||||
}
|
||||
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)) {
|
||||
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
|
||||
// as long we are successful or the field is not found, we continue
|
||||
if(e == simdjson::SUCCESS || e == simdjson::NO_SUCH_FIELD) {
|
||||
e = obj[key].get(out.[:mem:]);
|
||||
}
|
||||
@@ -361,7 +363,9 @@ error_code tag_invoke(deserialize_tag, simdjson_value &val, std::shared_ptr<T> &
|
||||
// Unique pointers
|
||||
////////////////////////////////////////
|
||||
error_code tag_invoke(deserialize_tag, auto &val, std::unique_ptr<bool> &out) noexcept {
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset();
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -374,7 +378,9 @@ error_code tag_invoke(deserialize_tag, auto &val, std::unique_ptr<bool> &out) no
|
||||
}
|
||||
|
||||
error_code tag_invoke(deserialize_tag, auto &val, std::unique_ptr<int64_t> &out) noexcept {
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset();
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -387,7 +393,9 @@ error_code tag_invoke(deserialize_tag, auto &val, std::unique_ptr<int64_t> &out)
|
||||
}
|
||||
|
||||
error_code tag_invoke(deserialize_tag, auto &val, std::unique_ptr<uint64_t> &out) noexcept {
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset();
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -400,7 +408,9 @@ error_code tag_invoke(deserialize_tag, auto &val, std::unique_ptr<uint64_t> &out
|
||||
}
|
||||
|
||||
error_code tag_invoke(deserialize_tag, auto &val, std::unique_ptr<double> &out) noexcept {
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset();
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -413,7 +423,9 @@ error_code tag_invoke(deserialize_tag, auto &val, std::unique_ptr<double> &out)
|
||||
}
|
||||
|
||||
error_code tag_invoke(deserialize_tag, auto &val, std::unique_ptr<std::string_view> &out) noexcept {
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset();
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -430,7 +442,9 @@ error_code tag_invoke(deserialize_tag, auto &val, std::unique_ptr<std::string_vi
|
||||
// Shared pointers
|
||||
////////////////////////////////////////
|
||||
error_code tag_invoke(deserialize_tag, auto &val, std::shared_ptr<bool> &out) noexcept {
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset();
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -443,7 +457,9 @@ error_code tag_invoke(deserialize_tag, auto &val, std::shared_ptr<bool> &out) no
|
||||
}
|
||||
|
||||
error_code tag_invoke(deserialize_tag, auto &val, std::shared_ptr<int64_t> &out) noexcept {
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset();
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -456,7 +472,9 @@ error_code tag_invoke(deserialize_tag, auto &val, std::shared_ptr<int64_t> &out)
|
||||
}
|
||||
|
||||
error_code tag_invoke(deserialize_tag, auto &val, std::shared_ptr<uint64_t> &out) noexcept {
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset();
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -469,7 +487,9 @@ error_code tag_invoke(deserialize_tag, auto &val, std::shared_ptr<uint64_t> &out
|
||||
}
|
||||
|
||||
error_code tag_invoke(deserialize_tag, auto &val, std::shared_ptr<double> &out) noexcept {
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset();
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -482,7 +502,9 @@ error_code tag_invoke(deserialize_tag, auto &val, std::shared_ptr<double> &out)
|
||||
}
|
||||
|
||||
error_code tag_invoke(deserialize_tag, auto &val, std::shared_ptr<std::string_view> &out) noexcept {
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset();
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -504,7 +526,9 @@ error_code tag_invoke(deserialize_tag, auto &val, std::shared_ptr<std::string_vi
|
||||
////////////////////////////////////////
|
||||
error_code tag_invoke(deserialize_tag, auto &val, std::unique_ptr<std::string> &out) noexcept {
|
||||
// Check if the value is null
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset(); // Set to nullptr
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -520,7 +544,9 @@ error_code tag_invoke(deserialize_tag, auto &val, std::unique_ptr<std::string> &
|
||||
|
||||
error_code tag_invoke(deserialize_tag, auto &val, std::shared_ptr<std::string> &out) noexcept {
|
||||
// Check if the value is null
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset(); // Set to nullptr
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -536,7 +562,9 @@ error_code tag_invoke(deserialize_tag, auto &val, std::shared_ptr<std::string> &
|
||||
|
||||
error_code tag_invoke(deserialize_tag, auto &val, std::unique_ptr<int> &out) noexcept {
|
||||
// Check if the value is null
|
||||
if (val.is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( val.is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset(); // Set to nullptr
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
@@ -79,7 +79,9 @@ public:
|
||||
using value_type = typename std::remove_cvref_t<T>::value_type;
|
||||
|
||||
// Check if the value is null
|
||||
if (is_null()) {
|
||||
bool is_null_value;
|
||||
SIMDJSON_TRY( is_null().get(is_null_value) );
|
||||
if (is_null_value) {
|
||||
out.reset(); // Set to nullopt
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#define SIMDJSON_SIMDJSON_VERSION_H
|
||||
|
||||
/** The version of simdjson being used (major.minor.revision) */
|
||||
#define SIMDJSON_VERSION "4.0.0"
|
||||
#define SIMDJSON_VERSION "4.0.5"
|
||||
|
||||
namespace simdjson {
|
||||
enum {
|
||||
@@ -19,7 +19,7 @@ enum {
|
||||
/**
|
||||
* The revision (major.minor.REVISION) of simdjson being used.
|
||||
*/
|
||||
SIMDJSON_VERSION_REVISION = 0
|
||||
SIMDJSON_VERSION_REVISION = 5
|
||||
};
|
||||
} // namespace simdjson
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* auto-generated on 2025-09-09 18:44:21 -0400. version 4.0.0 Do not edit! */
|
||||
/* auto-generated on 2025-09-18 15:16:38 -0600. version 4.0.5 Do not edit! */
|
||||
/* including simdjson.cpp: */
|
||||
/* begin file simdjson.cpp */
|
||||
#define SIMDJSON_SRC_SIMDJSON_CPP
|
||||
|
||||
+1093
-268
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -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() &&
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
using namespace simdjson;
|
||||
namespace ranges_test {
|
||||
|
||||
#if SIMDJSON_EXCEPTIONS
|
||||
bool printout() {
|
||||
TEST_START();
|
||||
auto cars_json = R"( [
|
||||
@@ -43,6 +43,7 @@ namespace ranges_test {
|
||||
ASSERT_TRUE(std::ranges::equal(cars, expected));
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
#endif // SIMDJSON_EXCEPTIONS
|
||||
|
||||
bool run() {
|
||||
return
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "simdjson/convert.h"
|
||||
#include "test_ondemand.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <map>
|
||||
#include <ranges>
|
||||
#include <string>
|
||||
@@ -21,6 +22,24 @@ private:
|
||||
std::string date_str;
|
||||
};
|
||||
|
||||
|
||||
struct Meeting {
|
||||
std::string title;
|
||||
std::vector<std::string> attendees;
|
||||
std::optional<std::string> location;
|
||||
bool is_recurring;
|
||||
};
|
||||
|
||||
|
||||
struct MeetingTime {
|
||||
std::string title;
|
||||
std::chrono::system_clock::time_point start_time;
|
||||
std::vector<std::string> attendees;
|
||||
std::optional<std::string> location;
|
||||
bool is_recurring;
|
||||
};
|
||||
|
||||
|
||||
namespace simdjson {
|
||||
template <typename simdjson_value>
|
||||
auto tag_invoke(deserialize_tag, simdjson_value &val, MyDate& date) {
|
||||
@@ -43,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"( {
|
||||
@@ -111,15 +79,78 @@ 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;
|
||||
}
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
#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();
|
||||
}
|
||||
#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 BadPlayer {
|
||||
int username; // Oops, should be string!
|
||||
@@ -160,6 +191,38 @@ bool simple() {
|
||||
ASSERT_SUCCESS(doc.get(p));
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
|
||||
|
||||
bool meeting_test() {
|
||||
TEST_START();
|
||||
std::string json = simdjson::to_json(Meeting{
|
||||
.title = "CppCon Planning",
|
||||
.attendees = {"Alice", "Bob", "Charlie"},
|
||||
.location = "Denver",
|
||||
.is_recurring = true
|
||||
});
|
||||
Meeting m2 = simdjson::from(json);
|
||||
std::cout << m2.title << std::endl;
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
bool meeting_time_test() {
|
||||
TEST_START();
|
||||
auto start_time = std::chrono::system_clock::now();
|
||||
std::string json = simdjson::to_json(MeetingTime{
|
||||
.title = "CppCon Planning",
|
||||
.start_time = start_time,
|
||||
.attendees = {"Alice", "Bob", "Charlie"},
|
||||
.location = "Denver",
|
||||
.is_recurring = true
|
||||
});
|
||||
std::cout << json << std::endl;
|
||||
MeetingTime m2 = simdjson::from(json);
|
||||
//ASSERT_EQUAL(m2.start_time, start_time);
|
||||
std::cout << m2.title << std::endl;
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
bool broken() {
|
||||
TEST_START();
|
||||
@@ -415,7 +478,10 @@ bool test_to_vs_from_equivalence() {
|
||||
bool run() {
|
||||
return
|
||||
#if SIMDJSON_STATIC_REFLECTION
|
||||
bad_player() && good_player() && complicated_weather_test() &&
|
||||
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