Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <nlohmann/json.hpp>
- #include "boost/any.hpp"
- using json = nlohmann::json;
- namespace exchangable_data {
- class user_data {
- public:
- user_data() : id(0) {}
- int id;
- };
- class inserction : public user_data {
- public:
- inserction(std::string&& data,int position) : data(data),position(position) {}
- std::string data;
- int position;
- };
- class deletion : public user_data {
- public:
- deletion(int length,int position) : length(length),position(position) {}
- int position;
- int length;
- };
- void to_json(json& j,const deletion& d){
- j = json{{"position",d.position},{"length",d.length}};
- }
- void from_json(const json& j,deletion& d){
- j.at("position").get_to(d.position);
- j.at("length").get_to(d.length);
- }
- void to_json(json& j, const inserction& i) {
- j = json{{"data", i.data}, {"position", i.position}};
- }
- void from_json(const json& j, inserction& i) {
- j.at("data").get_to(i.data);
- j.at("position").get_to(i.position);
- j.at("id").get_to(i.id);
- }
- void to_json(const json& j,user_data){
- }
- }
- int main() {
- exchangable_data::inserction i1 = {"ciao!", 60};
- exchangable_data::deletion d1 = {2,4};
- exchangable_data::inserction i2 = {"ciao!", 60};
- exchangable_data::deletion d2 = {2,4};
- std::vector<boost::any> vect;
- vect.push_back(i1);
- vect.push_back(d1);
- vect.push_back(i2);
- vect.push_back(d2);
- json j;
- for (auto v : vect){
- json t;
- if (v.type() == typeid(exchangable_data::inserction)){
- exchangable_data::inserction i = boost::any_cast<exchangable_data::inserction>(v);
- t = i;
- } else {
- exchangable_data::deletion d = boost::any_cast<exchangable_data::deletion>(v);
- t = d;
- }
- j.push_back(t);
- }
- std::cout << j;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement