mercredi 2 janvier 2019

How to set array with lenght 0 to boost::property_tree nod?

I read users from database and using add_child to add array of users to parent boost::property_node result_node. when count of users are equal to zero,

the output is like this: "users":""

but I need to add something like this: "users":[]

boost::property_tree::ptree UserModel::GetUsersJson(int page,   std::string query){
    pqxx::result R = GetUsers(page, query);
    int result_count = GetUsersCount(query);
    int pageCount = (result_count / OFFSET_COUNT) + 1;

    boost::property_tree::ptree result_node;
    boost::property_tree::ptree info_node;
    boost::property_tree::ptree users_node;
    boost::property_tree::ptree user_node;

    boost::property_tree::ptree details_node;

    for (size_t i = 0; i < R.size(); i++) {
        user_node.put("id", R[i][0]);
        user_node.put("email", R[i][1]);
        user_node.put("password", R[i][2]);
        std::string details = R[i][3].c_str();
        if (!details.empty() && details != ""){
            std::stringstream ss;
        ss << R[i][3].c_str();
            boost::property_tree::read_json(ss, details_node);
            user_node.add_child("details", details_node);
        }
        user_node.put("created_at", R[i][4]);
        users_node.push_back(std::make_pair("", user_node));
    }
    info_node.put("page", page);
    info_node.put("offset", OFFSET_COUNT);
    info_node.put("page_count", pageCount);
    info_node.put("result_count", result_count);

    result_node.add_child("info", info_node);
    result_node.add_child("users", users_node);
    return result_node;
}

Aucun commentaire:

Enregistrer un commentaire