前段时间用到C++来封装com 因此从数据转换上我采用的Json来当两种语言的传递方式,现做下json的序列化与反序列化方案的总结:

Rapidjson

文档地址:http://rapidjson.org/zh-cn/

使用体会:比C# 现有的各类Json库相比调用麻烦需要特别清楚整体结构。

序列化代码:

                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解决方案代码也简单易懂

序列化前必要的模版声明

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>();
}
}

序列化部分代码:

        std::vector<NaughtyKidFaceRectangle::FaceRectangle> faces;

	nlohmann::json j = faces;

	std::ostringstream oss;

	oss << j << endl;

反序列化代码:

const auto js = nlohmann::json::parse(sp.c_str());

const dlib_face::dlib_face ddlibfaces = js;

C++ json解决方案的更多相关文章

  1. 报错需要选择一个空目录,或者选择的非空目录下存在 app.json 或者 project.config.json解决方案

    前言 小程序的第一个坑就是,创建了一个小程序项目,却在微信web开发者工具无法打开... 报了个错:需要选择一个空目录,或者选择的非空目录下存在 app.json 或者 project.config. ...

  2. iOS学习—JSON数据解析

      关于在iOS平台上进行JSON解析,已经有很多第三方的开源项目,比如TouchJson,JSONKit,SBJon等,自从iOS5.0以后,苹果SDK推出了自带的JSON解决方案NSJSONSer ...

  3. iOS开发——网络Swift篇&JSON与XML数据解析

    JSON与XML数据解析 JSON数据解析(内置NSJSONSerialization与第三方JSONKit)   一,使用自带的NSJSONSerialization 苹果从IOS5.0后推出了SD ...

  4. Swift - 解析JSON数据(内置NSJSONSerialization与第三方JSONKit)

    一,使用自带的NSJSONSerialization 苹果从IOS5.0后推出了SDK自带的JSON解决方案NSJSONSerialization,这是一个非常好用的JSON生成和解析工具,效率也比其 ...

  5. iOS学习——JSON数据解析(十一)

    在之前的<iOS学习——xml数据解析(九)>介绍了xml数据解析,这一篇简单介绍一下Json数据解析.JSON 即 JavaScript Object Natation,它是一种轻量级的 ...

  6. json解包与json封包

    首先,对两个名词进行简单的说明: 1.NSData 用来存储二进制的数据类型.NSData类提供了一种简单的方式,它用来设置缓冲区.将文件的内容读入缓冲区,或将缓冲区的内容写到一个文件.不变缓冲区(N ...

  7. Akka(33): Http:Marshalling,to Json

    Akka-http是一项系统集成工具.这主要依赖系统之间的数据交换功能.因为程序内数据表达形式与网上传输的数据格式是不相同的,所以需要对程序高级结构化的数据进行转换(marshalling or se ...

  8. iOS学习笔记(十一)——JSON数据解析

    在之前的<iOS学习——xml数据解析(九)>介绍了xml数据解析,这一篇简单介绍一下Json数据解析.JSON 即 JavaScript Object Natation,它是一种轻量级的 ...

  9. [译]Flutter JSON和序列化

    [译]Flutter JSON和序列化   很难想象一个移动应用程序不需要与Web服务器通信或在某些时候容易存储结构化数据.制作网络连接的应用程序时,迟早需要消耗一些好的旧JSON. 本指南介绍了如何 ...

随机推荐

  1. 吴裕雄--天生自然 R语言数据分析:火箭发射的地点、日期/时间和结果分析

    dfS = read.csv("F:\\kaggleDataSet\\spacex-missions\\database.csv") library(dplyr) library( ...

  2. 垃圾回收GC

    ​ 每种语言都有自己的垃圾回收机制.接下来我们来讲一下python的垃圾回收机制. 小整数对象池:python对小整数的定义为[-5,257),这些整数对象是提前建立好的,不会被垃圾回收.单个字母也一 ...

  3. UMD: 通用模块规范

    既然CommonJs和AMD风格一样流行,似乎缺少一个统一的规范.所以人们产生了这样的需求,希望有支持两种风格的“通用”模式,于是通用模块规范(UMD)诞生了.

  4. [JS奇怪的世界]No.55 危險小叮嚀:陣列與for in

    前言 前面已經瞭解了使用內建函數建構子的某些危險地方,但其實陣列與for in,也是有一些危險的地方. 陣列與for in 在前面幾個章節有講過陣列就是物件,所以我們一樣可以使用 for in來做處理 ...

  5. Atom配置(VIM党) · iuunhao

    为什么说是Vim党呢?首先我是一个深度的Vim用户,自己的电脑上基本上可以兼容Vim的插件都有,所有浏览器,所有编辑器都是Vim的操作方式,当然包括我现在书写的markdown的软件EME也是兼容的V ...

  6. 记录一下自己写PHP程序时走过的一些坑

    写在前面: 喔噢,转眼间发现自己正式开发程序(PHP)已经有快有1个月了,一路上走了许多的坑,有时遇到坑的时候真想放弃,但是还是坚持下来了!所以写了这篇文章来帮助那些刚刚接触PHP的小白们.[: )] ...

  7. 先搞清楚这些问题,简历上再写你熟悉Java!

    原创声明 本文作者:黄小斜 转载请务必在文章开头注明出处和作者. 系列文章介绍 本文是<五分钟学Java>系列文章的一篇 本系列文章主要围绕Java程序员必须掌握的核心技能,结合我个人三年 ...

  8. Typora+PicGo+Gitee笔记方案

    前言:需要学习的知识太多,从一开始就在寻找一款能让我完全满意的编辑器,然而一直都没有令我满意的.在前两天Typora新版本更新后,总算是拥有了一套我认为很完美的笔记方案:使用Typora编写markd ...

  9. AVCaptureInput和AVCaptureOutput子类介绍

    AVCaptureInput AVCaptureDeviceInput:用于从AVCaptureDevice对象捕获数据. AVCaptureScreenInput:从macOS屏幕上录制的一种捕获输 ...

  10. (Win10)Java,Maven,Tomcat8.0,Mysql8.0.15安装与环境配置,以及IDEA2019.3使用JDBC连接MySQL、创建JavaEE项目

    之前用windows+linux的双系统,最近不怎么舒服就把双系统给卸了,没想到除了问题,导致有linux残余,于是就一狠心重装了电脑,又把Java及其相关的一些东西重新装了回来,还好当初存了网盘链接 ...