来了新公司之后,现在的json解析真的很难用,举个例子,假如想删除一个对象,要重新生成,去掉要删除的,其余的要组装上。很怀念之前用的jsoncpp,想引进来,就研究一下。

 下载和安装

  下载

    从github,直接搜jsoncpp就能搜到,第一个就是,懒得搜直接给你地址:https://github.com/open-source-parsers/jsoncpp

  安装

    python amalgamate.py

    然后执行

    cmake CMakeLists.txt

    没有安装cmake,可以参考这篇博客:https://www.cnblogs.com/liudw-0215/p/9877290.html

    新版cmake对gcc版本,用的centos6.5,是需要升级的,可以参考这篇博客:https://blog.csdn.net/centnetHY/article/details/81284657,但升级gcc,比较耗时。

    然后再执行

    make

    如果想把头文件和库安装到系统目录,就执行

    make install

    

    使用

    序列化新旧接口

    代码如下:

    

#include "json/json.h"
#include <iostream>
/** \brief Write a Value object to a string.
* Example Usage:
* $g++ stringWrite.cpp -ljsoncpp -std=c++11 -o stringWrite
* $./stringWrite
* {
* "action" : "run",
* "data" :
* {
* "number" : 1
* }
* }
*/
int main() {
Json::Value root;
Json::Value data;
constexpr bool shouldUseOldWay = false;
root["action"] = "run";
data["number"] = 1;
root["data"] = data; if (shouldUseOldWay) {
Json::FastWriter writer;
const std::string json_file = writer.write(root);
std::cout << json_file << std::endl;
} else {
Json::StreamWriterBuilder builder;
const std::string json_file = Json::writeString(builder, root);
std::cout << json_file << std::endl;
}
return EXIT_SUCCESS;
}

stringWrite.cpp

      

    会警告,is deprecated: Use StreamWriterBuilder instead [-Wdeprecated-declarations],因为这旧的接口,如果不想报警告,可以在代码最上面加上下面代码:    

#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
#pragma warning(disable : 4996)
#endif

  代码如下:

    

#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
#pragma warning(disable : 4996)
#endif #include "json/json.h"
#include <iostream>
/** \brief Write a Value object to a string.
* * Example Usage:
* * $g++ stringWrite.cpp -ljsoncpp -std=c++11 -o stringWrite
* * $./stringWrite
* * {
* * "action" : "run",
* * "data" :
* * {
* * "number" : 1
* * }
* * }
* */
int main() {
Json::Value root;
Json::Value data;
constexpr bool shouldUseOldWay = false;
root["action"] = "run";
data["number"] = 1;
root["data"] = data; if (shouldUseOldWay) {
Json::FastWriter writer;
const std::string json_file = writer.write(root);
std::cout << json_file << std::endl;
} else {
Json::StreamWriterBuilder builder;
const std::string json_file = Json::writeString(builder, root);
std::cout << json_file << std::endl;
}
return EXIT_SUCCESS;
}

stringWrite.cpp

    反序列化新旧接口

    

#include "json/json.h"
#include <iostream>
/**
* \brief Parse a raw string into Value object using the CharReaderBuilder
* class, or the legacy Reader class.
* Example Usage:
* $g++ readFromString.cpp -ljsoncpp -std=c++11 -o readFromString
* $./readFromString
* colin
* 20
*/
int main() {
const std::string rawJson = R"({"Age": 20, "Name": "colin"})";
const auto rawJsonLength = static_cast<int>(rawJson.length());
constexpr bool shouldUseOldWay = false;
JSONCPP_STRING err;
Json::Value root; if (shouldUseOldWay) {
Json::Reader reader;
reader.parse(rawJson, root);
} else {
Json::CharReaderBuilder builder;
const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
&err)) {
std::cout << "error" << std::endl;
return EXIT_FAILURE;
}
}
const std::string name = root["Name"].asString();
const int age = root["Age"].asInt(); std::cout << name << std::endl;
std::cout << age << std::endl;
return EXIT_SUCCESS;
}

readFromString.cpp

  

