I have a json containing variables of different types, something like this
varlist: [{
"name": "var_1",
"type": "double",
"value": 0.45
},
{
"name": "var_2",
"type": "int32_t",
"value": 32
}
]
is there any way, I can create variables by reading from json at runtime.
I thought of creating vector of different types, but there would be lot of different vectors. (just for storing value)
vector<uint8_t> something_1;
vector<uint16_t> something_2;
...... //all others
I have also tried creating struct, like:
enum type_enum{uint8_t, uint16_t, ......}
struct{
std::string name;
type_enum type;
union{
uint8_t,
uint16_t,
....
}mn;
}
I am using enum for accessing value from union, but for each time if i want some value from particular struct object, I am writing switch block for all available data types.
for e.g.
//(for now i know that var1_value is of type uint32_t) but how to create it at runtime.
uint32_t var1_value = var1_struc.mn.(specific_value) //using switch block(with enum..)
uint32_t var2_value = var2_struc.mn.(--------)
uint32_t calc_value = var1_value + var2_value;
I just wanted to know if there any better solution for achieving this (creating these variables from json at runtime) using c++.