2017-04-18 13 views
1

현재 CPPRESTSDK (a.k.a. Casablanca)를 사용하여 API의 서버 측을 작성하면서 C++ 프로젝트를 작성 중입니다.
int, double 등의 값을 직렬화하는 작업은 이미 cpprestsdk 라이브러리에 구현되어 있습니다.CPPREST-SDK를 사용하여 std :: vector를 JSON으로 변환

클라이언트에서 std::vectorjson::value으로 serialize 할 수있는 방법이 있으면 요청하고 서버에서 역 직렬화할까요? 같은
뭔가 :

http_client client(U("http://localhost")); 
    json::value jsonVector(std::vector); 
    make_task_request(client, methods::POST, jsonVector) 

사전에 모든 주셔서 감사합니다!

답변

0

벡터 직렬화 : 당신은 컨테이너 개체로 배열을 밀어 필요가없는 경우

std::vector<int> someVector; 
    web::json::value json; 

    std::vector<value> array; 

    if (someVectory.size()) { 
     for (auto num : someVector) { 
      array.push_back(value(num)); 
     } 

     json["yourKey"] = value::array(array); 
    } 

, 그럼 그냥 배열에 std::vector 변환 value::array(array)를 사용합니다.

std::vector<int> intVector; 
    for (auto it=array.cbegin();it!=array.cend();++it) { 
     intVector.push_back(it->as_integer()); 
    } 
+0

또한 반복자 생성자를 사용하여 _std : 벡터 <웹 :: JSON :: 값> _으로 deserialze 수는 즉 : 역 직렬화하기

,의 당신이 다음 array에서 알려진 배열을 가정 해 봅시다 : _std :: vector vec (array.cbegin(), array.cend()); _. – Quirysse