Skip to content

Commit 4f96527

Browse files
committed
factorisation
1 parent 1515b15 commit 4f96527

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

include/vix/json/Simple.hpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#ifndef VIX_JSON_SIMPLE_HPP
2+
#define VIX_JSON_SIMPLE_HPP
3+
4+
/**
5+
* @file Simple.hpp
6+
* @brief Minimal JSON-like data model for lightweight Vix internal APIs.
7+
*
8+
* @details
9+
* `vix::json::Simple` provides a self-contained JSON representation for internal
10+
* use, independent from `nlohmann::json`. It is designed to be header-only and
11+
* trivially embeddable in performance-sensitive modules or plugins.
12+
*
13+
* Features:
14+
* - `token`: a tagged variant supporting scalars, arrays, and objects.
15+
* - `array_t`: a flat sequence of tokens.
16+
* - `kvs`: a flattened key/value list representing JSON objects.
17+
* - Helper functions (`obj()` and `array()`) for quick construction.
18+
* - Implicit constructors for nesting (tokens can wrap `array_t` or `kvs`).
19+
*
20+
* Example:
21+
* ```cpp
22+
* using namespace vix::json;
23+
*
24+
* kvs user = obj({
25+
* "name", "Alice",
26+
* "age", 30,
27+
* "skills", array({"C++", "Networking", "Systems"})
28+
* });
29+
*
30+
* token t = user; // convertible to token
31+
* ```
32+
*
33+
* ### Design notes
34+
* - This type avoids dynamic JSON parsing/serialization overhead when
35+
* interoperating between different internal JSON adapters.
36+
* - Recursive types are handled using shared_ptr wrappers.
37+
*/
38+
39+
#include <string>
40+
#include <variant>
41+
#include <vector>
42+
#include <memory>
43+
#include <initializer_list>
44+
#include <type_traits>
45+
46+
namespace vix::json
47+
{
48+
49+
struct array_t;
50+
struct kvs;
51+
52+
struct token
53+
{
54+
using value_t = std::variant<
55+
std::monostate, // null
56+
bool,
57+
long long, // integer (64-bit)
58+
double,
59+
std::string,
60+
std::shared_ptr<array_t>, // array
61+
std::shared_ptr<kvs> // object
62+
>;
63+
64+
value_t v{std::monostate{}};
65+
66+
token() = default;
67+
token(std::nullptr_t) : v(std::monostate{}) {}
68+
token(bool b) : v(b) {}
69+
token(int i) : v(static_cast<long long>(i)) {}
70+
token(long long i) : v(i) {}
71+
token(double d) : v(d) {}
72+
token(const char *s) : v(std::string(s)) {}
73+
token(std::string s) : v(std::move(s)) {}
74+
75+
token(const kvs &obj);
76+
token(const array_t &arr);
77+
};
78+
79+
struct kvs
80+
{
81+
std::vector<token> flat{};
82+
83+
kvs() = default;
84+
kvs(std::initializer_list<token> list) : flat(list) {}
85+
explicit kvs(const std::vector<token> &v) : flat(v) {}
86+
explicit kvs(std::vector<token> &&v) : flat(std::move(v)) {}
87+
};
88+
89+
struct array_t
90+
{
91+
std::vector<token> elems;
92+
93+
array_t() = default;
94+
array_t(std::initializer_list<token> l) : elems(l) {}
95+
explicit array_t(const std::vector<token> &v) : elems(v) {}
96+
explicit array_t(std::vector<token> &&v) : elems(std::move(v)) {}
97+
};
98+
99+
inline token::token(const kvs &obj) : v(std::make_shared<kvs>(obj)) {}
100+
inline token::token(const array_t &arr) : v(std::make_shared<array_t>(arr)) {}
101+
inline array_t array(std::initializer_list<token> l) { return array_t{l}; }
102+
inline kvs obj(std::initializer_list<token> l) { return kvs{l}; }
103+
inline array_t array(const std::vector<token> &v) { return array_t{v}; }
104+
inline array_t array(std::vector<token> &&v) { return array_t{std::move(v)}; }
105+
inline kvs obj(const std::vector<token> &v) { return kvs{v}; }
106+
inline kvs obj(std::vector<token> &&v) { return kvs{std::move(v)}; }
107+
108+
} // namespace vix::json
109+
110+
#endif // VIX_JSON_SIMPLE_HPP

0 commit comments

Comments
 (0)