[CXX-1652] bsoncxx::to_json() do not support GBK Created: 14/Sep/18  Updated: 27/Oct/23  Resolved: 15/Sep/18

Status: Closed
Project: C++ Driver
Component/s: BSON
Affects Version/s: 3.2.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: haijiwei Assignee: A. Jesse Jiryu Davis
Resolution: Works as Designed Votes: 0
Labels: bson, gbk
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

win10--x64-Debug mongocxx-driver-3.2.0


Attachments: PNG File 2018-09-14_171126.png     PNG File Pic2.png    

 Description   

New to MongoDB. I have already install mongocxx-driver-3.2.0 successfully in my PC. But when i run my code as follows, this error happened:
the console outputs the invalid bson. What should i do to solve it?

int main() {
	bsoncxx::builder::stream::document stream_doc;
	stream_doc << "_id" << 1 << "name" << open_document
		<< "first" << "John" << "last" << "Backus" << close_document
		<< "contribs" << open_array << "Fortran" << "AlGOL" << "Backus-Naur Form" << "FP" << close_array
		<< "awards"
		<< open_array
		<< open_document
		<< "award" << "W.W. McDowell Award"
		<< "year" << 1967
		<< "by" << "IEEE Computer Society"
		<< close_document
		<< open_document
		<< "award" << "Draper Prize"
		<< "year" << 1993
		<< "by" << "你好"
		<< close_document
		<< close_array;
 
	bsoncxx::document::view view = stream_doc.view();
	std::cout << bsoncxx::to_json(view) << std::endl;
        return 0;
}



 Comments   
Comment by A. Jesse Jiryu Davis [ 15/Sep/18 ]

I'm glad you solved it! I didn't know about GBK encoding until now. As you have learned, BSON strings must be UTF8 encoded.

Comment by haijiwei [ 15/Sep/18 ]

Ok, I use a function convert gbk to utf8 to solve it! @A. Jesse Jiryu Davis , Thanks you for your answer!!

Comment by haijiwei [ 14/Sep/18 ]

@A. Jesse Jiryu Davis , In the Pic2 I posted, the Key-Value pair I emphasized with a red line, it seems that the value garbled in console.

Comment by A. Jesse Jiryu Davis [ 14/Sep/18 ]

Hi. In the screenshot you attached, the JSON output looks correct to me. What error do you see? What is the driver doing that is different from what you expect it to do?

Generated at Wed Feb 07 22:03:27 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.