mirror of
https://github.com/simdjson/simdjson
synced 2026-06-08 17:27:07 +00:00
adding concept examples to the compile-time JSON. (#2538)
This commit is contained in:
+6
-6
@@ -87,16 +87,16 @@ bool cast_tester<T>::test_get_error(simdjson_result<element> element, error_code
|
||||
|
||||
template<typename T>
|
||||
bool cast_tester<T>::test_get_t(element element, T expected) {
|
||||
auto actual = element.get<T>();
|
||||
ASSERT_SUCCESS(actual.error());
|
||||
return assert_equal(actual.value_unsafe(), expected);
|
||||
T value;
|
||||
ASSERT_SUCCESS(element.get<T>().get(value));
|
||||
return assert_equal(value, expected);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool cast_tester<T>::test_get_t(simdjson_result<element> element, T expected) {
|
||||
auto actual = element.get<T>();
|
||||
ASSERT_SUCCESS(actual.error());
|
||||
return assert_equal(actual.value_unsafe(), expected);
|
||||
T value;
|
||||
ASSERT_SUCCESS(element.get<T>().get(value));
|
||||
return assert_equal(value, expected);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
||||
@@ -597,6 +597,98 @@ bool test_top_level_array_example() {
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
|
||||
/**
|
||||
* Concept to validate that a type represents a person with name and age
|
||||
*/
|
||||
template <typename T>
|
||||
concept person = requires(T p) {
|
||||
std::string_view(p.name); // has name field convertible to string_view
|
||||
p.age; // has age field
|
||||
requires std::is_integral_v<decltype(p.age)>; // age is integral
|
||||
};
|
||||
|
||||
/**
|
||||
* Concept to validate that a type is an array of person objects
|
||||
*/
|
||||
template <typename T>
|
||||
concept array_of_person = requires(T arr) {
|
||||
arr.size(); // has size method
|
||||
arr[0]; // can access elements with []
|
||||
requires person<decltype(arr[0])>; // elements satisfy person concept
|
||||
};
|
||||
|
||||
/**
|
||||
* Test: Array of objects with concept validation
|
||||
*/
|
||||
bool test_array_of_objects_with_concept() {
|
||||
TEST_START();
|
||||
|
||||
constexpr auto config = R"(
|
||||
|
||||
[
|
||||
{ "name": "Alice", "age": 30 },
|
||||
{ "name": "Bob", "age": 25 },
|
||||
{ "name": "Charlie", "age": 35 }
|
||||
]
|
||||
|
||||
)"_json;
|
||||
|
||||
std::print("Array size: {}\n", config.size());
|
||||
|
||||
static_assert(config.size() == 3);
|
||||
|
||||
// Validate that the array satisfies the array_of_person concept
|
||||
static_assert(array_of_person<decltype(config)>);
|
||||
|
||||
|
||||
// Test the actual values
|
||||
static_assert(std::string_view(config[0].name) == "Alice");
|
||||
static_assert(config[0].age == 30);
|
||||
static_assert(std::string_view(config[1].name) == "Bob");
|
||||
static_assert(config[1].age == 25);
|
||||
static_assert(std::string_view(config[2].name) == "Charlie");
|
||||
static_assert(config[2].age == 35);
|
||||
|
||||
// Runtime assertions
|
||||
ASSERT_EQUAL(config.size(), 3);
|
||||
ASSERT_EQUAL(std::string_view(config[0].name), "Alice"sv);
|
||||
ASSERT_EQUAL(config[0].age, 30);
|
||||
ASSERT_EQUAL(std::string_view(config[1].name), "Bob"sv);
|
||||
ASSERT_EQUAL(config[1].age, 25);
|
||||
ASSERT_EQUAL(std::string_view(config[2].name), "Charlie"sv);
|
||||
ASSERT_EQUAL(config[2].age, 35);
|
||||
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test: #embed support for external JSON files (C++26)
|
||||
*/
|
||||
bool test_embed_twitter_json() {
|
||||
TEST_START();
|
||||
|
||||
// C++26 #embed allows embedding files directly into the binary at compile time
|
||||
// This creates a const char array with the file contents plus null terminator
|
||||
constexpr const char twitter_json[] = {
|
||||
#embed TWITTER_JSON
|
||||
, 0
|
||||
};
|
||||
|
||||
// Parse the embedded JSON at compile time
|
||||
constexpr auto parsed_twitter = simdjson::compile_time::parse_json<twitter_json>();
|
||||
|
||||
// Verify the structure - twitter.json should have a "statuses" array
|
||||
static_assert(parsed_twitter.statuses.size() > 0);
|
||||
|
||||
// Runtime verification
|
||||
ASSERT_TRUE(parsed_twitter.statuses.size() > 0);
|
||||
|
||||
std::cout << "Successfully parsed embedded twitter.json with "
|
||||
<< parsed_twitter.statuses.size() << " statuses" << std::endl;
|
||||
|
||||
TEST_SUCCEED();
|
||||
}
|
||||
|
||||
bool run() {
|
||||
return test_basic_object() &&
|
||||
test_nested_objects() &&
|
||||
@@ -620,7 +712,9 @@ bool run() {
|
||||
array_of_objects() &&
|
||||
test_user_config_example() &&
|
||||
test_nested_servers_example() &&
|
||||
test_top_level_array_example();
|
||||
test_top_level_array_example() &&
|
||||
test_array_of_objects_with_concept() &&
|
||||
test_embed_twitter_json();
|
||||
}
|
||||
|
||||
} // namespace compile_time_json_tests
|
||||
|
||||
Reference in New Issue
Block a user