StefanoBelli / xxtelebot

**DISCONTINUED** A simple C++11 Telegram Bot API implementation
https://stefanobelli.github.io/xxtelebot
MIT License
42 stars 6 forks source link

Creating objects of API-interacting types. #17

Closed Graullon closed 6 years ago

Graullon commented 6 years ago

Hello! The main idea what I want to do is: save the object of structure 'User' to the file and than read saved information back. Step with saving info is easy and clean, but when I try to do opposite things the problems appear. The problem is because I can't find a way and understand what exactly parameters are all API-interacting types needs in constructor? In documentation they are (for example structure User) User (const Json::Value &object). And what is this Json::Value I can't understand and as a conclusion, I can't create object. Can you please take a look and tell what to do? I will also attach some code below.

Write to file:

std::fstream write_file("users_database.txt", std::ios_base::out | std::ios_base::app);

    write_file << user_to_write.firstName << ' ' << *(user_to_write.lastName.get()) << ' ' 
        << *(user_to_write.username.get()) << ' '
        << user_to_write.id << ' ' << user_to_write.isBot << "\n\n";

    write_file.close();

Read from file and what I can't do:

std::fstream read_file("users_database.txt", std::ios_base::in);

    std::string temporary;

    User tmp_user{???}; // What to send to constructor????

    /*Some actions to copy everything into tmp_user*/

    list_of_users.push_back( std::move(tmp_user) ); //std::vector<User> list_of_users
    read_file.close();

Thanks in advance!

StefanoBelli commented 6 years ago

@Graullon I am not at home, I'll answer this night

StefanoBelli commented 6 years ago

I'll try from my mobile:

These types aren't meant to be constructed by the user itself, but anyway, you can construct this object by passing a valid Json::Value object containing the json object for user object

const std::string serializedJson = "{ ...";
Json::Reader reader;
Json::Value userObject;

reader.parse(serializedJson,userObject);

User u(userObject);

So you have to store the JSON serialized object into your file

Sorry for the inconvenience.

Graullon commented 6 years ago

Yes, I have already tried next solution for 'User':

Json::Value value(Json::objectValue);
    value["first_name"] = "";
    value["last_name"] = "";
    value["username"] = "";
    value["language_code"] = "";
    value["id"] = 0;
    value["is_bot"] = false;

and just passed it as argument. Thank you a lot for your time, maybe it also will be good to make one more constructor, that will allow to construct those objects from specified values, take a look at this proposition :)

StefanoBelli commented 6 years ago

@Graullon It worked?

Graullon commented 6 years ago

@StefanoBelli, yes, it worked.

StefanoBelli commented 6 years ago

Closing issue