Files
simdjson-simdjson/tests/builder/static_reflection_builder_tests.cpp
T
Daniel Lemire c806e955c4 C++26 static reflection (#2282)
* Initial work on JSON builder

* moving the files back to ondemand for now.

* tweak

* more later

* update

* minor edits

* dropping vs arm (missing support)

* adding tests. we still specialized write_string_escaped

* tweaking

* fix typo

* tweaking the approach

* minor fix

* missing store

* another missing store

* Attempt at fixing failing serialization tests. (#2292)

* Fixing appeand_float typo (#2294)

* applying a couple of fixes

* updating single header

* fix for pre C++17 if constexpr

* Fixing unused argument problem and updating the singleheader file

* various pedantic fixes

* Sketch of builder

* reordering.

* simplify

* Adding draft of static reflection based deserialization

* Updating simdjson singleheader

* patching the automated deserialization.

* automated

* Adding support for smart pointers of user defined types.

* Adding specialization for smart pointers for basic types. I think it is highly likely that this can be done in a more generic way.

* Referncing a later version of rapidjson that fixed the issue related with assignment attempt of a const variable for GenericStringRef class.

* guarding the tests

* adding documentation for string_builder

* saving

* rename to 'append'

* saving

* non-functional benchmarks (#2342)

* non-functional benchmarks

* Fix typo

* various fixes

* tweaking

---------

Co-authored-by: Daniel Lemire <dlemire@lemire.me>
Co-authored-by: Francisco Geiman Thiesen <franciscogthiesen@gmail.com>

* tuning

* various minor fixes

* minor tweak

* minor simplification

* updating amal

* adding a cast

* update

* fancy casting

* removing dead code

* Pushing latest changes. CITM benchmark is still not working.

* Still not working, but now I am getting only 10 errors.

* add static reflection benchmark to 'large random' benchmark and allows (#2349)

deserialization (with static reflection) from objects and arrays.

Co-authored-by: Daniel Lemire <dlemire@lemire.me>

* Removing std::map from CitmCatalog definition, since that is not currently supported.

* Added free to rust bench, segfault is still happening..

* The syntax changed: ^E became ^^E. (#2350)

* The syntax changed: ^E became ^^E.

* guarding

---------

Co-authored-by: Daniel Lemire <dlemire@lemire.me>

* Adding support for string_view_keyed_map types.

* Adding concepts as a conditional include.

* updating single-header

* Adding concepts to ondemand deps

* rust benchmark is finally working

* Fixing small typo in docs.

* adding docker config and instructions so that our users can test the static reflection (#2358)

* adding docker config and instructions so that our users can test the
static reflection

* completing the instructions

* pruning white spaces

---------

Co-authored-by: Daniel Lemire <dlemire@lemire.me>

* minor optimizations on the JSON builder branch

* avoiding undef behaviour

* saving

* somewhat nicer builder

* make it possible to run just one benchmark

* adding linux perf

* fixing minor issue

* updating swar

* Adding real world compilation benchmark (#2379)

* Adding compilation benchmark for json parsing with and without reflection

* Moving it to the benchmark folder, also reducing a bit the number of iterations.

* Removing script from root folder.

* Reducing number of iterations

* Update benchmark/benchmark_reflection_usage_compilation.sh

Co-authored-by: Daniel Lemire <daniel@lemire.me>

* Update benchmark/benchmark_reflection_usage_compilation.sh

Co-authored-by: Daniel Lemire <daniel@lemire.me>

* Update benchmark/benchmark_reflection_usage_compilation.sh

Co-authored-by: Daniel Lemire <daniel@lemire.me>

* Making the script more customizable and also test whether the compiler being used supports reflection before actually running the benchmark

---------

Co-authored-by: Daniel Lemire <daniel@lemire.me>

* Using define_static_string from  https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3491r2.html (#2389)

* Applying changes needed after latest reflection paper updates.

* Working, but no template for yet.

* Updating single-header to incldue the use of define_static_string.

* copying over master

---------

Co-authored-by: Daniel Lemire <dlemire@lemire.me>
Co-authored-by: Francisco Geiman Thiesen <franciscogthiesen@gmail.com>
2025-07-14 15:43:52 -04:00

143 lines
3.9 KiB
C++

#include "simdjson.h"
#include "test_builder.h"
#include <string>
#include <string_view>
#include <vector>
using namespace simdjson;
struct Car {
std::string make;
std::string model;
int64_t year;
std::vector<double> tire_pressure;
};
struct kid {
int age;
std::string name;
std::vector<std::string> toys;
bool operator<=> (const kid&) const = default;
};
struct Z {
int x;
bool operator<=> (const Z&) const = default;
};
struct Y {
int g;
std::string h;
std::vector<int> i;
Z z;
bool operator<=> (const Y&) const = default;
};
struct X {
char a;
int b;
int c;
std::string d;
std::vector<int> e;
std::vector<std::string> f;
Y y;
bool operator<=> (const X&) const = default;
};
namespace builder_tests {
bool car_test() {
TEST_START();
simdjson::builder::string_builder sb;
Car c = {"Toyota", "Corolla", 2017, {30.0,30.2,30.513,30.79}};
append(sb, c);
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]}");
std::string pstr(p.begin(), p.end());
ASSERT_EQUAL(pstr, "{\"make\":\"Toyota\",\"model\":\"Corolla\",\"year\":2017,\"tire_pressure\":[30.0,30.2,30.513,30.79]}");
simdjson::ondemand::parser parser;
simdjson::ondemand::document doc;
ASSERT_SUCCESS(parser.iterate(simdjson::pad(pstr)).get(doc));
Car c2;
ASSERT_SUCCESS(doc.get<Car>().get(c2));
ASSERT_EQUAL(c2.make, "Toyota");
ASSERT_EQUAL(c2.model, "Corolla");
ASSERT_EQUAL(c2.year, 2017);
ASSERT_EQUAL(c2.tire_pressure.size(), 4);
ASSERT_EQUAL(c2.tire_pressure[0], 30.0);
ASSERT_EQUAL(c2.tire_pressure[1], 30.2);
ASSERT_EQUAL(c2.tire_pressure[2], 30.513);
ASSERT_EQUAL(c2.tire_pressure[3], 30.79);
TEST_SUCCEED();
}
bool serialize_deserialize_kid() {
TEST_START();
simdjson::padded_string json_str =
R"({"age": 12, "name": "John", "toys": ["car", "ball"]})"_padded;
simdjson::ondemand::parser parser;
simdjson::ondemand::document doc;
ASSERT_SUCCESS(parser.iterate(json_str).get(doc));
kid k;
ASSERT_SUCCESS(doc.get<kid>().get(k));
ASSERT_EQUAL(k.age, 12);
ASSERT_EQUAL(k.name, "John");
ASSERT_EQUAL(k.toys.size(), 2);
ASSERT_EQUAL(k.toys[0], "car");
ASSERT_EQUAL(k.toys[1], "ball");
// Now, go the other direction:
std::string json;
ASSERT_SUCCESS(simdjson::builder::to_json_string(k).get(json));
std::cout << json << std::endl;
// Now we parse it back:
simdjson::ondemand::parser parser2;
simdjson::ondemand::document doc2;
ASSERT_SUCCESS(parser2.iterate(simdjson::pad(json)).get(doc2));
kid k2;
ASSERT_SUCCESS(doc2.get<kid>().get(k2));
ASSERT_EQUAL(k2.age, 12);
ASSERT_EQUAL(k2.name, "John");
ASSERT_EQUAL(k2.toys.size(), 2);
ASSERT_EQUAL(k2.toys[0], "car");
ASSERT_EQUAL(k2.toys[1], "ball");
TEST_SUCCEED();
}
bool serialize_deserialize_x_y_z() {
TEST_START();
X s1 = {.a = '1',
.b = 10,
.c = 0,
.d = "test string\n\r\"",
.e = {1, 2, 3},
.f = {"ab", "cd", "fg"},
.y = {.g = 100,
.h = "test string\n\r\"",
.i = {1, 2, 3},
.z = {.x = 1000}}};
std::string pstr;
ASSERT_SUCCESS(simdjson::builder::to_json_string(s1).get(pstr));
ASSERT_EQUAL(
pstr,
R"({"a":"1","b":10,"c":0,"d":"test string\n\r\"","e":[1,2,3],"f":["ab","cd","fg"],"y":{"g":100,"h":"test string\n\r\"","i":[1,2,3],"z":{"x":1000}}})");
simdjson::ondemand::parser parser;
simdjson::ondemand::document doc;
ASSERT_SUCCESS(parser.iterate(simdjson::pad(pstr)).get(doc));
X s2;
ASSERT_SUCCESS(doc.get<X>().get(s2));
ASSERT_TRUE(s1 == s2);
TEST_SUCCEED();
}
bool run() {
return car_test() && serialize_deserialize_kid() && serialize_deserialize_x_y_z() && true;
}
} // namespace builder_tests
int main(int argc, char *argv[]) {
return test_main(argc, argv, builder_tests::run);
}