1 Star 0 Fork 0

夜夜亮晶晶/从零实现JsonRpc框架

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Serialize.cpp 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
#include <iostream>
#include <string>
#include <sstream>
#include <memory>
#include <jsoncpp/json/json.h>
//数据序列化
bool Serialize(const Json::Value &val, std::string &body)
{
//中间缓冲区,将 Json::Value 格式转换为 std::string 格式
std::stringstream ss;
//定义工厂类对象
Json::StreamWriterBuilder swb;
// 设置 emitUTF8 使其输出原始的 UTF-8 编码而不是转义序列
swb["emitUTF8"] = true;
std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
//如果 write 返回值不为零,则错误
int ret = sw->write(val, &ss);
if (ret != 0)
{
std::cout << "Serialize Error!";
return false;
}
body = ss.str();
return true;
}
int main()
{
const char* name = "小明";
const char*sex = "男";
int age = 18;
int score[3] = { 100, 110, 96 };
Json::Value student;
std::string body;
student["姓名"] = name;
student["性别"] = sex;
student["年龄"] = age;
student["成绩"].append(score[0]);
student["成绩"].append(score[1]);
student["成绩"].append(score[2]);
Serialize(student, body);
//打印字符串信息
std::cout << body << std::endl;
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bright-and-sparkling-at-night/JsonRpc.git
git@gitee.com:bright-and-sparkling-at-night/JsonRpc.git
bright-and-sparkling-at-night
JsonRpc
从零实现JsonRpc框架
master

搜索帮助