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 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
|
# Wrappers
Glaze provides a number of wrappers that indicate at compile time how a value should be read and/or written. These wrappers allow you to modify the read/write behavior of a type without affecting your C++ class.
## Available Wrappers
```c++
glz::append_arrays<&T::x> // When reading into an array that is appendable, the new data will be appended rather than overwrite
glz::bools_as_numbers<&T::x> // Read and write booleans as numbers
glz::cast<&T::x, CastType> // Casts a value to and from the CastType, which is parsed/serialized
glz::quoted_num<&T::x> // Read and write numbers as strings
glz::quoted<&T::x> // Read a value as a string and unescape, to avoid the user having to parse twice
glz::number<&T::x> // Read a string as a number and writes the string as a number
glz::raw<&T::x> // Write out string like types without quotes
glz::raw_string<&T::string> // Do not decode/encode escaped characters for strings (improves read/write performance)
glz::escaped<&T::string> // Opposite of glz::raw_string, it turns off this behavior
glz::escape_bytes_t<T> // A wrapper type for local use to treat char array or vector as byte sequence to be fully escaped (prevents null termination truncation)
glz::escape_bytes<&T::x> // For meta usage: treats char array or vector as byte sequence to be fully escaped (prevents null termination truncation)
glz::read_constraint<&T::x, constraint_function, "Message"> // Applies a constraint function when reading
glz::max_length<&T::x, N> // Limits string length or array size to N when reading (BEVE format)
glz::partial_read<&T::x> // Reads into only existing fields and elements and then exits without parsing the rest of the input
glz::invoke<&T::func> // Invoke a std::function, lambda, or member function with n-arguments as an array input
glz::write_float32<&T::x> // Writes out numbers with a maximum precision of float32_t
glz::write_float64<&T::x> // Writes out numbers with a maximum precision of float64_t
glz::write_float_full<&T::x> // Writes out numbers with full precision (turns off higher level float precision wrappers)
glz::float_format<&T::x, "{:.2f}"> // Format floats using std::format syntax (C++23)
glz::custom<&T::read, &T::write> // Calls custom read and write std::functions, lambdas, or member functions
glz::manage<&T::x, &T::read_x, &T::write_x> // Calls read_x() after reading x and calls write_x() before writing x
glz::as_array<&T::member> // Treat a reflected/member-annotated type as a positional array for read and write
```
## Associated glz::opts
`glz::opts` is the compile time options struct passed to most of Glaze functions to configure read/write behavior. Often wrappers are associated with compile time options and can also be set via `glz::opts`. For example, the `glz::quoted_num` wrapper is associated with the `quoted_num` boolean in `glz::opts`.
## append_arrays
When reading into an array that is appendable, the new data will be appended rather than overwrite
Associated option: add `bool append_arrays = true;` to a custom options struct (for example,
`struct append_arrays_opts : glz::opts { bool append_arrays = true; };`).
```c++
struct append_obj
{
std::vector<std::string> names{};
std::vector<std::array<int, 2>> arrays{};
};
template <>
struct glz::meta<append_obj>
{
using T = append_obj;
static constexpr auto value = object("names", append_arrays<&T::names>, "arrays", append_arrays<&T::arrays>);
};
```
In use:
```c++
append_obj obj{};
expect(not glz::read_json(obj, R"({"names":["Bob"],"arrays":[[0,0]]})"));
expect(obj.names == std::vector<std::string>{"Bob"});
expect(obj.arrays == std::vector<std::array<int, 2>>{{0,0}});
expect(not glz::read_json(obj, R"({"names":["Liz"],"arrays":[[1,1]]})"));
expect(obj.names == std::vector<std::string>{"Bob", "Liz"});
expect(obj.arrays == std::vector<std::array<int, 2>>{{0,0},{1,1}});
```
## bools_as_numbers
Read and write booleans as numbers
Associated option: add `bool bools_as_numbers = true;` to a custom options struct (for example,
`struct bools_as_numbers_opts : glz::opts { bool bools_as_numbers = true; };`).
```c++
struct bools_as_numbers_struct
{
bool a{};
bool b{};
bool c{};
bool d{};
struct glaze {
using T = bools_as_numbers_struct;
static constexpr auto value = glz::object("a", glz::bools_as_numbers<&T::a>, "b", glz::bools_as_numbers<&T::b>, &T::c, &T::d);
};
};
```
In use:
```c++
std::string s = R"({"a":1,"b":0,"c":true,"d":false})";
bools_as_numbers_struct obj{};
expect(!glz::read_json(obj, s));
expect(obj.a == true);
expect(obj.b == false);
expect(glz::write_json(obj) == s);
```
### bools_as_numbers from glz::opts
You don't have to use wrappers if you want the global behavior to handle booleans as numbers.
```c++
std::string s = R"([1,0,1,0])";
std::array<bool, 4> obj{};
struct bools_as_numbers_opts : glz::opts
{
bool bools_as_numbers = true;
};
constexpr bools_as_numbers_opts opts{};
expect(!glz::read<opts>(obj, s));
expect(glz::write<opts>(obj) == s);
```
## cast
`glz::cast` is a simple wrapper that will serialize and deserialize the cast type rather than underlying type. This enables the user to parse JSON for a floating point value into an integer, or perform similar `static_cast` behaviors.
```c++
struct cast_obj
{
int integer{};
};
template <>
struct glz::meta<cast_obj>
{
using T = cast_obj;
static constexpr auto value = object("integer", cast<&T::integer, double>);
};
```
In use:
```c++
cast_obj obj{};
std::string buffer = R"({"integer":5.7})";
expect(not glz::read_json(obj, buffer));
expect(obj.integer == 5);
```
When a cast-backed type is used as a key in a map (for example `std::map<MyId, T>`), BEVE now recognises the wrapper and emits the same header as the cast target. This means strong-ID wrappers can be reused across JSON, TOML, and BEVE without specialising custom read/write logic.
## quoted_num
Read and write numbers as strings.
Associated option: `glz::opts{.quoted_num = true};`
```c++
struct foo {
int x{};
};
template <>
struct glz::meta<foo> {
using T = foo;
static constexpr auto value = object("x", quoted_num<&T::x>);
};
```
In use:
```c++
std::string input = R"({ "x": "5" })";
foo obj{};
expect(!glz::read_json(obj, input));
expect(glz::write_json(obj) == R"({ "x": "5" })");
```
> `quoted_num` is more efficient than `quoted` for numbers.
## quoted
When reading, first reads a value as a string, which unescapes, and then reads the value normally. When writing, will first write the value as a string and then write the string to produce escapes.
> `glz::quoted` is useful for storing escaped JSON inside of a higher level JSON object.
```c++
struct client_state
{
uint64_t id{};
std::map<std::string, std::vector<std::string>> layouts{};
};
template <>
struct glz::meta<client_state>
{
using T = client_state;
static constexpr auto value = object("id", &T::id, "layouts", quoted<&T::layouts>);
};
```
In use:
```c++
client_state obj{};
std::string input = R"({
"id": 4848,
"layouts": "{\"first layout\": [ \"inner1\", \"inner2\" ] }"
})";
expect(!glz::read_json(obj, input));
expect(obj.id == 4848);
expect(obj.layouts.at("first layout") == std::vector<std::string>{"inner1", "inner2"});
std::string out{};
glz::write_json(obj, out);
expect(out == R"({"id":4848,"layouts":"{\"first layout\":[\"inner1\",\"inner2\"]}"})");
```
## as_array
Convert a positional JSON array into an existing struct while writing back out as an array. Use `glz::as_array<&T::member>` when declaring the member in `glz::object`. Handy when a service sends compact arrays but your C++ type is a struct in memory.
```c++
struct Person_details
{
std::string_view name;
std::string_view surname;
std::string_view city;
std::string_view street;
};
struct Person
{
int id{};
Person_details person{};
};
template <>
struct glz::meta<Person>
{
using T = Person;
static constexpr auto value = glz::object(
"id", &T::id,
"person", glz::as_array<&T::person>
);
};
std::string payload = R"({
"id": 1,
"person": ["Joe", "Doe", "London", "Chamber St"]
})";
Person p{};
expect(!glz::read_json(p, payload));
expect(p.person.city == "London");
auto written = glz::write_json(p).value();
expect(written ==
R"({"id":1,"person":["Joe","Doe","London","Chamber St"]})"
);
```
## number
Read JSON numbers into strings and write strings as JSON numbers.
Associated option: `glz::opts{.number = true};`
```c++
struct numbers_as_strings
{
std::string x{};
std::string y{};
};
template <>
struct glz::meta<numbers_as_strings>
{
using T = numbers_as_strings;
static constexpr auto value = object("x", glz::number<&T::x>, "y", glz::number<&T::y>);
};
```
In use:
```c++
std::string input = R"({"x":555,"y":3.14})";
numbers_as_strings obj{};
expect(!glz::read_json(obj, input));
expect(obj.x == "555");
expect(obj.y == "3.14");
std::string output;
glz::write_json(obj, output);
expect(input == output);
```
## raw
Write out string like types without quotes.
> Useful for when a string is already in JSON format and doesn't need to be quoted.
Associated option: `glz::opts{.raw = true};`
```c++
struct raw_struct
{
std::string str{};
};
template <>
struct glz::meta<raw_struct>
{
using T = raw_struct;
static constexpr auto value = object("str", glz::raw<&T::str>);
};
```
In use:
```c++
suite raw_test = [] {
raw_struct obj{.str = R"("Hello")"};
// quotes would have been escaped if str were not wrapped with raw
expect(glz::write_json(obj) == R"({"str":"Hello"})");
};
```
## raw_string
Do not decode/encode escaped characters for strings (improves read/write performance).
> If your code does not care about decoding escaped characters or you know your input will never have escaped characters, this wrapper makes reading/writing that string faster.
Associated option: `glz::opts{.raw_string = true};`
```c++
struct raw_stuff
{
std::string a{};
std::string b{};
std::string c{};
struct glaze
{
using T = raw_stuff;
static constexpr auto value = glz::object(&T::a, &T::b, &T::c);
};
};
struct raw_stuff_wrapper
{
raw_stuff data{};
struct glaze
{
using T = raw_stuff_wrapper;
static constexpr auto value{glz::raw_string<&T::data>};
};
};
```
In use:
```c++
raw_stuff_wrapper obj{};
std::string buffer = R"({"a":"Hello\nWorld","b":"Hello World","c":"\tHello\bWorld"})";
expect(!glz::read_json(obj, buffer));
expect(obj.data.a == R"(Hello\nWorld)");
expect(obj.data.b == R"(Hello World)");
expect(obj.data.c == R"(\tHello\bWorld)");
buffer.clear();
glz::write_json(obj, buffer);
expect(buffer == R"({"a":"Hello\nWorld","b":"Hello World","c":"\tHello\bWorld"})");
```
## escaped
The `glz::escaped` wrapper turns off the effects of `glz::raw_string`.
```c++
struct raw_stuff_escaped
{
raw_stuff data{};
struct glaze
{
using T = raw_stuff_escaped;
static constexpr auto value{glz::escaped<&T::data>};
};
};
```
In use:
```c++
raw_stuff_escaped obj{};
std::string buffer = R"({"a":"Hello\nWorld"})";
expect(!glz::read_json(obj, buffer));
expect(obj.data.a ==
R"(Hello
World)");
buffer.clear();
glz::write_json(obj, buffer);
expect(buffer == R"({"a":"Hello\nWorld","b":"","c":""})");
```
## escape_bytes
The `glz::escape_bytes_t` wrapper (for local usage) and `glz::escape_bytes` (for meta usage) are used to read and write binary data stored in character arrays (`char[]`) or vectors (`std::vector<char>`) as fully escaped JSON strings. This is particularly useful for handling binary data that might contain null characters or other control characters that would otherwise be truncated or cause issues.
**Meta Usage (`glz::escape_bytes<&T::member>`):**
```c++
struct binary_data
{
char data[4];
};
template <>
struct glz::meta<binary_data>
{
using T = binary_data;
static constexpr auto value = object("data", glz::escape_bytes<&T::data>);
};
```
In use:
```c++
binary_data obj;
obj.data[0] = 0;
obj.data[1] = 1;
obj.data[2] = 0;
obj.data[3] = 2;
std::string out;
glz::write_json(obj, out);
expect(out == R"({"data":"\u0000\u0001\u0000\u0002"})");
binary_data obj2;
glz::read_json(obj2, out);
expect(std::memcmp(obj.data, obj2.data, 4) == 0);
```
**Local Usage (`glz::escape_bytes_t{value}`):**
```c++
char local_data[4] = {0, 'A', 0, 'B'};
std::string out;
glz::write_json(glz::escape_bytes_t{local_data}, out);
expect(out == R"("\u0000A\u0000B")");
char read_back_data[4];
glz::read_json(glz::escape_bytes_t{read_back_data}, out);
expect(read_back_data[0] == 0);
expect(read_back_data[1] == 'A');
expect(read_back_data[2] == 0);
expect(read_back_data[3] == 'B');
```
## read_constraint
Enables complex constraints to be defined within a `glz::meta` or using member functions. Parsing is short circuited upon violating a constraint and a nicely formatted error can be produced with a custom error message.
### Field order and optional members
Object members are visited in the order that the JSON input supplies them. This is an intentional design choice so
that input streams do not have to be re-ordered to match the declaration order. Because of this, a
`read_constraint` may only rely on fields that have already appeared in the JSON payload. If you need to validate the
final state of the entire object, use a `self_constraint` as shown below—those run after every field has been read.
Optional members are parsed lazily: if the JSON payload does not contain the key, the member is left untouched and the
corresponding `read_constraint` is not evaluated. This guarantees that absent optional data does not trigger
constraints. Keep in mind that reusing the same C++ object across multiple reads will retain the previous value for any
field that is omitted in later payloads, so reset or re-initialize the instance when you expect fresh state.
```c++
struct constrained_object
{
int age{};
std::string name{};
};
template <>
struct glz::meta<constrained_object>
{
using T = constrained_object;
static constexpr auto limit_age = [](const T&, int age) { return (age >= 0 && age <= 120); };
static constexpr auto limit_name = [](const T&, const std::string& name) { return name.size() <= 8; };
static constexpr auto value = object("age", read_constraint<&T::age, limit_age, "Age out of range">, //
"name", read_constraint<&T::name, limit_name, "Name is too long">);
};
```
### Object level validation
To validate combinations of fields after the object has been fully deserialized, provide a single
`self_constraint` entry. This constraint runs once after all object members have been populated and can therefore
reason about the final state.
```c++
struct cross_constrained
{
int age{};
std::string name{};
};
template <>
struct glz::meta<cross_constrained>
{
using T = cross_constrained;
static constexpr auto combined = [](const T& v) {
return ((v.name.starts_with('A') && v.age > 10) || v.age > 5);
};
static constexpr auto value = object(&T::age, &T::name);
static constexpr auto self_constraint = glz::self_constraint<combined, "Age/name combination invalid">;
};
```
You can perform more elaborate business logic as well, such as validating that user credentials are consistent and
secure:
```c++
struct registration_request
{
std::string username{};
std::string password{};
std::string confirm_password{};
std::optional<std::string> email{};
};
template <>
struct glz::meta<registration_request>
{
using T = registration_request;
static constexpr auto strong_credentials = [](const T& value) {
const bool strong_length = value.password.size() >= 12;
const bool matches = value.password == value.confirm_password;
const bool has_username = !value.username.empty();
return strong_length && matches && has_username;
};
static constexpr auto value = object(
&T::username,
&T::password,
&T::confirm_password,
&T::email);
static constexpr auto self_constraint = glz::self_constraint<strong_credentials,
"Password must be at least 12 characters and match confirmation">;
};
```
If a self constraint fails, deserialization stops and `glz::error_code::constraint_violated` is reported with the
associated message.
When it is important that object memory remains valid after every individual assignment—for example, when other code
observes the partially constructed object during parsing—prefer `read_constraint` on the specific members. Those
constraints fire before the member is written, so the in-memory representation never stores an invalid value. In
contrast, `self_constraint` runs after fields are populated, so it can detect issues that span multiple members but the
object may hold the problematic data until the constraint handler reports an error.
### Skipping self_constraint validation
In some cases you may want to skip `self_constraint` validation—for example, when performance is critical and the data
is known to be valid, or when validation should be deferred to a later stage. You can disable `self_constraint` checks
by creating a custom options struct with `skip_self_constraint = true`:
```c++
struct skip_constraint_opts : glz::opts
{
bool skip_self_constraint = true;
};
// Use it like this:
constexpr skip_constraint_opts opts{};
auto ec = glz::read<opts>(obj, buffer);
```
With this option enabled, the `self_constraint` is still defined in `glz::meta<T>` but will not be evaluated during
deserialization. This allows you to toggle validation on or off at compile time based on your use case.
## max_length
Limits string length or array/vector size when reading from binary formats (currently BEVE). This wrapper provides per-field control over allocation limits to prevent memory exhaustion from malicious or malformed input.
For strings (`std::string`), limits the maximum character count. For arrays/vectors, limits the maximum element count.
```c++
struct user_input
{
std::string username;
std::string bio;
std::vector<int> scores;
std::vector<std::string> tags;
};
template <>
struct glz::meta<user_input>
{
using T = user_input;
static constexpr auto value = object(
"username", glz::max_length<&T::username, 64>, // Max 64 characters
"bio", &T::bio, // No limit
"scores", glz::max_length<&T::scores, 100>, // Max 100 elements
"tags", glz::max_length<&T::tags, 10> // Max 10 strings
);
};
```
In use:
```c++
user_input obj{};
std::string buffer;
// Valid data within limits
user_input valid{.username = "alice", .bio = "Hello!", .scores = {95, 87, 92}, .tags = {"cpp", "glaze"}};
glz::write_beve(valid, buffer);
auto ec = glz::read_beve(obj, buffer);
expect(!ec); // Success
// Data exceeding limits
user_input oversized{.username = std::string(100, 'x'), .bio = "", .scores = {}, .tags = {}};
buffer.clear();
glz::write_beve(oversized, buffer);
ec = glz::read_beve(obj, buffer);
expect(ec.ec == glz::error_code::invalid_length); // Rejected - username exceeds 64 chars
```
### Works with complex types
The wrapper also works with arrays of complex structs:
```c++
struct item
{
std::string name;
int value;
std::vector<double> data;
};
struct container
{
std::vector<item> items;
};
template <>
struct glz::meta<container>
{
using T = container;
static constexpr auto value = object(
"items", glz::max_length<&T::items, 50> // Max 50 complex items
);
};
```
### Associated options
For global limits (applying to all strings/arrays), use custom options instead:
```c++
struct secure_opts : glz::opts
{
uint32_t format = glz::BEVE;
size_t max_string_length = 1024; // Max 1KB per string
size_t max_array_size = 10000; // Max 10,000 elements per array
};
auto ec = glz::read<secure_opts{}>(obj, buffer);
```
See [Security](./security.md) for more details on allocation limits and DoS prevention.
## partial_read
Reads into existing object and array elements and then exits without parsing the rest of the input. More documentation concerning `partial_read` can be found in the [Partial Read documentation](./partial-read.md).
> `partial_read` is useful when parsing header information before deciding how to decode the rest of a document. Or, when you only care about the first few elements of an array.
## invoke
Invoke a std::function or member function with n-arguments as an array input.
```c++
struct invoke_struct
{
int y{};
std::function<void(int x)> square{};
void add_one() { ++y; }
// MSVC requires this constructor for 'this' to be captured
invoke_struct()
{
square = [&](int x) { y = x * x; };
}
};
template <>
struct glz::meta<invoke_struct>
{
using T = invoke_struct;
static constexpr auto value = object("square", invoke<&T::square>, "add_one", invoke<&T::add_one>);
};
```
In use:
```c++
std::string s = R"(
{
"square":[5],
"add_one":[]
})";
invoke_struct obj{};
expect(!glz::read_json(obj, s));
expect(obj.y == 26); // 5 * 5 + 1
};
```
## write_float32
Writes out numbers with a maximum precision of `float32_t`.
```c++
struct write_precision_t
{
double pi = std::numbers::pi_v<double>;
struct glaze
{
using T = write_precision_t;
static constexpr auto value = glz::object("pi", glz::write_float32<&T::pi>);
};
};
```
> [!IMPORTANT]
>
> The `glz::float_precision float_max_write_precision` is not a core option in `glz::opts`. You must create an options structure that adds this field to enable float precision control. The example below shows this user defined options struct that inherits from `glz::opts`.
In use:
```c++
struct float_opts : glz::opts {
glz::float_precision float_max_write_precision{};
};
write_precision_t obj{};
std::string json_float = glz::write<float_opts{}>(obj);
expect(json_float == R"({"pi":3.1415927})") << json_float;
```
## write_float64
Writes out numbers with a maximum precision of `float64_t`.
## write_float_full
Writes out numbers with full precision (turns off higher level float precision wrappers).
## float_format
Format floating-point numbers using `std::format` syntax (C++23). This wrapper provides per-member control over float formatting with the full flexibility of C++ format specifications.
```c++
struct coordinates
{
double latitude{37.7749295};
double longitude{-122.4194155};
float altitude{10.5f};
};
template <>
struct glz::meta<coordinates>
{
using T = coordinates;
static constexpr auto value = glz::object(
"lat", glz::float_format<&T::latitude, "{:.4f}">,
"lon", glz::float_format<&T::longitude, "{:.4f}">,
"alt", glz::float_format<&T::altitude, "{:.1f}">
);
};
```
In use:
```c++
coordinates point{};
std::string json = glz::write_json(point).value_or("error");
// Output: {"lat":37.7749,"lon":-122.4194,"alt":10.5}
// Reading works normally - format only affects writing
coordinates point2{};
glz::read_json(point2, R"({"lat":40.7128,"lon":-74.0060,"alt":5.0})");
```
### Format String Syntax
The format string follows `std::format` syntax. Common specifiers for floats:
| Format | Description | Example Input | Example Output |
|--------|-------------|---------------|----------------|
| `{:.2f}` | Fixed, 2 decimal places | `3.14159` | `3.14` |
| `{:.0f}` | Fixed, no decimals (rounds) | `3.7` | `4` |
| `{:.6f}` | Fixed, 6 decimal places | `3.14159` | `3.141590` |
| `{:.2e}` | Scientific (lowercase) | `1234567.89` | `1.23e+06` |
| `{:.3E}` | Scientific (uppercase) | `0.000123` | `1.230E-04` |
| `{:.4g}` | General (auto-selects f/e) | `0.0001234` | `0.0001234` |
### Mixing Formatted and Unformatted Members
You can combine `float_format` with regular member pointers:
```c++
struct sensor_data
{
double temperature{23.456789}; // High precision needed
double display_value{23.456789}; // Formatted for display
int sensor_id{42};
};
template <>
struct glz::meta<sensor_data>
{
using T = sensor_data;
static constexpr auto value = glz::object(
"temperature", &T::temperature, // Full precision (default behavior)
"display", glz::float_format<&T::display_value, "{:.1f}">, // Formatted
"id", &T::sensor_id
);
};
```
Output: `{"temperature":23.456789,"display":23.5,"id":42}`
### Special Values
The wrapper handles special floating-point values using `std::format` behavior:
```c++
coordinates point{std::numeric_limits<double>::infinity(), -0.0, 0.0f};
// Output includes "inf" for infinity values
```
### Comparison with Global float_format Option
| Feature | Per-member `glz::float_format` | Global `float_format` option |
|---------|-------------------------------|------------------------------|
| Scope | Individual members | All floats in serialization |
| Flexibility | Different format per member | Same format for all |
| Usage | `glz::meta` wrappers | Custom `glz::opts` struct |
Use the per-member wrapper when different fields need different formatting. Use the global option when all floats should be formatted the same way.
### Global float_format Option
For formatting all floats globally, add `float_format` to a custom options struct:
```c++
struct format_opts : glz::opts
{
static constexpr std::string_view float_format = "{:.2f}";
};
double pi = 3.14159265358979;
std::string json = glz::write<format_opts{}>(pi).value_or("error");
// Output: 3.14
```
> [!NOTE]
> The `float_format` option uses `std::format` internally and requires C++23. The format string is validated at compile time via `std::format_string`.
## Associated glz::opts for float precision
```c++
enum struct float_precision : uint8_t { full, float32 = 4, float64 = 8, float128 = 16 };
```
glz::opts
```c++
// The maximum precision type used for writing floats, higher precision floats will be cast down to this precision
float_precision float_max_write_precision{};
```
## custom
Calls custom read and write std::functions, lambdas, or member functions.
```c++
struct custom_encoding
{
uint64_t x{};
std::string y{};
std::array<uint32_t, 3> z{};
void read_x(const std::string& s) { x = std::stoi(s); }
uint64_t write_x() { return x; }
void read_y(const std::string& s) { y = "hello" + s; }
auto& write_z()
{
z[0] = 5;
return z;
}
};
template <>
struct glz::meta<custom_encoding>
{
using T = custom_encoding;
static constexpr auto value = object("x", custom<&T::read_x, &T::write_x>, //
"y", custom<&T::read_y, &T::y>, //
"z", custom<&T::z, &T::write_z>);
};
```
In use:
```c++
"custom_reading"_test = [] {
custom_encoding obj{};
std::string s = R"({"x":"3","y":"world","z":[1,2,3]})";
expect(!glz::read_json(obj, s));
expect(obj.x == 3);
expect(obj.y == "helloworld");
expect(obj.z == std::array<uint32_t, 3>{1, 2, 3});
};
"custom_writing"_test = [] {
custom_encoding obj{};
std::string s = R"({"x":"3","y":"world","z":[1,2,3]})";
expect(!glz::read_json(obj, s));
std::string out{};
glz::write_json(obj, out);
expect(out == R"({"x":3,"y":"helloworld","z":[5,2,3]})");
};
```
### Another custom example
Showing use of constexpr lambdas for customization.
```c++
struct custom_buffer_input
{
std::string str{};
};
template <>
struct glz::meta<custom_buffer_input>
{
static constexpr auto read_x = [](custom_buffer_input& s, const std::string& input) { s.str = input; };
static constexpr auto write_x = [](auto& s) -> auto& { return s.str; };
static constexpr auto value = glz::object("str", glz::custom<read_x, write_x>);
};
```
In use:
```c++
std::string s = R"({"str":"Hello!"})";
custom_buffer_input obj{};
expect(!glz::read_json(obj, s));
expect(obj.str == "Hello!");
s.clear();
glz::write_json(obj, s);
expect(s == R"({"str":"Hello!"})");
expect(obj.str == "Hello!");
```
> [!NOTE]
>
> With read lambdas like `[](custom_buffer_input& s, const std::string& input)`, both types must be concrete (cannot use `auto`), otherwise you'll get a compilation error noting this. The reason is that Glaze must be able to determine what type to decode into before passing the decoded value to `input`.
## manage
Calls a read function after reading and calls a write function before writing.
> `glz::manage` is useful for transforming state from a user facing format into a more complex or esoteric internal format.
```c++
struct manage_x
{
std::vector<int> x{};
std::vector<int> y{};
bool read_x()
{
y = x;
return true;
}
bool write_x()
{
x = y;
return true;
}
};
template <>
struct glz::meta<manage_x>
{
using T = manage_x;
static constexpr auto value = object("x", manage<&T::x, &T::read_x, &T::write_x>);
};
```
In use:
```c++
manage_x obj{};
std::string s = R"({"x":[1,2,3]})";
expect(!glz::read_json(obj, s));
expect(obj.y[0] == 1);
expect(obj.y[1] == 2);
obj.x.clear();
s.clear();
glz::write_json(obj, s);
expect(s == R"({"x":[1,2,3]})");
expect(obj.x[0] == 1);
expect(obj.x[1] == 2);
```
### Another manage example
```c++
struct manage_x_lambda
{
std::vector<int> x{};
std::vector<int> y{};
};
template <>
struct glz::meta<manage_x_lambda>
{
using T = manage_x_lambda;
static constexpr auto read_x = [](auto& s) {
s.y = s.x;
return true;
};
static constexpr auto write_x = [](auto& s) {
s.x = s.y;
return true;
};
[[maybe_unused]] static constexpr auto value = object("x", manage<&T::x, read_x, write_x>);
};
```
In use:
```c++
manage_x_lambda obj{};
std::string s = R"({"x":[1,2,3]})";
expect(!glz::read_json(obj, s));
expect(obj.y[0] == 1);
expect(obj.y[1] == 2);
obj.x.clear();
s.clear();
glz::write_json(obj, s);
expect(s == R"({"x":[1,2,3]})");
expect(obj.x[0] == 1);
expect(obj.x[1] == 2);
```
## skip_null_members_on_read
The `skip_null_members_on_read` option allows reading JSON or BEVE data with null values without requiring `std::optional` wrappers on your C++ types. When enabled, null values in the input are simply skipped, leaving the existing field values unchanged.
This option is particularly useful when:
- You receive JSON from external sources that may include null values but you want to maintain default or existing values
- You want to avoid wrapping all your types in `std::optional` just to handle occasional null values
- You're implementing partial updates where null means "don't change this field"
### Usage
Add `bool skip_null_members_on_read = true;` to a custom options struct:
```c++
struct Person {
std::string name = "Unknown";
int age = 0;
double salary = 0.0;
};
template <>
struct glz::meta<Person> {
using T = Person;
static constexpr auto value = glz::object(&T::name, &T::age, &T::salary);
};
struct opts_skip_null : glz::opts {
bool skip_null_members_on_read = true;
};
```
### Example: Preserving Existing Values
```c++
Person person;
person.name = "Alice";
person.age = 30;
person.salary = 75000.0;
// JSON with null values
std::string json = R"({"name":null,"age":35,"salary":null})";
glz::read<opts_skip_null{}>(person, json);
// null fields were skipped, preserving existing values
expect(person.name == "Alice"); // Unchanged (was null in JSON)
expect(person.age == 35); // Updated
expect(person.salary == 75000.0); // Unchanged (was null in JSON)
```
### Format Support
This option works with both JSON and BEVE formats:
**JSON:**
```c++
struct opts_skip_null_json : glz::opts {
bool skip_null_members_on_read = true;
// format = JSON (default)
};
```
**BEVE:**
```c++
struct opts_skip_null_beve : glz::opts {
uint32_t format = glz::BEVE;
bool skip_null_members_on_read = true;
};
```
### Behavior
- **With option enabled:** Null values in input are skipped; C++ field retains its existing value
- **With option disabled (default):** Null values cause errors for non-nullable types, or reset `std::optional` types to empty
### Comparison with std::optional
**Without skip_null_members_on_read:**
```c++
struct PersonWithOptional {
std::optional<std::string> name;
std::optional<int> age;
};
PersonWithOptional p;
p.name = "Alice";
p.age = 30;
glz::read_json(p, R"({"name":null,"age":35})");
// name is reset to empty by null
expect(!p.name.has_value());
expect(p.age == 35);
```
**With skip_null_members_on_read:**
```c++
struct Person {
std::string name; // No std::optional needed
int age;
};
Person p;
p.name = "Alice";
p.age = 30;
glz::read<opts_skip_null{}>(p, R"({"name":null,"age":35})");
// name preserves existing value
expect(p.name == "Alice");
expect(p.age == 35);
```
> [!NOTE]
> This option is not a core field in `glz::opts`. You must create a custom options struct that adds this field, as shown in the examples above.
|