jsoncpp安装与使用 cmake安装 升级g++ gcc支持c++11的更多相关文章

  1. 升级 GCC 支持C++11 或 configure: error: *** A compiler with support for C++11 language features is required.

    configure: error: *** A compiler with support for C++11 language features is required. 参考链接: (1)升级 G ...

  2. g++默认支持c++11标准的办法

    //第一种,直接包含在源程序文件中,如第一行代码所示 #pragma GCC diagnostic error "-std=c++11" #include <iostream ...

  3. 转:linux下安装或升级GCC4.8,以支持C++11标准

    转:http://www.cnblogs.com/lizhenghn/p/3550996.html C++11标准在2011年8月份获得一致通过,这是自1998年后C++语言第一次大修订,对C++语言 ...

  4. cmake安装MySQL

    发现一个网址整理的挺好,请各位参考: http://www.chenyudong.com/archives/building-mysql-5-6-from-source.html#i 也可以参考我的另 ...

  5. linux下安装或升级GCC4.8,以支持C++11标准

    C++11标准在2011年8月份获得一致通过,这是自1998年后C++语言第一次大修订,对C++语言进行了改进和扩充.随后各编译器厂商都各自实现或部分实现了C++中的特性. 如需查看各编译器对C++1 ...

  6. linux 下使用 cmake安装mysql

    原文地址:http://www.cppblog.com/issay789/archive/2013/01/05/196967.html 一.安装 m4 下载地址: http://files.w3pc. ...

  7. OpenCV+Qt+CMake安装+十种踩坑

    平台:win10 x64+opencv-3.4.1 + qt-x86-5.9.0 + cmake3.13.4 x64 OpenCV+Qt+CMake安装,及目前安装完后打包:mingw32-make时 ...

  8. 在CentOS 6.3中安装与配置cmake

    安装说明安装环境:CentOS-6.3安装方式:源码编译安装软件:cmake-2.8.10.2.tar.gz下载地址:http://www.cmake.org/cmake/resources/soft ...

  9. 【整理】LINUX下使用CMAKE安装MYSQL

    原文地址:http://www.cppblog.com/issay789/archive/2013/01/05/196967.html 一.安装 m4 下载地址: http://files.w3pc. ...

  10. 转:CMake安装和使用

      CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX ...

随机推荐

  1. 适用于 .NET 的现代化、流畅、可测试的HTTP客户端库

    前言 今天大姚给大家分享一个.NET开源(MIT License).免费.现代化.流畅.可测试.可移植的URL构建器和HTTP客户端库:Flurl. 项目介绍 Flurl是一个集现代性.流畅性.异步性 ...

  2. C# 泛型SQLHelper<T>类

    示例[1] 1.创建SQLHelper类 using System.Collections.Generic; using System.Configuration; using System.Data ...

  3. SEO初学指南之关键词研究(3) - 关键词分析实战

    经过之前的学习,这次我们开始实战.手把手教大家如何为网站筛选符合条件的关键词. 还不熟练的,建议再反复学习前两篇文章: SEO初学指南之关键词研究 (1) - 入门 SEO初学指南之关键词研究 (2) ...

  4. python lambda 三元表达式

    python lambda 三元表达式 python中的lambda函数用法 通常定义的函数 def sum(x,y): return x+y print(sum(4,6)) 用lambda来实现: ...

  5. Python+selenium编写第一个UI自动化脚本

    python UI自动化前提:①python需要安装selenium模块  ②下载浏览器驱动 1.安装selenium模块 先确认本地是否安装selenium模块,没有的话点击右边的添加按钮" ...

  6. Cesium 实现可视域分析

    *前言:尝试了网上好多个版本的可视域分析,感觉都有一些问题,我这个也可能不是最完美的,但是我觉得对我来说够用了,实现效果如下* 此示例基于vue3上实现,cesium版本1.101.0 ,vite-p ...

  7. 【Mybatis-Plus】使用QueryWrapper作为自定义SQL的条件参数

    发现同事的自定义SQL写法是这样的 连表之后使用的条件是 ${ew.customSqlSegment} @Param声明的常量: /** * wrapper 类 */ String WRAPPER = ...

  8. 使用 Alba 对 AspnetCore项目进行测试

    前言 在AspnetCore生态系统中,我们测试项目一般使用Microsoft.AspNetCore.TestHost的TestServer 到.NET6后提供的Microsoft.AspNetCor ...

  9. 蔡磊公布渐冻症诊断报告 5月住进ICU一度考虑气切

    原文地址: https://baijiahao.baidu.com/s?id=1801485780372006198

  10. 【转载】 Ring Allreduce (深度神经网络的分布式计算范式 -------------- 环形全局规约)

    作者:初七123链接:https://www.jianshu.com/p/8c0e7edbefb9来源:简书著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. ----------- ...