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
|
# Reading
Glaze provides a unified return type for all read operations that gives you both error information and the byte count consumed.
## The `error_ctx` Type
All read functions return `glz::error_ctx`:
```cpp
struct error_ctx {
size_t count{}; // Bytes consumed from input
error_code ec{}; // Error code (none on success)
std::string_view custom_error_message{}; // Optional error details
operator bool() const noexcept; // Returns true when there IS an error
bool operator==(error_code e) const noexcept;
};
```
### Key Properties
- **`count`**: Number of bytes consumed from the input, even on error (useful for debugging)
- **`ec`**: The error code (`error_code::none` on success)
- **`operator bool()`**: Returns `true` when there is an error (matches `std::error_code` semantics)
## Basic Usage
### Reading from Strings
```cpp
std::string json = R"({"name":"Alice","age":30})";
my_struct obj{};
auto ec = glz::read_json(obj, json);
if (ec) {
std::cerr << glz::format_error(ec, json) << '\n';
return;
}
// Success: ec.count contains bytes consumed
```
### Reading from String Views
```cpp
std::string_view json = get_json_data();
my_struct obj{};
auto ec = glz::read_json(obj, json);
if (!ec) {
std::cout << "Consumed " << ec.count << " bytes\n";
}
```
### Reading Multiple Values
The `count` field enables reading multiple values from a single buffer:
```cpp
std::string input = R"({"x":1}{"y":2})"; // Two JSON objects
my_struct obj1{}, obj2{};
auto ec = glz::read_json(obj1, input);
if (!ec) {
size_t consumed = ec.count; // Bytes consumed by first read
// Read second object from remaining buffer
ec = glz::read_json(obj2, std::string_view(input).substr(consumed));
}
```
### Reading with Return Value
For convenience, Glaze provides overloads that return the parsed value:
```cpp
// Returns expected<T, error_ctx>
auto result = glz::read_json<my_struct>(json);
if (result) {
my_struct obj = *result;
} else {
std::cerr << glz::format_error(result.error(), json) << '\n';
}
```
## Error Handling
### Checking for Errors
```cpp
auto ec = glz::read_json(obj, json);
// Method 1: Boolean check
if (ec) {
// Error occurred
}
// Method 2: Check specific error
if (ec.ec == glz::error_code::parse_error) {
// Handle parse error
}
// Method 3: Use negation for success
if (!ec) {
// Success
}
```
### Formatting Errors
```cpp
auto ec = glz::read_json(obj, json);
if (ec) {
// Get formatted error message with context
std::string error_msg = glz::format_error(ec, json);
std::cerr << error_msg << '\n';
}
```
## Format Support
The `error_ctx` type is used consistently across all deserialization formats:
```cpp
// JSON
auto ec = glz::read_json(obj, buffer);
// BEVE (Binary)
auto ec = glz::read_beve(obj, buffer);
// CBOR
auto ec = glz::read_cbor(obj, buffer);
// MessagePack
auto ec = glz::read_msgpack(obj, buffer);
// Generic with options
auto ec = glz::read<glz::opts{.format = glz::JSON}>(obj, buffer);
```
## Reading from Streams
For reading directly from files or other input streams with bounded memory, see [Streaming I/O](streaming.md).
## See Also
- [Writing](writing.md) - Writing to buffers and error handling
- [Streaming I/O](streaming.md) - Reading/writing with streams
- [Options](options.md) - Compile time options
|