TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec::Json Struct Reference

Utility class for generating minimal, valid JSON strings at compile-time and runtime. More...

#include <tec_json.hpp>

Public Member Functions

template<typename T >
std::string operator() (const T &val, const char *name)
 Functor interface — serialize with explicit name.
 
template<typename T >
std::string operator() (const T &val)
 Functor interface — serialize without name (root value).
 

Static Public Member Functions

static void print_name (std::ostringstream &os, const char *name)
 Helper to optionally emit a JSON key name.
 
static std::string json (const std::string &val, const char *name=nullptr)
 Serialize a std::string as a JSON string literal.
 
static std::string json (const Blob &val, const char *name=nullptr)
 Serialize a Blob object as a base64-encoded JSON string.
 
static std::string json_bool (const bool &val, const char *name=nullptr)
 Serialize a boolean as JSON true or false.
 
template<typename TContainer >
static std::string json_container (const TContainer &c, const char *name)
 Serialize any container (with .begin()/.end()) as a JSON array.
 
template<typename TMap >
static std::string json_map (const TMap &m, const char *name)
 Serialize unordered map-like containers as JSON objects.
 
template<typename TObject >
static std::string json_object (const TObject &obj, const char *name)
 Serialize a tec::Serializable-derived object as a nested JSON object.
 
template<typename T >
static std::string json (const T &val, const char *name)
 Generic JSON serializer with automatic type dispatching.
 

Static Public Attributes

static constexpr char sep [] {", "}
 Separator used between array/object elements.
 
static constexpr char infix [] {": "}
 Separator used between object key and value.
 

Detailed Description

Utility class for generating minimal, valid JSON strings at compile-time and runtime.

Json provides a lightweight, header-only way to convert basic C++ types, containers, and tec::Serializable objects into JSON-formatted strings.

It is intentionally minimal — no external dependencies, no parsing, only serialization. The output is valid JSON but not pretty-printed (compact form).

Supports:

  • Fundamental types (int, float, bool, etc.)
  • std::string
  • tec::Blob (as Base64-encoded string)
  • Any type satisfying is_container_v<T> → JSON array
  • Any type satisfying is_map_v<T> → JSON object
  • Any type derived from tec::JsonSerializable → nested object via to_json()
Note
This is not a full JSON library — it's designed for debugging, logging, network messages, and configuration where performance and simplicity matter.
struct Person: tec::Serializable {
using json = tec::Json;
static constexpr auto sep{tec::Json::sep};
short age;
std::string name;
std::string surname;
friend std::ostream& operator << (std::ostream& os, const Person& p) {
os << json{}(p);
return os;
}
std::string to_json() const override {
std::ostringstream os;
os
<< json{}(age, "age") << sep
<< json{}(name, "name") << sep
<< json{}(surname, "surname")
;
return os.str();
}
};
Utility class for generating minimal, valid JSON strings at compile-time and runtime.
Definition tec_json.hpp:62
static constexpr char sep[]
Separator used between array/object elements.
Definition tec_json.hpp:65
Base interface for objects that support binary serialization.
Definition tec_serialize.hpp:60
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

Member Function Documentation

◆ json() [1/3]

static std::string tec::Json::json ( const Blob val,
const char name = nullptr 
)
inlinestatic

Serialize a Blob object as a base64-encoded JSON string.

Parameters
valThe byte container
nameOptional JSON key name
Returns
Base64-encoded string

◆ json() [2/3]

static std::string tec::Json::json ( const std::string &  val,
const char name = nullptr 
)
inlinestatic

Serialize a std::string as a JSON string literal.

Parameters
valThe string value
nameOptional JSON key name
Returns
JSON fragment (with optional key)

◆ json() [3/3]

template<typename T >
static std::string tec::Json::json ( const T val,
const char name 
)
inlinestatic

Generic JSON serializer with automatic type dispatching.

Dispatches to the most appropriate specialization based on type traits:

  1. tec::Serializablejson_object()
  2. Unordered map-like → json_map()
  3. Container → json_container()
  4. Scalar types → direct operator<<
Template Parameters
TValue type
Parameters
valValue to serialize
nameOptional JSON key name
Returns
JSON fragment

◆ json_bool()

static std::string tec::Json::json_bool ( const bool val,
const char name = nullptr 
)
inlinestatic

Serialize a boolean as JSON true or false.

Parameters
valThe boolean value
nameOptional JSON key name
Returns
JSON boolean literal

◆ json_container()

template<typename TContainer >
static std::string tec::Json::json_container ( const TContainer c,
const char name 
)
inlinestatic

Serialize any container (with .begin()/.end()) as a JSON array.

Elements are serialized using their own operator<< (or specialized json() if available).

Template Parameters
TContainerContainer type satisfying is_container_v
Parameters
cContainer instance
nameOptional array name (e.g., "myArray": [...])
Returns
JSON array string

◆ json_map()

template<typename TMap >
static std::string tec::Json::json_map ( const TMap m,
const char name 
)
inlinestatic

Serialize unordered map-like containers as JSON objects.

Keys and values must support operator<<. Structured bindings are used for iteration.

Template Parameters
TMapMap type satisfying is_map_v
Parameters
mMap instance
nameOptional object name
Returns
JSON object string

◆ json_object()

template<typename TObject >
static std::string tec::Json::json_object ( const TObject obj,
const char name 
)
inlinestatic

Serialize a tec::Serializable-derived object as a nested JSON object.

Calls the object's to_json() method and wraps it in braces.

Template Parameters
TObjectType derived from tec::Serializable
Parameters
objObject instance
nameOptional field name
Returns
JSON like "name": { ...to_json output... }

◆ operator()() [1/2]

template<typename T >
std::string tec::Json::operator() ( const T val)
inline

Functor interface — serialize without name (root value).

Allows usage like: Json{}(myValue)

Template Parameters
TValue type
Parameters
valValue to serialize
Returns
JSON string (no key prefix)

◆ operator()() [2/2]

template<typename T >
std::string tec::Json::operator() ( const T val,
const char name 
)
inline

Functor interface — serialize with explicit name.

Allows usage like: Json{}(myValue, "fieldName")

Template Parameters
TValue type
Parameters
valValue to serialize
nameField name in JSON
Returns
JSON string

◆ print_name()

static void tec::Json::print_name ( std::ostringstream &  os,
const char name 
)
inlinestatic

Helper to optionally emit a JSON key name.

If name is non-null, outputs "name": (with proper quoting and spacing). If name is null, does nothing.

Parameters
osOutput stream to write to
nameOptional field name (null-terminated string)

The documentation for this struct was generated from the following file: