BEVE)
`glz::lazy_json` provides on-demand parsing with zero upfront work. Construction is O(1) — it just stores a pointer. Only the bytes you actually access get parsed.
std::string json = R"({"name":"John","age":30,"scores":[95,87,92]})";
auto result = glz::lazy_json(json);
if (result) {
auto& doc = *result;
auto name = doc["name"].get<std::string_view>(); // Only parses "name"
auto age = doc["age"].get<int64_t>(); // Only parses "age"
}
For random access into large arrays, you can build an index in O(n) and then get O(1) lookups:
auto users = doc["users"].index(); // O(n) one-time build
auto user500 = users[500]; // O(1) random access
You can also deserialize into structs directly from a lazy view:
User user{};
glz::read_json(user, doc["user"]);
# HTTP Server, REST, and WebSockets
Glaze now includes a full HTTP server with async ASIO backend, TLS support, and WebSocket connections.
# Basic server
glz::http_server server;
server.get("/hello", [](const glz::request& req, glz::response& res) {
res.body("Hello, World!");
});
server.bind("127.0.0.1", 8080).with_signals();
server.start();
server.wait_for_signal();
# Auto-generated REST endpoints using reflection
You can register C++ objects and Glaze will automatically generate REST endpoints from reflected methods:
struct UserService {
std::vector<User> getAllUsers() { return users; }
User getUserById(size_t id) { return users.at(id); }
User createUser(const User& user) { users.push_back(user); return users.back(); }
};
glz::registry<glz::opts{}, glz::REST> registry;
registry.on(userService);
server.mount("/api", registry.endpoints);
Method names are mapped to HTTP methods automatically — `get*()` becomes GET, `create*()` becomes POST, etc.
# WebSockets
auto ws_server = std::make_shared<glz::websocket_server>();
ws_server->on_message([](auto conn, std::string_view msg, glz::ws_opcode opcode) {
conn->send_text("Echo: " + std::string(msg));
});
server.websocket("/ws", ws_server);
https://redd.it/1rrq9e6
@r_cpp
Post #24849
14