C++序列化和反序列化

C++序列化:

hello.cpp  中代码:

 1 #include <cppcms/json.h>  
2 #include <iostream>
3
4 using namespace std;
5
6 class response {
7 public:
8 //0 is ok
9 //other values are wrong
10 int status;
11 string message;
12 };
13 namespace cppcms {
14 namespace json {
15
16 template<>
17 struct traits<response> {
18
19 static void set(value &v, response const& in) {
20 v.set("status", in.status);
21 v.set("message", in.message);
22 }
23 };
24
25 }
26 }
27
28 int main(int argc, char** argv) {
29 response r1;
30 r1.status=0;
31 r1.message="ok";
32 cout<< cppcms::json::value(r1)<<endl;
33
34 response r2;
35 r2.status=1;
36 r2.message="unknown error";
37 cout<< cppcms::json::value(r2)<<endl;
38 return 0;
39 }

运行结果:

前提:安装CppCMS

C++反序列化:

fhello.cpp 中代码:

 1 #include <cppcms/json.h>  
2 #include <iostream>
3 #include <sstream>
4 using namespace std;
5 int main(int argc, char** argv) {
6 istringstream stream("{\"message\":\"ok\",\"status\":0}");
7
8 cppcms::json::value value2;
9 stream >> value2;
10 string m = value2.get("message","null");
11 int n = value2.get("status",1);
12 cout << "message:" << m << endl;
13 cout << "status:" << n << endl;
14 return 0;
15 }

运行结果:

参考:http://blog.csdn.net/sheismylife/article/details/7096228
        http://cppcms.com/wikipp/en/page/cppcms_1x_tut_hello



转载于:https://www.cnblogs.com/fengye-dream/archive/2012/03/21/2409964.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值