-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialization.cpp
More file actions
127 lines (100 loc) · 5.09 KB
/
Copy pathserialization.cpp
File metadata and controls
127 lines (100 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (C) 2026 Vallés Puig, Ramon
/**
* @file serialization.cpp
* @example serialization.cpp
* @brief Demonstrates JSON serialization and deserialization of qtty
* quantities.
*
* Requires the qtty-ffi library built with the `qtty_serde` Cargo feature:
*
* cmake -B build -DQTTY_FFI_FEATURES=qtty_serde && cmake --build build
* ./build/serialization
*
* Features shown:
* - Compact value-only JSON via `to_json_value` / `from_json_value`
* - Self-documenting JSON (value + unit) via `to_json` / `from_json`
* - DerivedQuantity JSON round-trip
* - Constructing quantities from external JSON payloads
*/
#include "qtty/serialization.hpp"
#include "qtty/qtty.hpp"
#include <cassert>
#include <iomanip>
#include <iostream>
using namespace qtty;
using namespace qtty::serialization;
int main() {
std::cout << "=== qtty-cpp Serialization Demo ===" << std::endl;
std::cout << "FFI ABI Version: " << abi_version() << "\n" << std::endl;
// ── 1. Value-only JSON ──────────────────────────────────────────────────
std::cout << "1. Value-only JSON (compact, default serde format):" << std::endl;
auto distance = Meter(42.5);
auto time = Second(9.58);
auto mass = Kilogram(70.0);
std::string json_d = to_json_value(distance);
std::string json_t = to_json_value(time);
std::string json_m = to_json_value(mass);
std::cout << " " << distance << " → " << json_d << std::endl;
std::cout << " " << time << " → " << json_t << std::endl;
std::cout << " " << mass << " → " << json_m << std::endl;
// Deserialize back
Meter d_rt = from_json_value<Meter>(json_d);
Second t_rt = from_json_value<Second>(json_t);
Kilogram m_rt = from_json_value<Kilogram>(json_m);
std::cout << " (restored) " << d_rt << ", " << t_rt << ", " << m_rt << std::endl;
assert(std::abs(d_rt.value() - 42.5) < 1e-9);
assert(std::abs(t_rt.value() - 9.58) < 1e-9);
assert(std::abs(m_rt.value() - 70.0) < 1e-9);
// ── 2. Self-documenting JSON (value + unit name) ────────────────────────
std::cout << "\n2. Self-documenting JSON (value + unit name):" << std::endl;
auto wavelength = Nanometer(656.3); // Hα spectral line
auto period = Hour(24.0);
std::string full_d = to_json(wavelength);
std::string full_t = to_json(period);
std::cout << " " << wavelength << " → " << full_d << std::endl;
std::cout << " " << period << " → " << full_t << std::endl;
// Round-trip
Nanometer wl_rt = from_json<Nanometer>(full_d);
Hour p_rt = from_json<Hour>(full_t);
std::cout << " (restored) " << wl_rt << ", " << p_rt << std::endl;
assert(std::abs(wl_rt.value() - 656.3) < 1e-6);
assert(std::abs(p_rt.value() - 24.0) < 1e-9);
// ── 3. DerivedQuantity JSON round-trip ─────────────────────────────────────
std::cout << "\n3. DerivedQuantity JSON (velocity = length / time):" << std::endl;
auto velocity = MeterPerSecond(343.0); // speed of sound in air at 20 °C
std::string vel_json = derived_serialization::to_json(velocity);
std::cout << " " << velocity << " → " << vel_json << std::endl;
MeterPerSecond vel_rt = derived_serialization::from_json<MeterPerSecond>(vel_json);
std::cout << " (restored) " << vel_rt << std::endl;
assert(std::abs(vel_rt.value() - 343.0) < 1e-9);
// ── 4. Consuming external JSON payloads
// ─────────────────────────────────────
std::cout << "\n4. External JSON payloads:" << std::endl;
// Value-only
const std::string ext_val = "1.496e+11"; // 1 AU in metres
Meter au_m = from_json_value<Meter>(ext_val);
Kilometer au_km = au_m.to<Kilometer>();
std::cout << std::fixed << std::setprecision(3);
std::cout << " 1 AU from JSON → " << au_km << std::endl;
// Self-documenting
const std::string ext_full = R"({"value":3.085677581e+16,"unit":"Meter"})"; // 1 parsec
Meter pc_m = from_json<Meter>(ext_full);
std::cout << std::scientific << std::setprecision(4);
std::cout << " 1 pc from JSON → " << pc_m << std::endl;
// ── 5. New dimensions: area and energy ─────────────────────────────────
std::cout << "\n5. Area and Energy serialization:" << std::endl;
auto field = Hectare(2.5);
auto work = Kilojoule(8.4);
std::string field_json = to_json(field);
std::string work_json = to_json(work);
std::cout << " " << field << " → " << field_json << std::endl;
std::cout << " " << work << " → " << work_json << std::endl;
Hectare field_rt = from_json<Hectare>(field_json);
Kilojoule work_rt = from_json<Kilojoule>(work_json);
std::cout << " (restored) " << field_rt << ", " << work_rt << std::endl;
assert(std::abs(field_rt.value() - 2.5) < 1e-9);
assert(std::abs(work_rt.value() - 8.4) < 1e-9);
std::cout << "\n=== Serialization demo completed successfully ===" << std::endl;
return 0;
}