|
TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
|
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. | |
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:
int, float, bool, etc.)std::stringtec::Blob (as Base64-encoded string)is_container_v<T> → JSON arrayis_map_v<T> → JSON objecttec::JsonSerializable → nested object via to_json()Serialize a Blob object as a base64-encoded JSON string.
| val | The byte container |
| name | Optional JSON key name |
|
inlinestatic |
Serialize a std::string as a JSON string literal.
| val | The string value |
| name | Optional JSON key name |
Generic JSON serializer with automatic type dispatching.
Dispatches to the most appropriate specialization based on type traits:
tec::Serializable → json_object()json_map()json_container()operator<<| T | Value type |
| val | Value to serialize |
| name | Optional JSON key name |
|
inlinestatic |
Serialize a boolean as JSON true or false.
| val | The boolean value |
| name | Optional JSON key name |
|
inlinestatic |
Serialize any container (with .begin()/.end()) as a JSON array.
Elements are serialized using their own operator<< (or specialized json() if available).
| TContainer | Container type satisfying is_container_v |
| c | Container instance |
| name | Optional array name (e.g., "myArray": [...]) |
Serialize unordered map-like containers as JSON objects.
Keys and values must support operator<<. Structured bindings are used for iteration.
| TMap | Map type satisfying is_map_v |
| m | Map instance |
| name | Optional object name |
Serialize a tec::Serializable-derived object as a nested JSON object.
Calls the object's to_json() method and wraps it in braces.
| TObject | Type derived from tec::Serializable |
| obj | Object instance |
| name | Optional field name |
"name": { ...to_json output... } Functor interface — serialize without name (root value).
Allows usage like: Json{}(myValue)
| T | Value type |
| val | Value to serialize |
Functor interface — serialize with explicit name.
Allows usage like: Json{}(myValue, "fieldName")
| T | Value type |
| val | Value to serialize |
| name | Field name in JSON |
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.
| os | Output stream to write to |
| name | Optional field name (null-terminated string) |