
MongoDBBSONDocument::toCanonicalExtendedJSON 메소드를 사용하여 CANONICAL EXTENDED JSON을 생성할 수 있습니다. 이 메소드는 BSON 문서를 CANONICAL EXTENDED JSON 형식으로 변환하는 데 사용됩니다.
이 메소드를 사용하여 CANONICAL EXTENDED JSON을 생성할 때, 다음과 같은 옵션을 사용할 수 있습니다.
- indentation: CANONICAL EXTENDED JSON의 들여쓰기 수준을 지정합니다. 기본값은 0입니다.
- sortKeys: 키를 정렬할지 여부를 지정합니다. 기본값은 false입니다.
- eol: 줄바꿈 문자를 지정합니다. 기본값은 "\n"입니다.
CANONICAL EXTENDED JSON을 생성하는 예제 코드는 다음과 같습니다.
#hostingforum.kr
cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main() {
// MongoDB 클라이언트 인스턴스 생성
mongocxx::instance inst;
mongocxx::client conn{mongocxx::uri{}};
// BSON 문서 생성
bsoncxx::document::value doc = bsoncxx::document::builder{}
<< "name" << "John Doe"
<< "age" << 30
<< "address" << bsoncxx::document::builder{}
<< "street" << "123 Main St"
<< "city" << "New York"
<< "state" << "NY"
<< "zip" << "10001"
.get();
// CANONICAL EXTENDED JSON 생성
bsoncxx::json::view_or_value json = doc.toCanonicalExtendedJSON(4, true, "n");
// CANONICAL EXTENDED JSON 출력
std::cout << json << std::endl;
return 0;
}
이 코드는 MongoDB 클라이언트 인스턴스를 생성하고 BSON 문서를 생성한 다음, CANONICAL EXTENDED JSON을 생성하고 출력합니다. `toCanonicalExtendedJSON` 메소드는 `indentation` 옵션을 4로 설정하고 `sortKeys` 옵션을 true로 설정하여 CANONICAL EXTENDED JSON을 생성합니다.
2025-08-12 07:19