Skip to content

Commit f7dc907

Browse files
committed
json: enrich Simple token containers + fix int64/ll ambiguity
Keep public API stable while adding vector-like helpers for array_t/kvs (reserve, push, erase, typed set/get, iteration). Fix ambiguous conversion when passing long long into token/object initializers on platforms where int64_t is not long long. Document JSON helpers consistently (build/convert/jpath/loads/dumps/json.hpp) and add Simple examples under examples/json_simple/.
1 parent 6ae65b0 commit f7dc907

File tree

15 files changed

+1428
-508
lines changed

15 files changed

+1428
-508
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <vix/json/Simple.hpp>
2+
#include <iostream>
3+
4+
using namespace vix::json;
5+
6+
int main()
7+
{
8+
token t1; // null
9+
token t2 = true; // bool
10+
token t3 = 42; // int -> int64
11+
token t4 = 3.14; // double
12+
token t5 = "hello"; // string
13+
14+
std::cout << "is_null=" << t1.is_null() << "\n";
15+
std::cout << "bool=" << t2.as_bool_or(false) << "\n";
16+
std::cout << "int64=" << t3.as_i64_or(-1) << "\n";
17+
std::cout << "double=" << t4.as_f64_or(0.0) << "\n";
18+
std::cout << "string=" << t5.as_string_or("empty") << "\n";
19+
20+
return 0;
21+
}

examples/json_simple/02_arrays.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <vix/json/Simple.hpp>
2+
#include <iostream>
3+
4+
using namespace vix::json;
5+
6+
int main()
7+
{
8+
array_t arr;
9+
10+
arr.push_int(1);
11+
arr.push_int(2);
12+
arr.push_string("three");
13+
arr.push_bool(true);
14+
15+
arr.reserve(10);
16+
arr.ensure(6).set_string("auto-filled");
17+
18+
for (std::size_t i = 0; i < arr.size(); ++i)
19+
{
20+
if (arr[i].is_string())
21+
std::cout << "string: " << arr[i].as_string_or("") << "\n";
22+
else if (arr[i].is_i64())
23+
std::cout << "int: " << arr[i].as_i64_or(0) << "\n";
24+
}
25+
26+
return 0;
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <vix/json/Simple.hpp>
2+
#include <iostream>
3+
4+
using namespace vix::json;
5+
6+
int main()
7+
{
8+
kvs user;
9+
10+
user.set_string("name", "Alice");
11+
user.set_int("age", 30);
12+
user.set_bool("active", true);
13+
14+
std::cout << user.get_string_or("name", "unknown") << "\n";
15+
std::cout << user.get_i64_or("age", 0) << "\n";
16+
std::cout << user.get_bool_or("active", false) << "\n";
17+
18+
return 0;
19+
}

examples/json_simple/04_nested.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <vix/json/Simple.hpp>
2+
#include <iostream>
3+
4+
using namespace vix::json;
5+
6+
int main()
7+
{
8+
kvs root;
9+
10+
kvs &user = root.ensure_object("user");
11+
user.set_string("name", "Gaspard");
12+
user.set_int("id", 42);
13+
14+
array_t &skills = user.ensure_array("skills");
15+
skills.push_string("C++");
16+
skills.push_string("P2P");
17+
skills.push_string("Systems");
18+
19+
std::cout << user.get_string_or("name", "") << "\n";
20+
std::cout << skills[0].as_string_or("") << "\n";
21+
22+
return 0;
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <vix/json/Simple.hpp>
2+
#include <iostream>
3+
4+
using namespace vix::json;
5+
6+
int main()
7+
{
8+
token root;
9+
10+
// Transforme dynamiquement en objet
11+
kvs &obj = root.ensure_object();
12+
obj.set_string("mode", "dev");
13+
14+
// Puis en array
15+
array_t &logs = obj.ensure_array("logs");
16+
logs.push_string("boot");
17+
logs.push_string("init");
18+
logs.push_string("ready");
19+
20+
logs.erase_at(1);
21+
22+
for (const auto &t : logs)
23+
std::cout << t.as_string_or("") << "\n";
24+
25+
return 0;
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <vix/json/Simple.hpp>
2+
#include <iostream>
3+
4+
using namespace vix::json;
5+
6+
int main()
7+
{
8+
kvs cfg = obj({"host", "localhost",
9+
"port", 8080,
10+
"secure", false});
11+
12+
cfg.for_each_pair([](std::string_view key, const token &value)
13+
{
14+
std::cout << key << " -> ";
15+
if (value.is_string())
16+
std::cout << value.as_string_or("");
17+
else if (value.is_i64())
18+
std::cout << value.as_i64_or(0);
19+
else if (value.is_bool())
20+
std::cout << value.as_bool_or(false);
21+
std::cout << "\n"; });
22+
23+
return 0;
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <vix/json/Simple.hpp>
2+
#include <iostream>
3+
4+
using namespace vix::json;
5+
6+
int main()
7+
{
8+
kvs base = obj({"env", "prod",
9+
"debug", false});
10+
11+
kvs override = obj({"debug", true,
12+
"trace", true});
13+
14+
base.merge_from(override, true);
15+
16+
base.erase("env");
17+
18+
for (auto &k : base.keys())
19+
std::cout << "key=" << k << "\n";
20+
21+
return 0;
22+
}

examples/json_simple/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Simple.json examples
2+
3+
These examples demonstrate the usage of `vix::json::Simple`,
4+
a lightweight in-memory JSON-like model.
5+
6+
## When to use Simple.json
7+
- Internal data exchange
8+
- No JSON parsing/serialization needed
9+
- Predictable memory layout
10+
- Cheap copies and mutations
11+
12+
## Files
13+
- `01_basic_values.cpp` – primitive values
14+
- `02_arrays.cpp` – array container API
15+
- `03_objects.cpp` – object container API
16+
- `04_nested.cpp` – nested structures
17+
- `05_mutation.cpp` – dynamic mutation
18+
- `06_iteration.cpp` – iteration helpers
19+
- `07_merge_and_erase.cpp` – advanced object ops
20+
21+
If you need parsing or dumping JSON text, use `<vix/json.hpp>` instead.

0 commit comments

Comments
 (0)