65 static constexpr char sep[]{
", "};
68 static constexpr char infix[]{
": "};
79 inline static void print_name(std::ostringstream& os,
const char* name) {
92 static std::string
json(
const std::string& val,
const char* name =
nullptr) {
93 std::ostringstream os;
95 os <<
"\"" << val <<
"\"";
106 static std::string
json(
const Blob& val,
const char* name =
nullptr) {
107 std::ostringstream os;
109 os <<
"\"" << base64::encode(val.
str()) <<
"\"";
120 static std::string
json_bool(
const bool& val,
const char* name =
nullptr) {
121 constexpr static char t[]{
"true"};
122 constexpr static char f[]{
"false"};
123 std::ostringstream os;
145 template <
typename TContainer>
147 std::ostringstream os;
151 for(
const auto& e: c ) {
174 template <
typename TMap>
175 static std::string
json_map(
const TMap& m,
const char* name) {
176 std::ostringstream os;
180 for (
const auto& [k, v]: m) {
182 os << k <<
infix << v;
210 template <
typename TObject>
211 static std::string
json_object(
const TObject& obj,
const char* name) {
212 std::ostringstream os;
214 os <<
"{" << obj.to_json() <<
"}";
238 template <
typename T>
239 static std::string
json(
const T& val,
const char* name) {
240 std::ostringstream os;
241 if constexpr (is_serializable_v<T>) {
244 else if constexpr (is_map_v<T>) {
247 else if constexpr (is_container_v<T>) {
250 else if constexpr (std::is_same_v<T, bool>) {
272 template <
typename T>
286 template <
typename T>
288 return json(val,
nullptr);
A byte buffer class with stream-like read/write semantics.
Definition tec_memfile.hpp:50
const std::string & str() const noexcept
Returns a const reference to the internal string buffer.
Definition tec_memfile.hpp:235
Utility class for generating minimal, valid JSON strings at compile-time and runtime.
Definition tec_json.hpp:62
static std::string json_bool(const bool &val, const char *name=nullptr)
Serialize a boolean as JSON true or false.
Definition tec_json.hpp:120
static constexpr char infix[]
Separator used between object key and value.
Definition tec_json.hpp:68
static std::string json(const T &val, const char *name)
Generic JSON serializer with automatic type dispatching.
Definition tec_json.hpp:239
static std::string json(const std::string &val, const char *name=nullptr)
Serialize a std::string as a JSON string literal.
Definition tec_json.hpp:92
std::string operator()(const T &val)
Functor interface — serialize without name (root value).
Definition tec_json.hpp:287
std::string operator()(const T &val, const char *name)
Functor interface — serialize with explicit name.
Definition tec_json.hpp:273
static void print_name(std::ostringstream &os, const char *name)
Helper to optionally emit a JSON key name.
Definition tec_json.hpp:79
static std::string json_object(const TObject &obj, const char *name)
Serialize a tec::Serializable-derived object as a nested JSON object.
Definition tec_json.hpp:211
static constexpr char sep[]
Separator used between array/object elements.
Definition tec_json.hpp:65
static std::string json_map(const TMap &m, const char *name)
Serialize unordered map-like containers as JSON objects.
Definition tec_json.hpp:175
static std::string json_container(const TContainer &c, const char *name)
Serialize any container (with .begin()/.end()) as a JSON array.
Definition tec_json.hpp:146
static std::string json(const Blob &val, const char *name=nullptr)
Serialize a Blob object as a base64-encoded JSON string.
Definition tec_json.hpp:106
A header-only Base64 encoder/decoder.
Generic container and map traits.
Common definitions and utilities for the tec namespace.
A byte buffer class with stream-like read/write semantics.
auto name(const Message &msg) noexcept
Retrieves the type name of a message's content for registering the corresponding message handler.
Definition tec_message.hpp:84
The base interface for serializable objects.