프로그래밍/C++

[C++, rapidjson] AddMember 시의 형변환 문제

Who is JMH 2019. 5. 14. 13:06

rapidjson 을 사용해 array 형태의 json message 를 만들다보니..

 

내가 원하는건 std::string 을 사용해서 A :{[B:C, D:F]} 형태로 모두 string 으로 이뤄진 배열을 만드는 것.

 

rapidjson::Document tempres;
tempres.SetObject();
rapidjson::Value integrityArray(rapidjson::kArrayType);
rapidjson::Document::AllocatorType& allocator = tempres.GetAllocator();

for (auto iter : *objArray) {
  rapidjson::Value objValue;
  rapidjson::Value strValue;
  objValue.SetObject();

  strValue.SetString(iter.Name.c_str(), iter.Name.length(), allocator);
  objValue.AddMember("filePath", strValue, allocator); //처음엔 strValue 안쓰고, 그냥 바로 iter.Name 넣었는데 안됨.
  strValue.SetString(iter.NewHash.c_str(), iter.NewHash.length(), allocator);
  objValue.AddMember("hash", strValue, allocator);
  integrityArray.PushBack(objValue, allocator);
}

 

이렇게 하면, AddMember 쪽에서 GenericValue 부분의 T를 형변환을 제대로 하지 못해서 error가 발생.

 

그래서 검색하다보니...

 

rapidjson 을 include 하기 전에,

 

#define RAPIDJSON_HAS_STDSTRING 1

 

위의 한 줄만 define 해주니까 error 해결. 일단은 넘어갔다!!

 

몇 시간 삽질을 한건지... 별 수를 다 써봐도 안되던게 해결됨.

 

 

출처 : https://stackoverflow.com/questions/34573940/how-can-i-add-string-pairs-to-a-document-of-rapidjson