Writing a json object into a file

Feb 11, 2016 at 3:47 PM
Hi, using REST sdk (I'm relatively new to it), I have created a client/server system, in which the client sends data using the POST method, now on the server side, I want to write the data (json object) into a file "data.txt"
how can I do it?

handler
void handle_request(http_request request, std::function<void(json::value &, json::value &)> action)
{
    json::value  answer;

    request
        .extract_json()
        .then([&answer, &action](pplx::task<json::value> task) {
        try
        {
            auto & jvalue = task.get();

            if (!jvalue.is_null())
            {
                action(jvalue, answer);
            }
        }
        catch (http_exception const & e)
        {
            std::wcout << e.what() << std::endl;
        }
    })
        .wait();

    request.reply(status_codes::OK, answer);
}
POST method
void handle_post(http_request request)
{
    TRACE("\nMessage Recived With Type: POST \n");
    handle_request(
        request,
        [&](json::value & jvalue, json::value & answer)
    {
        auto reason = 0;
        auto messageType = jvalue.at(U("MessageType"));

        if (messageType == MTYPE_LOGIN) 
        {
            TRACE("\nClient Login Recived\n");

            auto messageName = jvalue.at(U("LoginMessage"));
            auto userID = messageName.at(U("UserID"));

            if (!serverObject.IsClientlogged(userID))
            {
                auto userName = messageName.at(U("UserName"));
                auto userPassword = messageName.at(U("Password"));

                auto clientArray = serverObject.clientData.at(U("Client"));

                for (auto i = 0; i < clientArray.as_array().size(); i++)
                {
                    auto tempUserName = clientArray[i].at(U("Username"));
                    auto tempUserID = clientArray[i].at(U("UserID"));
                    if (userID == tempUserID)
                    {
                        if (userName == tempUserName)
                        {
                            auto tempUserPassword = clientArray[i].at(U("Password"));
                            if (userPassword == tempUserPassword)
                            {
                                serverObject.loggedInClients.push_back(tempUserID);
                                reason = LOGIN_SUCCESS;
                            }
                        }
                    }
                }
            }
            else
            {
                reason = LOGIN_FAIL_USER_ALREADY_LOGGEDIN;
            }
        }
        else if (messageType == MTYPE_PROCESSDATA)
        {
            TRACE("\nProcess Data Recived\n");

            auto userID = jvalue.at(U("UserID"));

            if (serverObject.IsClientlogged(userID))
            {
                serverObject.processData = jvalue;

                auto tarray = jvalue.at(U("Processes"));
                auto cmemory = jvalue.at(U("TotalConsumedMemory"));
                auto fmemory = jvalue.at(U("TotalFreeMemory"));
                auto tmemory = jvalue.at(U("TotalMemory"));

                /////////////////////////////////
                //Write to a file here
                /////////////////////////////////

                if (!serverObject.CheckForAlert(userID.as_integer(), tarray.as_array().size(), tmemory.as_double(), fmemory.as_double()))
                {
                    reason = PROCESS_DATA_MAIL_SEND_FAIL;
                }
                else
                {
                    reason = PROCESS_DATA_SUCCESS;
                }
            }
            else
            {
                reason = PROCESS_DATA_UNKNOWN_USER;
            }
        }

        switch (reason)
        {
        case LOGIN_SUCCESS: answer = json::value::string(U("Login Success!"));
            break;
        case LOGIN_FAIL_USER_ALREADY_LOGGEDIN: answer = json::value::string(U("Login Fail! User Already LoggedIn"));
            break;
        case LOGIN_FAIL_INVALID_USER: answer = json::value::string(U("Login Fail! Unkown Username/Password"));
            break;
        case PROCESS_DATA_SUCCESS: answer = json::value::string(U("Process Data Success! Data Recived Correctly"));
            break;
        case PROCESS_DATA_UNKNOWN_USER: answer = json::value::string(U("Process Data Fail! Data sent by not Logggin User/Unkown User"));
            break;
        case PROCESS_DATA_MAIL_SEND_FAIL: answer = json::value::string(U("Process Data Fail! Server Side Failure to send Alert Mail to User"));
            break;
        default: {  std::wcout << "Unknown Message Received" << std::endl;
            answer = json::value::string(U("Unknown Message Sent!"));
                }
        }
    }
    );
}
Main
void  main()
{
    http_listener listener(L"http://localhost/Server");
    
    listener.support(methods::GET, handle_get);
    listener.support(methods::POST, handle_post);
    //listener.support(methods::PUT, handle_put);
    //listener.support(methods::DEL, handle_del);

    try
    {
        listener
            .open()
            .then([&listener]() {TRACE(L"\nstarting to listen\n"); })
            .wait();

        while (true);
    }
    catch (std::exception const & e)
    {
        std::wcout << e.what() << std::endl;
    }

    _getch();
}
Feb 12, 2016 at 5:58 AM
After hours of searching, I figured out a way.. :)
void Server::WriteToFile(json::value userID)
{
    std::string fileName = CreateFileName(userID.as_integer());
    auto tarray = processData.at(U("Processes"));

    try {
        utility::ofstream_t of(fileName);
        if (of.is_open())
        {
            of << L"User ID: " << processData.at(U("UserID")) << "\n";
            of << L"TotalMemory: " << processData.at(U("TotalMemory"))<<"\n";
            of << L"Total Consumed Memory: " << processData.at(U("TotalConsumedMemory")) << "\n";;
            of << L"Total Free Memory: " << processData.at(U("TotalConsumedMemory")) << "\n";

            for (auto i = 0; i < tarray.as_array().size(); i++)
            {
                of << "-----------------------------------------------\n";
                of << "No: " << i+1;
                of << " ProcessID:" << tarray[i].at(U("ProcessID"));
                of << " ProcessName:" << tarray[i].at(U("ProcessName"));
                of << " MemoryUsed:" << tarray[i].at(U("MemoryUsed")) << "MB\n";
                of << "-----------------------------------------------\n";
            }
        }
        
        of.close();                                        
        std::cout << "\n Process Data written in file: " << fileName << "\n";
    }
    catch (web::json::json_exception e) 
    {
        std::wcout << "ERROR Parsing JSON file: ";
        std::wcout << e.what() << "\n";
    }
}