{"id":15089313,"url":"https://github.com/xyz347/xpack","last_synced_at":"2025-04-12T21:33:51.571Z","repository":{"id":37021006,"uuid":"323058853","full_name":"xyz347/xpack","owner":"xyz347","description":"convert json/xml/bson to c++ struct","archived":false,"fork":false,"pushed_at":"2024-05-22T02:26:18.000Z","size":184,"stargazers_count":639,"open_issues_count":3,"forks_count":131,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-04-04T01:09:48.033Z","etag":null,"topics":["bson","cpp","json","mysql","sqlite","struct","xml","yaml"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xyz347.png","metadata":{"files":{"readme":"README-bson.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-20T11:45:10.000Z","updated_at":"2025-03-27T08:20:19.000Z","dependencies_parsed_at":"2023-02-19T07:31:13.712Z","dependency_job_id":"4658c3d1-01da-4750-8de2-1f7a56ad55d3","html_url":"https://github.com/xyz347/xpack","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xyz347%2Fxpack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xyz347%2Fxpack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xyz347%2Fxpack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xyz347%2Fxpack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xyz347","download_url":"https://codeload.github.com/xyz347/xpack/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248636348,"owners_count":21137433,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bson","cpp","json","mysql","sqlite","struct","xml","yaml"],"created_at":"2024-09-25T08:44:41.253Z","updated_at":"2025-04-12T21:33:51.549Z","avatar_url":"https://github.com/xyz347.png","language":"C++","funding_links":[],"categories":["C++"],"sub_categories":[],"readme":"* 用于在C++结构体和bson之间互相转换\n* 这个库可以配合[mongoxclient](https://github.com/xyz347/mongoxclient)使用\n\n# 目录\n- [encoder](#encoder)\n- [decoder](#decoder)\n- [bson types](#bsontypes)\n- [builder](#builder)\n- [重要说明](#重要说明)\n\n## encoder\n用于将结构体转成bson格式。范例：\n```C++\nstruct Test {\n\tint64_t     id;\n\tstd::string name;\n\tstd::string email;\n\tXPACK(A(id, \"bson:_id\"), O(name, email));\n};\n\nTest t;\nstd::string data = xpack::bson::encode(t); // data就是t转成bson的数据\n```\n\n## decoder\n用于将bson数据转成结构体\n用于将结构体转成bson格式。范例：\n```C++\nstruct Test {\n\tint64_t     id;\n\tstd::string name;\n\tstd::string email;\n\tXPACK(A(id, \"bson:_id\"), O(name, email));\n};\n\nstd::string data;\n// data = xxxx; data存储了相应的bson数据\nTest t;\nxpack::bson::decode(data, t);\n// xpack::bson::decode 还有另一个参数方式：static void decode(const uint8_t* data, size_t len, T \u0026val)\n```\n\n## bsontypes\n- date time: `bson_date_time_t`\n- timestamp: `bson_timestamp_t`\n- binary: `bson_binary_t`\n- regex: `bson_regex_t`\n- oid: `bson_oid_t`\n- decimal128: `bson_decimal128_t`\n\n前4种是xbson额外定义的，后面两种是定义在libbson内的，为了统一，前4种也是定义在全局命名空间。\n\n## builder\n**需要C++11或以上版本支持**。用json的格式来构造bson数据。查询接口中的query，一般只需要指定很少几个字段，这个场景用结构体表达不是很方便。用builder就会简单很多。基本用法是：\n1. 定义一个BsonBuilder：\n\t- 参数是一个json格式的字符串\n\t- 里面的双引号用单引号替代\n\t- 变量用问号作为占位符，问号后面可以加一个单词用来作为助记符，比如\"?\"和\"?uid\"是等效的\n\t- key也可以是变量，key作为变量，问号不用加引号\n\t- BsonBuilder是线程安全的，建议定义为static变量，这样json字符串只需解析一次\n范例：\n```C++\nstatic xpack::BsonBuilder bd(\"{'_id':?, ?:?}\"); // 带占位符的builder\nstatic xpack::BsonBuilder empty(\"{}\");   // 空bson\nstatic xpack::BsonBuilder student(\"{'type':'student'}\"); // 不带占位符的builder\n```\n\n2. 调用Encode函数来构造bson数据，Encode的参数对应的是各个占位符，因此参数个数应该和占位符一致\n比如对于上述的BsonBuilder\n```C++\nstd::string data1 = bd.Encode(12345, \"type\", \"master\");\nstd::string data2 = empty.Encode();\nstd::string data3 = student.Encode();\n// 这些bson的数据可以用在mongoxclient的各个接口中\n```\n\n3. 其他的一些API\n\t- EncodeAsJson 和Encode类似，但是输出的是json格式的数据，可以用于调试等场景（所以这个其实也是个JsonBuilder）\n\t- Error 如果构造BsonBuilder的json字符串有误，这个接口可以返回错误信息\n\t- static std::string En(const std::string\u0026fmt, Args... args)。这个是把构造BsonBuilder和Encode合在一起了，不是很建议使用，建议用于调试，因为每次都要构造一次BsonBuilder\n\n\n## 重要说明\n- 依赖于[libbson](https://github.com/mongodb/libbson/tree/1.0.0)，需自行安装\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxyz347%2Fxpack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxyz347%2Fxpack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxyz347%2Fxpack/lists"}