Skip to content

Commit c4b7d53

Browse files
committed
added reverse mapping of c++ structure on json data
1 parent 9a38ac3 commit c4b7d53

File tree

13 files changed

+809
-112
lines changed

13 files changed

+809
-112
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
cmake_minimum_required(VERSION 3.5)
2-
2+
33
project(struct_mapping LANGUAGES CXX)
44

55
set(LIB_MAJOR_VERSION "0")
6-
set(LIB_MINOR_VERSION "1")
6+
set(LIB_MINOR_VERSION "2")
77
set(LIB_PATCH_VERSION "0")
88
set(LIB_VERSION "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_PATCH_VERSION}")
99

@@ -21,6 +21,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
2121
set(WARNINGS "${WARNINGS} -Wno-unused-member-function")
2222
set(WARNINGS "${WARNINGS} -Wno-ctad-maybe-unsupported")
2323
set(WARNINGS "${WARNINGS} -Wno-undef")
24+
set(WARNINGS "${WARNINGS} -Wno-double-promotion")
2425

2526
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
2627

@@ -36,7 +37,6 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
3637
set(WARNINGS "${WARNINGS} -Wconversion")
3738
set(WARNINGS "${WARNINGS} -Wsign-conversion")
3839
set(WARNINGS "${WARNINGS} -Wnull-dereference")
39-
set(WARNINGS "${WARNINGS} -Wdouble-promotion")
4040
set(WARNINGS "${WARNINGS} -Wformat=2")
4141
set(WARNINGS "${WARNINGS} -Wduplicated-cond")
4242
set(WARNINGS "${WARNINGS} -Wduplicated-branches")

changelog.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).
3+
4+
## 0.2.0 - 2020.06.14
5+
6+
### Added
7+
* reverse mapping of c++ structure on json data
8+
9+
## 0.1.0 - 2020.06.10
10+
11+
Initial. Mapping data in json format on a c++ structure.

example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(EXAMPLES
66
person
77
simple
88
struct
9+
struct_to_json
910
)
1011

1112
include_directories("../include")
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <iostream>
2+
#include <sstream>
3+
4+
#include "struct_mapping/struct_mapping.h"
5+
6+
#define MANAGED_STRUCT_NAME OceanPart
7+
BEGIN_MANAGED_STRUCT
8+
9+
MANAGED_FIELD(std::string, name)
10+
MANAGED_FIELD(double, average_depth)
11+
12+
END_MANAGED_STRUCT
13+
#undef MANAGED_STRUCT_NAME
14+
15+
#define MANAGED_STRUCT_NAME OceanColor
16+
BEGIN_MANAGED_STRUCT
17+
18+
MANAGED_FIELD(std::string, name)
19+
20+
END_MANAGED_STRUCT
21+
#undef MANAGED_STRUCT_NAME
22+
23+
24+
#define MANAGED_STRUCT_NAME Ocean
25+
BEGIN_MANAGED_STRUCT
26+
27+
MANAGED_FIELD(double, water_volume)
28+
MANAGED_FIELD(long long, surface_area)
29+
MANAGED_FIELD(bool, liquid)
30+
MANAGED_FIELD(std::string, name)
31+
32+
MANAGED_FIELD_STRUCT(OceanColor, color)
33+
34+
MANAGED_FIELD_ARRAY(OceanPart, parts)
35+
36+
END_MANAGED_STRUCT
37+
#undef MANAGED_STRUCT_NAME
38+
39+
40+
#define MANAGED_STRUCT_NAME Planet
41+
BEGIN_MANAGED_STRUCT
42+
43+
MANAGED_FIELD(bool, giant)
44+
MANAGED_FIELD(long long, surface_area)
45+
MANAGED_FIELD(double, mass)
46+
MANAGED_FIELD(double, volume)
47+
MANAGED_FIELD(long long, orbital_period)
48+
MANAGED_FIELD(std::string, name)
49+
MANAGED_FIELD(bool, terrestrial)
50+
MANAGED_FIELD(std::string, shape)
51+
52+
MANAGED_FIELD_STRUCT(Ocean, ocean)
53+
54+
END_MANAGED_STRUCT
55+
#undef MANAGED_STRUCT_NAME
56+
57+
int main() {
58+
Planet earth;
59+
60+
earth.giant = false;
61+
earth.terrestrial = true;
62+
earth.surface_area = 510072000;
63+
earth.orbital_period = 365 * 24 * 3600;
64+
earth.mass = 5.97237e24;
65+
earth.name = "Terra";
66+
earth.volume = 1.08321e12;
67+
earth.shape = "nearly spherical";
68+
69+
earth.ocean.water_volume = 1332000000;
70+
earth.ocean.surface_area = 361132000;
71+
earth.ocean.liquid = true;
72+
earth.ocean.name = "World Ocean";
73+
earth.ocean.color.name = "blue";
74+
75+
OceanPart pacific;
76+
pacific.name = "Pacific Ocean";
77+
pacific.average_depth = 4.280111;
78+
79+
OceanPart atlantic;
80+
atlantic.name = "Atlantic Ocean";
81+
atlantic.average_depth = 3.646;
82+
83+
earth.ocean.parts.get_data().push_back(pacific);
84+
earth.ocean.parts.get_data().push_back(atlantic);
85+
86+
std::ostringstream json_data;
87+
struct_mapping::mapper::map_struct_to_json(earth, json_data, " ");
88+
89+
std::cout << json_data.str() << std::endl;
90+
}

