1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
#include <iostream>
#include <set>
#include "simdjson.h"
#include "test_ondemand.h"
using namespace simdjson;
namespace wildcard_tests {
using namespace std;
bool array_wildcard_basic() {
TEST_START();
auto json = R"({
"books": [
{"title": "Book A", "price": 10},
{"title": "Book B", "price": 15},
{"title": "Book C", "price": 20}
]
})"_padded;
ondemand::parser parser;
auto doc = parser.iterate(json);
std::vector<ondemand::value> titles;
ASSERT_SUCCESS(doc.at_path_with_wildcard("$.books[*].title").get(titles));
ASSERT_EQUAL(titles.size(), 3);
std::string_view title;
ASSERT_SUCCESS(titles[0].get_string().get(title));
ASSERT_EQUAL(title, "Book A");
ASSERT_SUCCESS(titles[1].get_string().get(title));
ASSERT_EQUAL(title, "Book B");
ASSERT_SUCCESS(titles[2].get_string().get(title));
ASSERT_EQUAL(title, "Book C");
TEST_SUCCEED();
}
bool array_wildcard_numbers() {
TEST_START();
auto json = R"({
"prices": [10, 20, 30, 40, 50]
})"_padded;
ondemand::parser parser;
auto doc = parser.iterate(json);
std::vector<ondemand::value> prices;
ASSERT_SUCCESS(doc.at_path_with_wildcard("$.prices[*]").get(prices));
ASSERT_EQUAL(prices.size(), 5);
uint64_t price;
ASSERT_SUCCESS(prices[0].get_uint64().get(price));
ASSERT_EQUAL(price, 10);
ASSERT_SUCCESS(prices[2].get_uint64().get(price));
ASSERT_EQUAL(price, 30);
ASSERT_SUCCESS(prices[4].get_uint64().get(price));
ASSERT_EQUAL(price, 50);
TEST_SUCCEED();
}
bool object_wildcard_basic() {
TEST_START();
auto json = R"({
"users": {
"user1": {"name": "Alice", "age": 30},
"user2": {"name": "Bob", "age": 25},
"user3": {"name": "Charlie", "age": 35}
}
})"_padded;
ondemand::parser parser;
auto doc = parser.iterate(json);
std::vector<ondemand::value> names;
auto error = doc.at_path_with_wildcard("$.users.*.name").get(names);
ASSERT_SUCCESS(error);
ASSERT_EQUAL(names.size(), 3);
// Verify we got all three names (order may vary)
std::set<std::string_view> name_set;
for (auto& name : names) {
std::string_view view;
ASSERT_SUCCESS(name.get_string().get(view));
name_set.insert(view);
}
ASSERT_TRUE(name_set.count("Alice") > 0);
ASSERT_TRUE(name_set.count("Bob") > 0);
ASSERT_TRUE(name_set.count("Charlie") > 0);
TEST_SUCCEED();
}
bool combined_wildcards() {
TEST_START();
auto json = R"({
"departments": {
"engineering": {
"employees": [
{"name": "Alice", "salary": 100000},
{"name": "Bob", "salary": 95000}
]
},
"sales": {
"employees": [
{"name": "Charlie", "salary": 80000},
{"name": "Diana", "salary": 85000}
]
}
}
})"_padded;
ondemand::parser parser;
auto doc = parser.iterate(json);
std::vector<ondemand::value> salaries;
auto error = doc.at_path_with_wildcard("$.departments.*.employees[*].salary").get(salaries);
ASSERT_SUCCESS(error);
ASSERT_EQUAL(salaries.size(), 4);
// Check sum of all salaries
uint64_t total = 0;
for (auto& salary : salaries) {
uint64_t value;
ASSERT_SUCCESS(salary.get_uint64().get(value));
total += value;
}
ASSERT_EQUAL(total, 360000);
TEST_SUCCEED();
}
bool empty_array_wildcard() {
TEST_START();
auto json = R"({"items": []})"_padded;
ondemand::parser parser;
auto doc = parser.iterate(json);
std::vector<ondemand::value> items;
auto error = doc.at_path_with_wildcard("$.items[*].name").get(items);
ASSERT_SUCCESS(error);
ASSERT_EQUAL(items.size(), 0);
TEST_SUCCEED();
}
bool empty_object_wildcard() {
TEST_START();
auto json = R"({"users": {}})"_padded;
ondemand::parser parser;
auto doc = parser.iterate(json);
std::vector<ondemand::value> names;
auto error = doc.at_path_with_wildcard("$.users.*.name").get(names);
ASSERT_SUCCESS(error);
ASSERT_EQUAL(names.size(), 0);
TEST_SUCCEED();
}
bool wildcard_on_scalar_error() {
TEST_START();
auto json = R"({"data": 123})"_padded;
ondemand::parser parser;
auto doc = parser.iterate(json);
// Can't use array wildcard on scalar
std::vector<ondemand::value> values;
auto error = doc.at_path_with_wildcard("$.data[*]").get(values);
ASSERT_ERROR(error, INVALID_JSON_POINTER);
TEST_SUCCEED();
}
bool nested_arrays_wildcard() {
TEST_START();
auto json = R"({
"matrix": [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
})"_padded;
ondemand::parser parser;
auto doc = parser.iterate(json);
// Get all nested arrays
std::vector<ondemand::value> arrays;
ASSERT_SUCCESS(doc.at_path_with_wildcard("$.matrix[*]").get(arrays));
// Verify we got 3 arrays
ASSERT_EQUAL(arrays.size(), 3);
TEST_SUCCEED();
}
bool wildcard_with_nested_objects() {
TEST_START();
auto json = R"({
"data": [
{"info": {"id": 1, "status": "active"}},
{"info": {"id": 2, "status": "inactive"}},
{"info": {"id": 3, "status": "active"}}
]
})"_padded;
ondemand::parser parser;
auto doc = parser.iterate(json);
std::vector<ondemand::value> statuses;
ASSERT_SUCCESS(doc.at_path_with_wildcard("$.data[*].info.status").get(statuses));
ASSERT_EQUAL(statuses.size(), 3);
std::string_view status;
ASSERT_SUCCESS(statuses[0].get_string().get(status));
ASSERT_EQUAL(status, "active");
ASSERT_SUCCESS(statuses[1].get_string().get(status));
ASSERT_EQUAL(status, "inactive");
ASSERT_SUCCESS(statuses[2].get_string().get(status));
ASSERT_EQUAL(status, "active");
TEST_SUCCEED();
}
bool mixed_types_in_array() {
TEST_START();
auto json = R"({
"mixed": [
42,
"hello",
{"key": "value"},
[1, 2, 3],
true,
null
]
})"_padded;
ondemand::parser parser;
auto doc = parser.iterate(json);
std::vector<ondemand::value> values;
ASSERT_SUCCESS(doc.at_path_with_wildcard("$.mixed[*]").get(values));
// Just verify we got all 6 elements
ASSERT_EQUAL(values.size(), 6);
// Don't try to access the values - they've been consumed by the OnDemand API
TEST_SUCCEED();
}
bool wildcard_nonexistent_field() {
TEST_START();
auto json = R"({
"data": [
{"name": "Alice"},
{"name": "Bob"},
{"other": "field"}
]
})"_padded;
ondemand::parser parser;
auto doc = parser.iterate(json);
// Third element doesn't have "name" field
std::vector<ondemand::value> names;
auto error = doc.at_path_with_wildcard("$.data[*].name").get(names);
// This might error or return partial results
if (error) {
// If it errors, that's acceptable
TEST_SUCCEED();
}
// Should have at least 2 results
ASSERT_TRUE(names.size() >= 2);
TEST_SUCCEED();
}
bool root_array_wildcard() {
TEST_START();
auto json = R"([
{"id": 1, "name": "Item 1"},
{"id": 2, "name": "Item 2"},
{"id": 3, "name": "Item 3"}
])"_padded;
ondemand::parser parser;
auto doc = parser.iterate(json);
std::vector<ondemand::value> names;
ASSERT_SUCCESS(doc.at_path_with_wildcard("$[*].name").get(names));
ASSERT_EQUAL(names.size(), 3);
std::string_view name;
ASSERT_SUCCESS(names[0].get_string().get(name));
ASSERT_EQUAL(name, "Item 1");
ASSERT_SUCCESS(names[1].get_string().get(name));
ASSERT_EQUAL(name, "Item 2");
ASSERT_SUCCESS(names[2].get_string().get(name));
ASSERT_EQUAL(name, "Item 3");
TEST_SUCCEED();
}
bool run() {
return array_wildcard_basic() &&
array_wildcard_numbers() &&
object_wildcard_basic() &&
combined_wildcards() &&
empty_array_wildcard() &&
empty_object_wildcard() &&
wildcard_on_scalar_error() &&
nested_arrays_wildcard() &&
wildcard_with_nested_objects() &&
mixed_types_in_array() &&
wildcard_nonexistent_field() &&
root_array_wildcard();
}
}
int main(int argc, char *argv[]) {
return test_main(argc, argv, wildcard_tests::run);
}
|