What is best way to parse Json object we get from API response and remove it?

what is the best way to parse JSON object we get from API response and remove it so it won’t live in RAM

we are doing like this, do I need to do any suggestion please cz, me really having problem with memory

void TitleScene::sendApplicantJoinClub(std::string clubToJoin, std::string Verificationinfo){

    httpRequestNumber += 1;
    log("httpRequestNumber : %d \n",httpRequestNumber);
    rapidjson::Document d;
    d.SetObject();
    rapidjson::Document::AllocatorType& allocator = d.GetAllocator();
    d.AddMember("name",  rapidjson::Value(clubToJoin.c_str() , allocator) ,d.GetAllocator()) ;
    d.AddMember("verification_info",  rapidjson::Value(Verificationinfo.c_str() , allocator) ,d.GetAllocator()) ;

    // Convert JSON document to string
    rapidjson::StringBuffer strbuf;
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(strbuf);
    d.Accept(writer);

    auto request = new (std :: nothrow) cocos2d::network::HttpRequest();
    request->setUrl(SendApplicantJoinClubURL);
    request->setRequestType(cocos2d::network::HttpRequest::Type::POST);
    request->setResponseCallback(CC_CALLBACK_2 (TitleScene::sendApplicantJoinClubAPIResponse, this));
    std::vector<std::string> headers;
    headers.emplace_back(CONTENT_TYPE);
    headers.emplace_back(X_AUTO_TOKEN);
    request->setHeaders(headers);
    request->setRequestData(strbuf.GetString(), strbuf.GetSize());
    cocos2d::network::HttpClient::getInstance()->sendImmediate(request);
    request->release();

}

void TitleScene::sendApplicantJoinClubAPIResponse(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response){

    httpRequestNumber -= 1;
    log("httpRequestNumber : %d \n",httpRequestNumber);
    std::string err;
    if(!response->isSucceed()){

        log("Respond Failed getClubListAPIResponse\n");
        return;
    }

    //Json made
    std::vector<char> *buffer = response->getResponseData();
    char *json = (char *) malloc(buffer->size() + 1);
    std::string s2(buffer->begin(), buffer->end());
    strcpy(json, s2.c_str());
    CCASSERT(err.empty(), err.c_str());

    /////////////////////////////////////////////////////////////////////
    // Need Document object
    rapidjson::Document jsonDoc;
    // data is like string above
    jsonDoc.Parse(json);
    // parse Status
    bool Status = jsonDoc["isValid"].GetBool();
    // parse RespondCode
    int RespondCode = jsonDoc["code"].GetInt();

    if (Status && RespondCode==200) {

        log("Succeeded in Join club : %ld\n", response->getResponseCode());
        getClubList();
        return;

    }else{

        const char * ErrorMessage = jsonDoc["message"].GetString();
        log(" ErrorMessage : %s \n ResponceCode : %d \n",ErrorMessage,RespondCode);
    }
}

You allocate memory but do not free it!
Actually you don’t need that additional two strings at all.

response->getResponseData()->push_back(0);
jsonDoc.Parse(response->getResponseData()->data());
1 Like

hi, thanks for the reply,
@slackmoehrle

will it stay in memory until I close game?

and what about this one if I parse data like this do I need to do anything extra to free it or it will automatically from the heap, actually we are developing a multiplayer poker game so it works on WebSocket and so we keep getting data and need to parse it,

we trying below code will it work, any suggestion for that if you can

void GameScene2::onMessage(cocos2d::network::WebSocket* ws, const cocos2d::network::WebSocket::Data& data)
{
    
    auto text = std::string(data.bytes);
    log("message : %s\n", text.c_str());
    KPLog::getInstance()->Logger(text);

    if (data.len <= 0) {

        log("message : empty in websocket ");
        return;

    }

    rapidjson::Document jsonDoc;
    jsonDoc.Parse(data.bytes);

}

Yes

That code should work.

1 Like

thanks @dimon4eg

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.