include/struct_mapping/managed/array.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <type_traits>
88
#include <vector>
99

10+
#include "fs.h"
1011
#include "../debug.h"
1112
#include "../exception.h"
1213

@@ -19,6 +20,19 @@ class ManagedArray {
1920
return data;
2021
}
2122

23+
void iterate_over(std::string const & name) {
24+
if constexpr (struct_mapping::debug_Q5w6E7r8) std::cout << "struct_mapping: ManagedArray::iterate_over: " << name << std::endl;
25+
struct_mapping::managed::Fs_iterate_over::start_array(name);
26+
for (auto v : data) {
27+
if constexpr (std::is_same_v<bool, T>) {struct_mapping::managed::Fs_iterate_over::set_bool("", v);}
28+
else if constexpr (std::is_same_v<std::string, T>) {struct_mapping::managed::Fs_iterate_over::set_string("", v);}
29+
else if constexpr (std::is_floating_point_v<T>) {struct_mapping::managed::Fs_iterate_over::set_floating_point("", v);}
30+
else if constexpr (std::is_integral_v<T>){struct_mapping::managed::Fs_iterate_over::set_integral("", v);}
31+
else v.iterate_over("");
32+
}
33+
struct_mapping::managed::Fs_iterate_over::end_array();
34+
}
35+
2236
bool release() {
2337
if constexpr (struct_mapping::debug_Q5w6E7r8) std::cout << "struct_mapping: ManagedArray::release: " << std::endl;
2438
if (!f_use) return true;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#ifndef STRUCT_MAPPING_FS_H
2+
#define STRUCT_MAPPING_FS_H
3+
4+
#include <functional>
5+
#include <string>
6+
#include <unordered_map>
7+
#include <unordered_set>
8+
9+
namespace struct_mapping::managed {
10+
11+
template<typename F>
12+
class Fs_set_field {
13+
public:
14+
static void add(std::string const & name, F f) {
15+
if (fs.find(name) == fs.end()) fs[name] = f;
16+
}
17+
18+
static inline std::unordered_map<std::string, F> fs;
19+
};
20+
21+
template<typename F>
22+
class Fs_set {
23+
public:
24+
static void add(std::string const & name, F f) {
25+
if (fs.find(name) == fs.end()) fs[name] = f;
26+
}
27+
28+
static inline std::unordered_map<std::string, F> fs;
29+
};
30+
31+
template<typename F>
32+
class Fs_use {
33+
public:
34+
static void add(std::string const & name, F f) {
35+
if (fs.find(name) == fs.end()) fs[name] = f;
36+
}
37+
38+
static inline std::unordered_map<std::string, F> fs;
39+
};
40+
41+
template<typename F>
42+
class Fs_release {
43+
public:
44+
static void add(std::string const & name, F f) {
45+
if (fs.find(name) == fs.end()) fs[name] = f;
46+
}
47+
48+
static inline std::unordered_map<std::string, F> fs;
49+
};
50+
51+
class Fs_iterate_over {
52+
public:
53+
using SetBool = void(std::string const &, bool);
54+
using SetIntegral = void(std::string const &, long long);
55+
using SetFloatingPoint = void(std::string const &, double);
56+
using SetString = void(std::string const &, std::string const &);
57+
using StartStruct = void(std::string const &);
58+
using EndStruct = void();
59+
using StartArray = void(std::string const &);
60+
using EndArray = void();
61+
62+
static inline std::function<SetBool> set_bool;
63+
static inline std::function<SetIntegral> set_integral;
64+
static inline std::function<SetFloatingPoint> set_floating_point;
65+
static inline std::function<SetString> set_string;
66+
static inline std::function<StartStruct> start_struct;
67+
static inline std::function<EndStruct> end_struct;
68+
static inline std::function<StartArray> start_array;
69+
static inline std::function<EndArray> end_array;
70+
};
71+
72+
template<typename T>
73+
class Fs_iterate_over_fields {
74+
public:
75+
using IterateOver = std::function<void(T&)>;
76+
static void add(std::string const & name, IterateOver f) {
77+
if (names.find(name) == names.end()) {
78+
names.insert(name);
79+
fs.push_back(f);
80+
}
81+
}
82+
83+
static inline std::unordered_set<std::string> names;
84+
static inline std::vector<IterateOver> fs;
85+
};
86+
87+
}
88+
89+
#endif

0 commit comments

Comments
 (0)