A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
msgpack.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
8#ifndef A11_DATA_MSGPACK_H_
9#define A11_DATA_MSGPACK_H_
10
11#include <cstddef>
12#include <string>
13#include <string_view>
14
15#include <absl/status/status.h>
16#include <absl/status/statusor.h>
17#include <nlohmann/json.hpp>
18
19namespace a11::data {
20
28 public:
30 absl::Status Pack(const nlohmann::json& value);
31
33 [[nodiscard]] const std::string& bytes() const { return bytes_; }
34
36 [[nodiscard]] std::string TakeBytes() { return std::move(bytes_); }
37
38 private:
39 std::string bytes_;
40};
41
44 public:
46 explicit MsgpackReader(std::string_view bytes) : bytes_(bytes) {}
47
49 absl::StatusOr<nlohmann::json> Read();
51 absl::Status EnsureFullyConsumed() const;
52
54 [[nodiscard]] size_t position() const { return position_; }
55
56 private:
57 std::string_view bytes_;
58 size_t position_ = 0;
59};
60
62absl::StatusOr<std::string> PackStatus(const absl::Status& status);
64absl::StatusOr<absl::Status> UnpackStatus(std::string_view bytes);
65
67nlohmann::json Binary(std::string_view bytes);
69absl::StatusOr<std::string> GetBinary(const nlohmann::json& value,
70 std::string_view field_name);
71
72} // namespace a11::data
73
74#endif // A11_DATA_MSGPACK_H_
Sequentially decode the fields of one A11 MessagePack wire record.
Definition msgpack.h:43
absl::StatusOr< nlohmann::json > Read()
Decode the next field and advance the byte cursor.
Definition msgpack.cc:263
MsgpackReader(std::string_view bytes)
Read from bytes, which must remain alive while this reader is used.
Definition msgpack.h:46
absl::Status EnsureFullyConsumed() const
Return an error if unread trailing bytes remain in the record.
Definition msgpack.cc:283
size_t position() const
Current byte offset, useful when diagnosing malformed peer data.
Definition msgpack.h:54
Incrementally encode the fields of one A11 wire record.
Definition msgpack.h:27
std::string TakeBytes()
Transfer the encoded bytes out of this writer.
Definition msgpack.h:36
absl::Status Pack(const nlohmann::json &value)
Append a JSON-compatible field to the encoded record.
Definition msgpack.cc:251
const std::string & bytes() const
View the encoded bytes without transferring ownership.
Definition msgpack.h:33
Definition json.cc:22
absl::StatusOr< std::string > GetBinary(const nlohmann::json &value, std::string_view field_name)
Read a binary JSON value, naming field_name in any validation error.
Definition msgpack.cc:295
absl::StatusOr< absl::Status > UnpackStatus(std::string_view bytes)
Decode a structured status without losing A11 status details.
Definition msgpack.cc:319
nlohmann::json Binary(std::string_view bytes)
Wrap arbitrary bytes as a MessagePack binary JSON value.
Definition msgpack.cc:290
absl::StatusOr< std::string > PackStatus(const absl::Status &status)
Encode a structured Abseil status for action and transport status fields.
Definition msgpack.cc:305
Future< T > SubmitWithCancellationHook(absl::AnyInvocable< absl::StatusOr< T >() && > work, std::function< void()> cancellation_hook, thread::TreeOptions tree_options)
Run work on A11's fiber pool with application-specific cancellation.
Definition executor.h:30