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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
|
// Glaze Library
// For the license information refer to glaze.hpp
#include <charconv> // for std::from_chars
#include "glaze/json.hpp"
#include "ut/ut.hpp"
using namespace ut;
// Structs for testing custom readers with glz::generic
struct generic_optional
{
std::optional<char> val;
void read_val(const glz::generic& value)
{
if (value.is_null()) {
val = std::nullopt;
return;
}
if (value.is_string()) {
const auto& str = value.get<std::string>();
val = str.empty() ? std::nullopt : std::optional(str[0]);
return;
}
val = std::nullopt;
}
glz::generic write_val() const
{
if (val.has_value()) {
return std::string(1, val.value());
}
return nullptr;
}
};
struct generic_optional_int
{
std::optional<int> num;
void read_val(const glz::generic& value)
{
if (value.is_null()) {
num = std::nullopt;
return;
}
if (value.is_number()) {
num = value.as<int>();
return;
}
if (value.is_string()) {
const auto& str = value.get<std::string>();
if (str.empty()) {
num = std::nullopt;
return;
}
int val{};
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), val);
if (ec == std::errc{} && ptr == str.data() + str.size()) {
num = val;
return;
}
}
num = std::nullopt;
}
glz::generic write_val() const
{
if (num.has_value()) {
return num.value();
}
return nullptr;
}
};
template <>
struct glz::meta<generic_optional>
{
using T = generic_optional;
static constexpr auto value = glz::object("val", glz::custom<&T::read_val, &T::write_val>);
};
template <>
struct glz::meta<generic_optional_int>
{
using T = generic_optional_int;
static constexpr auto value = glz::object("num", glz::custom<&T::read_val, &T::write_val>);
};
suite generic_json_tests = [] {
"generic_json_write"_test = [] {
glz::generic json = {{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {{"everything", 42.0}}},
{"list", {1.0, 0.0, 2.0}},
{"object", {{"currency", "USD"}, {"value", 42.99}}}};
std::string buffer{};
expect(not glz::write_json(json, buffer));
expect(
buffer ==
R"({"answer":{"everything":42},"happy":true,"list":[1,0,2],"name":"Niels","nothing":null,"object":{"currency":"USD","value":42.99},"pi":3.141})")
<< buffer;
};
"generic_json_read"_test = [] {
glz::generic json{};
std::string buffer = R"([5,"Hello World",{"pi":3.14},null])";
expect(glz::read_json(json, buffer) == glz::error_code::none);
expect(json[0].get<double>() == 5.0);
expect(json[1].get<std::string>() == "Hello World");
expect(json[2]["pi"].get<double>() == 3.14);
expect(json[3].holds<glz::generic::null_t>());
};
"generic_json_roundtrip"_test = [] {
glz::generic json{};
std::string buffer = R"([5,"Hello World",{"pi":3.14},null])";
expect(glz::read_json(json, buffer) == glz::error_code::none);
expect(glz::write_json(json) == buffer);
expect(json.dump().value() == buffer);
};
"generic_json_const"_test = [] {
auto foo = [](const glz::generic& json) { return json["s"].get<std::string>(); };
glz::generic json = {{"s", "hello world"}};
expect(foo(json) == "hello world");
expect(json.dump().value() == R"({"s":"hello world"})");
std::map<std::string, std::string> obj{};
expect(not glz::read_json(obj, json));
expect(obj.contains("s"));
expect(obj.at("s") == "hello world");
};
"generic_json_int"_test = [] {
glz::generic json = {{"i", 1}};
expect(json["i"].get<double>() == 1);
expect(json.dump().value() == R"({"i":1})");
};
"generic_json_as"_test = [] {
glz::generic json = {{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {{"everything", 42.0}}},
{"list", {1.0, 0.0, 2.0}},
{"object", {{"currency", "USD"}, {"value", 42.99}}}};
expect(json["list"][2].as<int>() == 2);
expect(json["pi"].as<double>() == 3.141);
expect(json["name"].as<std::string_view>() == "Niels");
expect(
json.dump().value() ==
R"({"answer":{"everything":42},"happy":true,"list":[1,0,2],"name":"Niels","nothing":null,"object":{"currency":"USD","value":42.99},"pi":3.141})")
<< json.dump().value();
};
"generic_json_nested_initialization"_test = [] {
static const glz::generic messageSchema = {{"type", "struct"},
{"fields",
{
{{"field", "branch"}, {"type", "string"}},
}}};
std::string buffer{};
expect(not glz::write_json(messageSchema, buffer));
expect(buffer == R"({"fields":[{"field":"branch","type":"string"}],"type":"struct"})") << buffer;
};
"generic_contains"_test = [] {
auto json = glz::read_json<glz::generic>(R"({"foo":"bar"})");
expect(bool(json));
expect(!json->contains("id"));
expect(json->contains("foo"));
auto obj = glz::read_json<std::map<std::string, std::string>>(json.value());
expect(bool(obj));
expect(obj->contains("foo"));
expect(obj->at("foo") == "bar");
};
"buffer underrun"_test = [] {
std::string buffer{"000000000000000000000"};
glz::generic json{};
expect(glz::read_json(json, buffer) == glz::error_code::parse_number_failure);
};
"generic copy construction"_test = [] {
std::string s{};
expect(not glz::write_json(glz::generic(*(glz::read_json<glz::generic>("{}"))), s));
expect(s == "{}");
};
"generic is_object"_test = [] {
glz::generic json{};
expect(not glz::read_json(json, "{}"));
expect(json.is_object());
expect(glz::is_object(json));
expect(json.empty());
expect(json.size() == 0);
expect(json.get_object().size() == 0);
};
"generic is_object"_test = [] {
glz::generic json{};
expect(not glz::read_json(json, R"({"age":"22","name":"Noah"})"));
expect(json.is_object());
expect(glz::is_object(json));
expect(not json.empty());
expect(json.size() == 2);
expect(json.get_object().size() == 2);
};
"generic is_array"_test = [] {
glz::generic json{};
expect(not glz::read_json(json, "[]"));
expect(json.is_array());
expect(glz::is_array(json));
expect(json.empty());
expect(json.size() == 0);
expect(json.get_array().size() == 0);
};
"generic is_array"_test = [] {
glz::generic json{};
expect(not glz::read_json(json, "[1,2,3]"));
expect(json.is_array());
expect(glz::is_array(json));
expect(not json.empty());
expect(json.size() == 3);
expect(json.get_array().size() == 3);
std::array<int, 3> v{};
expect(not glz::read<glz::opts{}>(v, json));
expect(v == std::array{1, 2, 3});
};
"generic is_string"_test = [] {
glz::generic json{};
expect(not glz::read_json(json, R"("")"));
expect(json.is_string());
expect(glz::is_string(json));
expect(json.empty());
expect(json.size() == 0);
expect(json.get_string() == "");
};
"generic is_string"_test = [] {
glz::generic json{};
expect(not glz::read_json(json, R"("Beautiful beginning")"));
expect(json.is_string());
expect(glz::is_string(json));
expect(not json.empty());
expect(json.size() == 19);
expect(json.get_string() == "Beautiful beginning");
std::string v{};
expect(not glz::read<glz::opts{}>(v, json));
expect(v == "Beautiful beginning");
};
"generic is_number"_test = [] {
glz::generic json{};
expect(not glz::read_json(json, "3.882e2"));
expect(json.is_number());
expect(glz::is_number(json));
expect(not json.empty());
expect(json.size() == 0);
expect(json.get_number() == 3.882e2);
double v{};
expect(not glz::read<glz::opts{}>(v, json));
expect(v == 3.882e2);
};
"generic is_boolean"_test = [] {
glz::generic json{};
expect(not glz::read_json(json, "true"));
expect(json.is_boolean());
expect(glz::is_boolean(json));
expect(not json.empty());
expect(json.size() == 0);
expect(json.get_boolean());
bool v{};
expect(not glz::read<glz::opts{}>(v, json));
expect(v);
};
"generic is_null"_test = [] {
glz::generic json{};
expect(not glz::read_json(json, "null"));
expect(json.is_null());
expect(glz::is_null(json));
expect(json.empty());
expect(json.size() == 0);
};
"generic garbage input"_test = [] {
glz::generic json{};
expect(glz::read_json(json, "\x22\x5c\x75\xff\x22"));
};
"generic string_view"_test = [] {
glz::generic json = std::string_view{"Hello"};
expect(glz::write_json(json).value() == R"("Hello")");
json = std::string_view{"World"};
expect(glz::write_json(json).value() == R"("World")");
};
"generic int"_test = [] {
glz::generic json = 55;
expect(glz::write_json(json).value() == "55");
json = 44;
expect(glz::write_json(json).value() == "44");
};
"generic const char*"_test = [] {
glz::generic j{};
j["some key"] = "some value";
expect(j.dump() == R"({"some key":"some value"})");
};
};
glz::generic create_test_json()
{
glz::generic root;
// Create nested object structure
root["user"]["name"] = "John Doe";
root["user"]["age"] = 30.0;
root["user"]["active"] = true;
root["user"]["address"]["street"] = "123 Main St";
root["user"]["address"]["city"] = "Anytown";
root["user"]["address"]["zip"] = "12345";
// Create array structure
root["items"] = glz::generic::array_t{};
auto& items = root["items"].get_array();
glz::generic item1;
item1["id"] = 1.0;
item1["name"] = "Widget A";
item1["price"] = 19.99;
items.push_back(std::move(item1));
glz::generic item2;
item2["id"] = 2.0;
item2["name"] = "Widget B";
item2["price"] = 29.99;
items.push_back(std::move(item2));
glz::generic item3;
item3["id"] = 3.0;
item3["name"] = "Widget C";
item3["price"] = 39.99;
items.push_back(std::move(item3));
// Create mixed nested structure
root["metadata"]["version"] = "1.0";
root["metadata"]["tags"] = glz::generic::array_t{"production", "stable", "v1"};
return root;
}
suite json_pointer_extraction_tests = [] {
"seek_extract_string"_test = [] {
auto json = create_test_json();
std::string result;
bool found = glz::seek(
[&](auto&& val) {
if constexpr (std::same_as<std::decay_t<decltype(val)>, std::string>) {
result = val;
}
},
json, "/user/name");
expect(found) << "Should find user name\n";
expect(result == "John Doe") << "Should extract correct user name\n";
};
"seek_extract_number"_test = [] {
auto json = create_test_json();
double age = 0.0;
bool found = glz::seek(
[&](auto&& val) {
if constexpr (std::same_as<std::decay_t<decltype(val)>, double>) {
age = val;
}
},
json, "/user/age");
expect(found) << "Should find user age\n";
expect(age == 30.0) << "Should extract correct age\n";
};
"seek_extract_boolean"_test = [] {
auto json = create_test_json();
bool active = false;
bool found = glz::seek(
[&](auto&& val) {
if constexpr (std::same_as<std::decay_t<decltype(val)>, bool>) {
active = val;
}
},
json, "/user/active");
expect(found) << "Should find user active status\n";
expect(active == true) << "Should extract correct active status\n";
};
"get_string_reference"_test = [] {
auto json = create_test_json();
auto name_ref = glz::get<std::string>(json, "/user/name");
expect(name_ref.has_value()) << "Should successfully get string reference\n";
expect(name_ref->get() == "John Doe") << "Should get correct name value\n";
};
"get_number_reference"_test = [] {
auto json = create_test_json();
auto age_ref = glz::get<double>(json, "/user/age");
expect(age_ref.has_value()) << "Should successfully get number reference\n";
expect(age_ref->get() == 30.0) << "Should get correct age value\n";
};
"get_boolean_reference"_test = [] {
auto json = create_test_json();
auto active_ref = glz::get<bool>(json, "/user/active");
expect(active_ref.has_value()) << "Should successfully get boolean reference\n";
expect(active_ref->get() == true) << "Should get correct active value\n";
};
"get_nested_string"_test = [] {
auto json = create_test_json();
auto city_ref = glz::get<std::string>(json, "/user/address/city");
expect(city_ref.has_value()) << "Should successfully get nested string\n";
expect(city_ref->get() == "Anytown") << "Should get correct city value\n";
};
"get_array_element_string"_test = [] {
auto json = create_test_json();
auto item_name_ref = glz::get<std::string>(json, "/items/1/name");
expect(item_name_ref.has_value()) << "Should successfully get array element string\n";
expect(item_name_ref->get() == "Widget B") << "Should get correct item name\n";
};
"get_array_element_number"_test = [] {
auto json = create_test_json();
auto item_id_ref = glz::get<double>(json, "/items/0/id");
expect(item_id_ref.has_value()) << "Should successfully get array element number\n";
expect(item_id_ref->get() == 1.0) << "Should get correct item id\n";
auto item_price_ref = glz::get<double>(json, "/items/2/price");
expect(item_price_ref.has_value()) << "Should successfully get item price\n";
expect(item_price_ref->get() == 39.99) << "Should get correct item price\n";
};
"get_if_string_success"_test = [] {
auto json = create_test_json();
auto name_ptr = glz::get_if<std::string>(json, "/user/name");
expect(name_ptr != nullptr) << "Should get valid pointer to string\n";
expect(*name_ptr == "John Doe") << "Should get correct name value\n";
};
"get_if_number_success"_test = [] {
auto json = create_test_json();
auto age_ptr = glz::get_if<double>(json, "/user/age");
expect(age_ptr != nullptr) << "Should get valid pointer to number\n";
expect(*age_ptr == 30.0) << "Should get correct age value\n";
};
"get_if_failure_wrong_type"_test = [] {
auto json = create_test_json();
// Try to get string as number
auto wrong_type_ptr = glz::get_if<double>(json, "/user/name");
expect(wrong_type_ptr == nullptr) << "Should get null pointer for wrong type\n";
// Try to get number as string
auto wrong_type_ptr2 = glz::get_if<std::string>(json, "/user/age");
expect(wrong_type_ptr2 == nullptr) << "Should get null pointer for wrong type\n";
};
"get_if_failure_invalid_path"_test = [] {
auto json = create_test_json();
auto invalid_ptr = glz::get_if<std::string>(json, "/nonexistent/path");
expect(invalid_ptr == nullptr) << "Should get null pointer for invalid path\n";
auto out_of_bounds_ptr = glz::get_if<double>(json, "/items/999/id");
expect(out_of_bounds_ptr == nullptr) << "Should get null pointer for out of bounds array access\n";
};
"get_value_copy"_test = [] {
auto json = create_test_json();
auto name_copy = glz::get_value<std::string>(json, "/user/name");
expect(name_copy.has_value()) << "Should successfully copy string value\n";
expect(*name_copy == "John Doe") << "Should get correct copied name\n";
auto age_copy = glz::get_value<double>(json, "/user/age");
expect(age_copy.has_value()) << "Should successfully copy number value\n";
expect(*age_copy == 30.0) << "Should get correct copied age\n";
};
"set_string_value"_test = [] {
auto json = create_test_json();
bool success = glz::set(json, "/user/name", std::string{"Jane Smith"});
expect(success) << "Should successfully set string value\n";
auto updated_name = glz::get<std::string>(json, "/user/name");
expect(updated_name.has_value()) << "Should be able to retrieve updated value\n";
expect(updated_name->get() == "Jane Smith") << "Should have updated name\n";
};
"set_number_value"_test = [] {
auto json = create_test_json();
bool success = glz::set(json, "/user/age", 35.0);
expect(success) << "Should successfully set number value\n";
auto updated_age = glz::get<double>(json, "/user/age");
expect(updated_age.has_value()) << "Should be able to retrieve updated value\n";
expect(updated_age->get() == 35.0) << "Should have updated age\n";
};
"set_boolean_value"_test = [] {
auto json = create_test_json();
bool success = glz::set(json, "/user/active", false);
expect(success) << "Should successfully set boolean value\n";
auto updated_active = glz::get<bool>(json, "/user/active");
expect(updated_active.has_value()) << "Should be able to retrieve updated value\n";
expect(updated_active->get() == false) << "Should have updated active status\n";
};
"array_of_primitives_access"_test = [] {
auto json = create_test_json();
auto first_tag = glz::get<std::string>(json, "/metadata/tags/0");
expect(first_tag.has_value()) << "Should get reference to first tag\n";
expect(first_tag->get() == "production") << "Should get correct first tag\n";
auto second_tag = glz::get<std::string>(json, "/metadata/tags/1");
expect(second_tag.has_value()) << "Should get reference to second tag\n";
expect(second_tag->get() == "stable") << "Should get correct second tag\n";
auto last_tag = glz::get<std::string>(json, "/metadata/tags/2");
expect(last_tag.has_value()) << "Should get reference to last tag\n";
expect(last_tag->get() == "v1") << "Should get correct last tag\n";
};
"json_pointer_escaping"_test = [] {
glz::generic json;
json["key~with~tilde"] = "tilde value";
json["key/with/slash"] = "slash value";
// Test tilde escaping (~0 for ~)
auto tilde_value = glz::get<std::string>(json, "/key~0with~0tilde");
expect(tilde_value.has_value()) << "Should handle tilde escaping\n";
expect(tilde_value->get() == "tilde value") << "Should get correct tilde value\n";
// Test slash escaping (~1 for /)
auto slash_value = glz::get<std::string>(json, "/key~1with~1slash");
expect(slash_value.has_value()) << "Should handle slash escaping\n";
expect(slash_value->get() == "slash value") << "Should get correct slash value\n";
};
"type_mismatch_errors"_test = [] {
auto json = create_test_json();
// Try to get string as bool
auto wrong_bool = glz::get<bool>(json, "/user/name");
expect(not wrong_bool.has_value()) << "Should fail when requesting string as bool\n";
// Try to get number as string
auto wrong_string = glz::get<std::string>(json, "/user/age");
expect(not wrong_string.has_value()) << "Should fail when requesting number as string\n";
// Try to get object as primitive
auto wrong_primitive = glz::get<double>(json, "/user");
expect(not wrong_primitive.has_value()) << "Should fail when requesting object as primitive\n";
};
"const_json_access"_test = [] {
const auto json = create_test_json();
// Test const access
auto name = glz::get<std::string>(json, "/user/name");
expect(name.has_value()) << "Should get string from const json\n";
expect(name->get() == "John Doe") << "Should get correct value from const json\n";
auto age = glz::get<double>(json, "/user/age");
expect(age.has_value()) << "Should get number from const json\n";
expect(age->get() == 30.0) << "Should get correct age from const json\n";
};
"simple_usage_example"_test = [] {
glz::generic json{{"test", true}};
auto result = glz::get<bool>(json, "/test");
expect(result.has_value()) << "Should successfully get boolean value\n";
expect(result->get() == true) << "Should get correct boolean value\n";
// Also test get_if
auto bool_ptr = glz::get_if<bool>(json, "/test");
expect(bool_ptr != nullptr) << "Should get valid pointer\n";
expect(*bool_ptr == true) << "Should get correct value via pointer\n";
};
};
// Define structs at namespace level for linkage requirements
struct Thing
{
int value1;
std::string value2;
};
struct Person
{
std::string name;
int age;
std::vector<std::string> hobbies;
};
struct Address
{
std::string street;
std::string city;
int zip;
};
struct Employee
{
std::string name;
Address address;
double salary;
};
suite struct_assignment_tests = [] {
"struct_to_generic_assignment"_test = [] {
Thing t{42, "hello, world!"};
glz::generic document;
document["some"]["nested"]["key"] = t;
auto json_str = document.dump();
expect(json_str.has_value()) << "Should successfully dump generic to string";
auto expected = R"({"some":{"nested":{"key":{"value1":42,"value2":"hello, world!"}}}})";
expect(json_str.value() == expected) << "Should produce correct nested JSON structure";
// Also verify we can access the values
expect(document["some"]["nested"]["key"]["value1"].get<double>() == 42.0);
expect(document["some"]["nested"]["key"]["value2"].get<std::string>() == "hello, world!");
};
"complex_struct_assignment"_test = [] {
Person p{"Alice", 30, {"reading", "gaming", "cooking"}};
glz::generic json;
json["person"] = p;
auto json_str = json.dump();
expect(json_str.has_value()) << "Should successfully serialize complex struct";
// Verify the structure
expect(json["person"]["name"].get<std::string>() == "Alice");
expect(json["person"]["age"].get<double>() == 30.0);
expect(json["person"]["hobbies"][0].get<std::string>() == "reading");
expect(json["person"]["hobbies"][1].get<std::string>() == "gaming");
expect(json["person"]["hobbies"][2].get<std::string>() == "cooking");
};
"nested_struct_assignment"_test = [] {
Employee e{"Bob", {"123 Main St", "Anytown", 12345}, 75000.50};
glz::generic json;
json["employee"] = e;
auto json_str = json.dump();
expect(json_str.has_value()) << "Should successfully serialize nested struct";
// Verify nested structure
expect(json["employee"]["name"].get<std::string>() == "Bob");
expect(json["employee"]["address"]["street"].get<std::string>() == "123 Main St");
expect(json["employee"]["address"]["city"].get<std::string>() == "Anytown");
expect(json["employee"]["address"]["zip"].get<double>() == 12345.0);
expect(json["employee"]["salary"].get<double>() == 75000.50);
};
};
// Tests for issue #1807: glz::get<T> with container types
// https://github.com/stephenberry/glaze/issues/1807
suite issue_1807_tests = [] {
"get_vector_from_generic"_test = [] {
std::string buffer = R"({"test0": false, "test1": ["alice", "bob"]})";
glz::generic json{};
expect(!glz::read_json(json, buffer));
// These should work
auto test0_result = glz::get<bool>(json, "/test0");
expect(test0_result.has_value()) << "Should get bool value";
expect(*test0_result == false) << "Should get correct bool value";
// This should work now with the new overload
auto test1_result = glz::get<std::vector<std::string>>(json, "/test1");
expect(test1_result.has_value()) << "Should get vector<string> value";
if (test1_result.has_value()) {
auto& vec = *test1_result;
expect(vec.size() == 2) << "Vector should have 2 elements";
expect(vec[0] == "alice") << "First element should be 'alice'";
expect(vec[1] == "bob") << "Second element should be 'bob'";
}
// Individual element access works (returns reference wrapper)
auto elem0_result = glz::get<std::string>(json, "/test1/0");
expect(elem0_result.has_value()) << "Should get first array element";
expect(elem0_result->get() == "alice") << "First element should be 'alice'";
};
"get_map_from_generic"_test = [] {
std::string buffer = R"({"test0": false, "test1": {"0": "alice", "1": "bob"}})";
glz::generic json{};
expect(!glz::read_json(json, buffer));
auto test0_result = glz::get<bool>(json, "/test0");
expect(test0_result.has_value()) << "Should get bool value";
// This should work now with the new overload
auto test1_result = glz::get<std::map<std::string, std::string>>(json, "/test1");
expect(test1_result.has_value()) << "Should get map<string, string> value";
if (test1_result.has_value()) {
auto& map = *test1_result;
expect(map.size() == 2) << "Map should have 2 elements";
expect(map.at("0") == "alice") << "First element should be 'alice'";
expect(map.at("1") == "bob") << "Second element should be 'bob'";
}
// Individual element access works (returns reference wrapper)
auto elem0_result = glz::get<std::string>(json, "/test1/0");
expect(elem0_result.has_value()) << "Should get map element";
expect(elem0_result->get() == "alice") << "Element should be 'alice'";
};
"get_array_from_generic"_test = [] {
std::string buffer = R"({"items": [1, 2, 3, 4, 5]})";
glz::generic json{};
expect(!glz::read_json(json, buffer));
// This should work now with the new overload
auto result = glz::get<std::array<int, 5>>(json, "/items");
expect(result.has_value()) << "Should get array<int, 5> value";
if (result.has_value()) {
auto& arr = *result;
expect(arr[0] == 1) << "First element should be 1";
expect(arr[4] == 5) << "Last element should be 5";
}
};
"get_list_from_generic"_test = [] {
std::string buffer = R"({"tags": ["tag1", "tag2", "tag3"]})";
glz::generic json{};
expect(!glz::read_json(json, buffer));
// This should work now with the new overload
auto result = glz::get<std::list<std::string>>(json, "/tags");
expect(result.has_value()) << "Should get list<string> value";
if (result.has_value()) {
auto& list = *result;
expect(list.size() == 3) << "List should have 3 elements";
auto it = list.begin();
expect(*it == "tag1") << "First element should be 'tag1'";
}
};
"get_nested_containers"_test = [] {
std::string buffer = R"({
"matrix": [[1, 2], [3, 4], [5, 6]],
"table": {"row1": [10, 20], "row2": [30, 40]}
})";
glz::generic json{};
expect(!glz::read_json(json, buffer));
// Test vector of vectors
auto matrix = glz::get<std::vector<std::vector<int>>>(json, "/matrix");
expect(matrix.has_value()) << "Should get nested vector";
if (matrix.has_value()) {
expect(matrix->size() == 3) << "Matrix should have 3 rows";
expect((*matrix)[0].size() == 2) << "First row should have 2 elements";
expect((*matrix)[0][0] == 1) << "First element should be 1";
expect((*matrix)[2][1] == 6) << "Last element should be 6";
}
// Test map of vectors
auto table = glz::get<std::map<std::string, std::vector<int>>>(json, "/table");
expect(table.has_value()) << "Should get map of vectors";
if (table.has_value()) {
expect(table->size() == 2) << "Table should have 2 rows";
expect(table->at("row1").size() == 2) << "Row1 should have 2 elements";
expect(table->at("row1")[0] == 10) << "First element of row1 should be 10";
expect(table->at("row2")[1] == 40) << "Last element of row2 should be 40";
}
};
"get_empty_containers"_test = [] {
std::string buffer = R"({
"empty_array": [],
"empty_object": {}
})";
glz::generic json{};
expect(!glz::read_json(json, buffer));
// Test empty vector
auto empty_vec = glz::get<std::vector<int>>(json, "/empty_array");
expect(empty_vec.has_value()) << "Should get empty vector";
if (empty_vec.has_value()) {
expect(empty_vec->empty()) << "Vector should be empty";
}
// Test empty map
auto empty_map = glz::get<std::map<std::string, int>>(json, "/empty_object");
expect(empty_map.has_value()) << "Should get empty map";
if (empty_map.has_value()) {
expect(empty_map->empty()) << "Map should be empty";
}
};
"get_integer_conversion"_test = [] {
std::string buffer = R"({"numbers": [1.0, 2.5, 3.7, 4.9]})";
glz::generic json{};
expect(!glz::read_json(json, buffer));
// Test conversion from double to int
auto numbers = glz::get<std::vector<int>>(json, "/numbers");
expect(numbers.has_value()) << "Should get vector<int> from doubles";
if (numbers.has_value()) {
expect(numbers->size() == 4) << "Should have 4 elements";
expect((*numbers)[0] == 1) << "First element should be 1";
expect((*numbers)[1] == 2) << "Second element should be 2 (truncated)";
expect((*numbers)[2] == 3) << "Third element should be 3 (truncated)";
expect((*numbers)[3] == 4) << "Fourth element should be 4 (truncated)";
}
};
"get_error_wrong_type"_test = [] {
std::string buffer = R"({"value": "not a number", "data": {"nested": true}})";
glz::generic json{};
expect(!glz::read_json(json, buffer));
// Try to get string as vector
auto wrong_vec = glz::get<std::vector<int>>(json, "/value");
expect(!wrong_vec.has_value()) << "Should fail to get string as vector";
// Try to get object as vector
auto wrong_vec2 = glz::get<std::vector<int>>(json, "/data");
expect(!wrong_vec2.has_value()) << "Should fail to get object as vector";
// Try to get object as map with wrong value type
auto wrong_map = glz::get<std::map<std::string, int>>(json, "/data");
expect(!wrong_map.has_value()) << "Should fail to convert boolean to int";
};
"get_error_nonexistent_path"_test = [] {
std::string buffer = R"({"data": [1, 2, 3]})";
glz::generic json{};
expect(!glz::read_json(json, buffer));
// Try to access non-existent path
auto result = glz::get<std::vector<int>>(json, "/nonexistent");
expect(!result.has_value()) << "Should fail to get non-existent path";
// Try to access out of bounds array index
auto result2 = glz::get<std::vector<int>>(json, "/data/10");
expect(!result2.has_value()) << "Should fail to get out of bounds index";
};
"get_deque_container"_test = [] {
std::string buffer = R"({"queue": [10, 20, 30, 40]})";
glz::generic json{};
expect(!glz::read_json(json, buffer));
auto result = glz::get<std::deque<int>>(json, "/queue");
expect(result.has_value()) << "Should get deque<int> value";
if (result.has_value()) {
auto& dq = *result;
expect(dq.size() == 4) << "Deque should have 4 elements";
expect(dq.front() == 10) << "Front should be 10";
expect(dq.back() == 40) << "Back should be 40";
}
};
"convert_from_generic_lowlevel_api"_test = [] {
// Test the lower-level convert_from_generic API directly
glz::generic arr_json{};
expect(!glz::read_json(arr_json, R"([1, 2, 3, 4, 5])"));
std::vector<int> vec;
auto ec = glz::convert_from_generic(vec, arr_json);
expect(!ec) << "Should convert array to vector";
expect(vec.size() == 5) << "Vector should have 5 elements";
expect(vec[0] == 1 && vec[4] == 5) << "Elements should be correct";
glz::generic obj_json{};
expect(!glz::read_json(obj_json, R"({"a": 1, "b": 2})"));
std::map<std::string, int> map;
ec = glz::convert_from_generic(map, obj_json);
expect(!ec) << "Should convert object to map";
expect(map.size() == 2) << "Map should have 2 elements";
expect(map["a"] == 1 && map["b"] == 2) << "Elements should be correct";
// Test primitive conversion
glz::generic num_json{};
expect(!glz::read_json(num_json, "42.5"));
int num{};
ec = glz::convert_from_generic(num, num_json);
expect(!ec) << "Should convert number to int";
expect(num == 42) << "Number should be 42";
};
"large_container_conversion"_test = [] {
// Build a large array
std::string buffer = "[";
for (int i = 0; i < 1000; ++i) {
if (i > 0) buffer += ",";
buffer += std::to_string(i);
}
buffer += "]";
glz::generic json{};
expect(!glz::read_json(json, buffer));
// Convert to vector - tests performance of direct traversal
std::vector<int> result;
auto ec = glz::convert_from_generic(result, json);
expect(!ec) << "Should convert large array";
expect(result.size() == 1000) << "Should have 1000 elements";
expect(result[0] == 0) << "First element should be 0";
expect(result[999] == 999) << "Last element should be 999";
};
};
// Tests for optimized read_json from generic
suite optimized_read_json_tests = [] {
"read_json_vector_optimized"_test = [] {
glz::generic json{};
expect(!glz::read_json(json, R"([1, 2, 3, 4, 5])"));
// This should use the optimized direct-traversal path
std::vector<int> vec;
expect(!glz::read_json(vec, json));
expect(vec.size() == 5) << "Vector should have 5 elements";
expect(vec[0] == 1 && vec[4] == 5) << "Elements should be correct";
};
"read_json_map_optimized"_test = [] {
glz::generic json{};
expect(!glz::read_json(json, R"({"a": 10, "b": 20})"));
// This should use the optimized direct-traversal path
std::map<std::string, int> map;
expect(!glz::read_json(map, json));
expect(map.size() == 2) << "Map should have 2 elements";
expect(map["a"] == 10 && map["b"] == 20) << "Elements should be correct";
};
"read_json_primitive_optimized"_test = [] {
glz::generic num_json{};
expect(!glz::read_json(num_json, "42.5"));
int num{};
expect(!glz::read_json(num, num_json));
expect(num == 42) << "Should convert double to int";
glz::generic str_json{};
expect(!glz::read_json(str_json, R"("hello world")"));
std::string str;
expect(!glz::read_json(str, str_json));
expect(str == "hello world") << "Should get string value";
};
"read_json_expected_form_optimized"_test = [] {
glz::generic json{};
expect(!glz::read_json(json, R"([10, 20, 30])"));
// Test the expected<T> form
auto vec_result = glz::read_json<std::vector<int>>(json);
expect(vec_result.has_value()) << "Should successfully convert";
if (vec_result.has_value()) {
expect(vec_result->size() == 3) << "Should have 3 elements";
expect((*vec_result)[1] == 20) << "Middle element should be 20";
}
};
"read_json_nested_containers_optimized"_test = [] {
glz::generic json{};
expect(!glz::read_json(json, R"([[1, 2], [3, 4], [5, 6]])"));
std::vector<std::vector<int>> matrix;
expect(!glz::read_json(matrix, json));
expect(matrix.size() == 3) << "Should have 3 rows";
expect(matrix[0].size() == 2) << "Each row should have 2 elements";
expect(matrix[2][1] == 6) << "Last element should be 6";
};
"read_json_struct_still_works"_test = [] {
// Test that structs still work (using JSON round-trip)
glz::generic json{};
expect(!glz::read_json(json, R"({"name":"Alice","age":30,"hobbies":["reading","gaming"]})"));
Person person;
expect(!glz::read_json(person, json));
expect(person.name == "Alice") << "Name should be correct";
expect(person.age == 30) << "Age should be correct";
expect(person.hobbies.size() == 2) << "Should have 2 hobbies";
};
"read_with_opts_optimized"_test = [] {
glz::generic json{};
expect(!glz::read_json(json, R"([1, 2, 3])"));
// Test the templated read<Opts> function
std::vector<int> vec;
expect(!glz::read<glz::opts{}>(vec, json));
expect(vec.size() == 3) << "Vector should have 3 elements";
expect(vec[0] == 1) << "First element should be 1";
};
};
suite generic_custom_reader_tests = [] {
// Tests for handling null values with custom readers using glz::generic
"generic custom reader with null"_test = [] {
generic_optional obj{};
obj.val = 'x';
std::string json = R"({"val":null})";
auto ec = glz::read_json(obj, json);
expect(!ec) << glz::format_error(ec, json);
expect(!obj.val.has_value());
};
"generic custom reader with string"_test = [] {
generic_optional obj{};
std::string json = R"({"val":"abc"})";
auto ec = glz::read_json(obj, json);
expect(!ec) << glz::format_error(ec, json);
expect(obj.val.has_value());
expect(obj.val.value() == 'a');
};
"generic custom reader with empty string"_test = [] {
generic_optional obj{};
obj.val = 'x';
std::string json = R"({"val":""})";
auto ec = glz::read_json(obj, json);
expect(!ec) << glz::format_error(ec, json);
expect(!obj.val.has_value());
};
"generic custom writer with null"_test = [] {
generic_optional obj{};
obj.val = std::nullopt;
std::string buffer;
auto ec = glz::write_json(obj, buffer);
expect(!ec);
expect(buffer == R"({"val":null})");
};
"generic custom writer with value"_test = [] {
generic_optional obj{};
obj.val = 'z';
std::string buffer;
auto ec = glz::write_json(obj, buffer);
expect(!ec);
expect(buffer == R"({"val":"z"})");
};
"generic custom reader roundtrip"_test = [] {
generic_optional obj{};
obj.val = 'k';
std::string buffer;
auto ec_write = glz::write_json(obj, buffer);
expect(!ec_write);
generic_optional obj2{};
auto ec_read = glz::read_json(obj2, buffer);
expect(!ec_read) << glz::format_error(ec_read, buffer);
expect(obj2.val.has_value());
expect(obj2.val.value() == 'k');
};
"generic custom reader int with null"_test = [] {
generic_optional_int obj{};
obj.num = 42;
std::string json = R"({"num":null})";
auto ec = glz::read_json(obj, json);
expect(!ec) << glz::format_error(ec, json);
expect(!obj.num.has_value());
};
"generic custom reader int with number"_test = [] {
generic_optional_int obj{};
std::string json = R"({"num":123})";
auto ec = glz::read_json(obj, json);
expect(!ec) << glz::format_error(ec, json);
expect(obj.num.has_value());
expect(obj.num.value() == 123);
};
"generic custom reader int with string"_test = [] {
generic_optional_int obj{};
std::string json = R"({"num":"456"})";
auto ec = glz::read_json(obj, json);
expect(!ec) << glz::format_error(ec, json);
expect(obj.num.has_value());
expect(obj.num.value() == 456);
};
};
suite fuzz_tests = [] {
"fuzz1"_test = [] {
std::string_view s = "[true,true,tur";
std::vector<char> buffer{s.data(), s.data() + s.size()};
buffer.push_back('\0');
glz::generic json{};
auto ec = glz::read_json(json, buffer);
expect(bool(ec));
};
};
int main() { return 0; }
|