C++ json解决方案
前段时间用到C++来封装com 因此从数据转换上我采用的Json来当两种语言的传递方式,现做下json的序列化与反序列化方案的总结:
Rapidjson
文档地址:http://rapidjson.org/zh-cn/
使用体会:比C# 现有的各类Json库相比调用麻烦需要特别清楚整体结构。
序列化代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | rapidjson::Document jsonDoc; rapidjson::Document::AllocatorType &allocator = jsonDoc.GetAllocator(); //获取分配器 jsonDoc.SetArray(); for ( int i=0;i< facesPoint.size();i++) { Value faceArray(kObjectType); Value jawArray(kArrayType); for ( int j=0;j<facesPoint[i].jaw.size();j++) { Value pointobj(kObjectType); pointobj.AddMember( "x" , facesPoint[i].jaw[j].X, allocator); pointobj.AddMember( "y" , facesPoint[i].jaw[j].Y, allocator); jawArray.PushBack(pointobj, allocator); } faceArray.AddMember( "jaw" , jawArray, allocator); Value leftBroArray(kArrayType); for ( int j = 0; j<facesPoint[i].leftBrow.size(); j++) { Value pointobj(kObjectType); pointobj.AddMember( "x" , facesPoint[i].leftBrow[j].X, allocator); pointobj.AddMember( "y" , facesPoint[i].leftBrow[j].Y, allocator); leftBroArray.PushBack(pointobj, allocator); } faceArray.AddMember( "leftBrow" , leftBroArray, allocator); Value leftEyeArray(kArrayType); for ( int j = 0; j<facesPoint[i].leftEye.size(); j++) { Value pointobj(kObjectType); pointobj.AddMember( "x" , facesPoint[i].leftEye[j].X, allocator); pointobj.AddMember( "y" , facesPoint[i].leftEye[j].Y, allocator); leftEyeArray.PushBack(pointobj, allocator); } faceArray.AddMember( "leftEye" , leftEyeArray, allocator); Value mouthArray(kArrayType); for ( int j = 0; j<facesPoint[i].mouth.size(); j++) { Value pointobj(kObjectType); pointobj.AddMember( "x" , facesPoint[i].mouth[j].X, allocator); pointobj.AddMember( "y" , facesPoint[i].mouth[j].Y, allocator); mouthArray.PushBack(pointobj, allocator); } faceArray.AddMember( "mouth" , mouthArray, allocator); Value mouth2Array(kArrayType); for ( int j = 0; j<facesPoint[i].mouth2.size(); j++) { Value pointobj(kObjectType); pointobj.AddMember( "x" , facesPoint[i].mouth2[j].X, allocator); pointobj.AddMember( "y" , facesPoint[i].mouth2[j].Y, allocator); mouth2Array.PushBack(pointobj, allocator); } faceArray.AddMember( "mouth2" , mouth2Array, allocator); Value noseArray(kArrayType); for ( int j = 0; j<facesPoint[i].nose.size(); j++) { Value pointobj(kObjectType); pointobj.AddMember( "x" , facesPoint[i].nose[j].X, allocator); pointobj.AddMember( "y" , facesPoint[i].nose[j].Y, allocator); noseArray.PushBack(pointobj, allocator); } faceArray.AddMember( "nose" , noseArray, allocator); Value rightBrowArray(kArrayType); for ( int j = 0; j<facesPoint[i].rightBrow.size(); j++) { Value pointobj(kObjectType); pointobj.AddMember( "x" , facesPoint[i].rightBrow[j].X, allocator); pointobj.AddMember( "y" , facesPoint[i].rightBrow[j].Y, allocator); rightBrowArray.PushBack(pointobj, allocator); } faceArray.AddMember( "rightBrow" , rightBrowArray, allocator); Value rightEyeArray(kArrayType); for ( int j = 0; j<facesPoint[i].rightEye.size(); j++) { Value pointobj(kObjectType); pointobj.AddMember( "x" , facesPoint[i].rightEye[j].X, allocator); pointobj.AddMember( "y" , facesPoint[i].rightEye[j].Y, allocator); rightEyeArray.PushBack(pointobj, allocator); } faceArray.AddMember( "rightEye" , rightEyeArray, allocator); jsonDoc.PushBack(faceArray, allocator); } rapidjson::StringBuffer buffer; rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); jsonDoc.Accept(writer); |
JSON for Modern C++
文档地址:https://github.com/nlohmann/json
使用体会:目前为止找到比较简单粗暴的json解决方案代码也简单易懂
序列化前必要的模版声明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | namespace NaughtyKidFaceRectangle { struct FaceRectangle { public : double top; double bottom; double left; double right; double width; double height; FaceRectangle() {}; FaceRectangle( double _top, double _bottom, double _left, double _right, double _width, double _height) { top = _top; bottom = _bottom; left = _left; right = _right; width = _width; height = _height; } }; inline void to_json(nlohmann::json& j, const FaceRectangle& p) { j = nlohmann::json { { "top" , p.top}, { "bottom" , p.bottom} , { "left" , p.left }, { "right" , p.right }, { "width" , p.width }, { "height" , p.height } }; } inline void from_json( const nlohmann::json& j, FaceRectangle& p) { p.top = j.at( "top" ). get < double >(); p.bottom = j.at( "bottom" ). get < double >(); p.left = j.at( "left" ). get < double >(); p.right = j.at( "right" ). get < double >(); p.width = j.at( "width" ). get < double >(); p.height = j.at( "height" ). get < double >(); } } namespace NaughtyKid { struct NaughtyKidPoint { public : NaughtyKidPoint() {}; NaughtyKidPoint( double _x, double _y) { x = _x; y = _y; } NaughtyKidPoint( double _x, double _y, int _index) { x = _x; y = _y; index = _index; } double x; double y; int index; }; void to_json(nlohmann::json& j, const NaughtyKidPoint& p) { j = nlohmann::json{ { "x" , p.x },{ "y" , p.y } ,{ "index" ,p.index}}; } void from_json( const nlohmann::json& j, NaughtyKidPoint& p) { p.x = j.at( "x" ). get < double >(); p.y = j.at( "y" ). get < double >(); p.index = j.at( "index" ). get < double >(); } } using namespace NaughtyKid; namespace dlib_face { struct dlib_face { public : std::vector<NaughtyKidPoint> jaw; //下巴 std::vector<NaughtyKidPoint> rightBrow; //右眉毛 std::vector<NaughtyKidPoint> leftBrow; //左眉毛 std::vector<NaughtyKidPoint> nose; //鼻子 std::vector<NaughtyKidPoint> rightEye; //右眼 std::vector<NaughtyKidPoint> leftEye; //左眼 std::vector<NaughtyKidPoint> mouth; //上嘴唇 std::vector<NaughtyKidPoint> mouth2; //下嘴唇 double face_width; double face_height; }; struct dlib_facedetails { public : dlib_face featurepoint; double angleofface; }; inline void to_json(nlohmann::json& j, const dlib_facedetails& p) { j = nlohmann::json { { "featurepoint" ,p.featurepoint}, { "angleofface" ,p.angleofface} }; } inline void from_json( const nlohmann::json& j, dlib_facedetails& p) { p.featurepoint = j.at( "featurepoint" ). get <dlib_face>(); p.angleofface = j.at( "angleofface" ). get < double >(); } inline void to_json(nlohmann::json& j, const dlib_face& p) { j = nlohmann::json{ { "jaw" , p.jaw }, { "rightBrow" , p.rightBrow }, { "leftBrow" , p.leftBrow }, { "nose" ,p.nose }, { "rightEye" ,p.rightEye }, { "leftEye" ,p.leftEye }, { "mouth" ,p.mouth }, { "mouth2" ,p.mouth2 }, { "face_width" ,p.face_width}, { "face_height" ,p.face_height} }; } inline void from_json( const nlohmann::json& j, dlib_face& p) { p.jaw = j.at( "jaw" ). get <std::vector<NaughtyKidPoint>>(); p.rightBrow = j.at( "rightBrow" ). get <std::vector<NaughtyKidPoint>>(); p.leftBrow = j.at( "leftBrow" ). get <std::vector<NaughtyKidPoint>>(); p.nose = j.at( "nose" ). get <std::vector<NaughtyKidPoint>>(); p.rightEye = j.at( "rightEye" ). get <std::vector<NaughtyKidPoint>>(); p.leftEye = j.at( "leftEye" ). get <std::vector<NaughtyKidPoint>>(); p.mouth = j.at( "mouth" ). get <std::vector<NaughtyKidPoint>>(); p.mouth2 = j.at( "mouth2" ). get <std::vector<NaughtyKidPoint>>(); p.face_width = j.at( "face_width" ). get < double >(); p.face_height = j.at( "face_height" ). get < double >(); } } |
序列化部分代码:
1 2 3 4 5 6 7 | std::vector<NaughtyKidFaceRectangle::FaceRectangle> faces; nlohmann::json j = faces; std::ostringstream oss; oss << j << endl; |
反序列化代码:
1 2 3 | const auto js = nlohmann::json::parse(sp.c_str()); const dlib_face::dlib_face ddlibfaces = js; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