parcel 0.2.2
Wrappable, wire-transferable C++23 value system with JSON serialization
Loading...
Searching...
No Matches
json_io.h
Go to the documentation of this file.
1#pragma once
2
13#include <parcel/cell.h>
14#include <parcel/error.h>
15#include <parcel/json.h>
16#include <parcel/registry.h>
17
18#include <cstddef>
19#include <span>
20#include <string>
21
22#if PARCEL_HAS_EXPECTED
23#include <expected>
24#endif
25
26namespace parcel {
27
35[[nodiscard]] inline cell_t cell_from_stream(std::istream& is, ParcelRegistry const& reg) {
36 json_t j;
37 is >> j;
38 return reg.cell_from_json(j);
39}
40
47[[nodiscard]] inline cell_t cell_from_bytes(const std::span<const std::byte> bytes,
48 ParcelRegistry const& reg) {
49 // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) — std::byte → const char* is the
50 // canonical conversion at this boundary
51 const auto j = json_t::parse(reinterpret_cast<const char*>(bytes.data()),
52 reinterpret_cast<const char*>(bytes.data() + bytes.size()));
53 // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
54 return reg.cell_from_json(j);
55}
56
63inline void cell_to_stream(std::ostream& os, cell_t const& c, const int indent = -1) {
64 if (!c) {
65 os << json_t().dump(indent);
66 return;
67 }
68 os << c->to_json().dump(indent);
69}
70
71#if PARCEL_HAS_EXPECTED
73[[nodiscard]] inline std::expected<cell_t, ParcelError>
74try_cell_from_stream(std::istream& is, ParcelRegistry const& reg) {
75 json_t j;
76 try {
77 is >> j;
78 } catch (std::exception const& e) {
79 return std::unexpected(ParcelError::make(ParcelError::Code::InvalidJson, e.what()));
80 } catch (...) {
81 return std::unexpected(
83 }
84 return reg.try_cell_from_json(j);
85}
86
88[[nodiscard]] inline std::expected<cell_t, ParcelError>
89try_cell_from_bytes(const std::span<const std::byte> bytes, ParcelRegistry const& reg) {
90 json_t j;
91 try {
92 // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) — std::byte → const char* is the
93 // canonical conversion at this boundary
94 j = json_t::parse(reinterpret_cast<const char*>(bytes.data()),
95 reinterpret_cast<const char*>(bytes.data() + bytes.size()));
96 // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
97 } catch (std::exception const& e) {
98 return std::unexpected(ParcelError::make(ParcelError::Code::InvalidJson, e.what()));
99 } catch (...) {
100 return std::unexpected(
102 }
103 return reg.try_cell_from_json(j);
104}
105#endif
106
107} // namespace parcel
Core ICell interface, cell_t handle, BaseCell CRTP base, and CellLike concept.
std::shared_ptr< ICell > cell_t
Shared handle to any ICell-derived value — the canonical cell pointer.
Definition cell.h:68
Runtime catalog of cell-type descriptors, keyed by wire kind id.
Definition registry.h:115
cell_t cell_from_json(json_t const &j) const
Deserialize any registered cell from JSON, dispatching by "k".
Definition registry.h:266
ParcelError and the non-throwing surface that returns std::expected.
nlohmann::json typedef shared across cell types.
nlohmann::json json_t
Project-wide alias for nlohmann::json.
Definition json.h:19
void cell_to_stream(std::ostream &os, cell_t const &c, const int indent=-1)
Render c as JSON onto os.
Definition json_io.h:63
cell_t cell_from_bytes(const std::span< const std::byte > bytes, ParcelRegistry const &reg)
Read raw bytes as JSON and dispatch via reg.
Definition json_io.h:47
cell_t cell_from_stream(std::istream &is, ParcelRegistry const &reg)
Read one JSON document from is and dispatch via reg.
Definition json_io.h:35
ParcelRegistry, Definition, and BuiltinsOptions for polymorphic dispatch.
static ParcelError make(const Code c, std::string msg, std::string kind_id={}, std::string fld={})
Construct from code, message, and optional kind/field tags.
Definition error.h:107
@ InvalidJson
Input was not parseable JSON or had wrong shape.