来了新公司之后,现在的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. 【java深入学习第1章】深入探究 MyBatis-Spring 中 SqlSession 的原理与应用

    前言 在使用 MyBatis 进行持久层开发时,通常会与 Spring 框架集成,以便更好地管理事务和依赖注入.在 MyBatis-Spring 集成中,SqlSession 是一个非常重要的概念.本 ...

  2. Jenkins插件管理(Manager Plugins)【快速提升项目构建和部署实施的工作效率】

    Jenkins 是一个很棒的开源自动化平台.它有一些开箱即用的强大功能.然而,在我看来,让它脱颖而出的是它的社区和它开发的插件.有超过一千个插件可用于支持几乎所有用于构建.部署和自动化项目的技术.工具 ...

  3. 人类高质量 Java 学习路线【一条龙版】

    Java 学习路线一条龙版 by 鱼皮. 原创不易,请勿抄袭,违者必究! 大家好,我是鱼皮.现在网上的编程资料实在太多了,而且人人肯定都说自己的最好,那就导致大家又不知道怎么选了.大部分的博主推荐资源 ...

  4. oeasy教您玩转vim - 21 - 状态横条

    状态横条 回忆上节课内容 我们上次研究了标尺 标尺 开启 se ru 关闭 se noru 行号 开启 se nu 关闭 se nonu 命令位置 开启 se showcmd 关闭 se noshow ...

  5. 【MongoDB】Re05 分片集群(Win平台搭建)

    分片副本集1 (3实例)  主1 从1 裁1 分片副本集2 (3实例)  主1 从1 裁1 配置副本集(3实例) 主1 从2 路由(2配置) 用Windows平台搭建 配置目录设置: ├─config ...

  6. 【Mybatis】06 Session获取 & 配置参数总结

    会话获取 SqlSessionFactory 最佳的获取方式就是使用Mybatis提供的资源类加载配置文件 调用会话工厂建造者实例的建造方法注入读取流 要注意的是建造者生成了了实例就可以不需要了 这里 ...

  7. 【Android】看安卓代码的一点笔记

    最近项目需要把安卓项目拉下来看了 简单来说的话,网页是HTML + CSS + JS 组成的,运行环境是浏览器上面 安卓APP应用是 Java + XML 组成的,运行环境是这个安卓系统中 构成结构 ...

  8. 深度学习框架Theano停止维护

    Theano停止开发的声明地址: https://groups.google.com/g/theano-users/c/7Poq8BZutbY/m/rNCIfvAEAwAJ 原文内容: Dear us ...

  9. 乌克兰学者的学术图谱case2

    ======================================= 0. 学者:Солонін Ю.М. 中文翻译名:索洛宁·尤里·米哈伊洛维奇 英文翻译名:Solonin Yuriy M ...

  10. 为python安装扩展模块时报错——error: invalid command 'bdist_wheel'

    具体过程: devil@hp:~/lab$ ./bazel-bin/python/pip_package/build_pip_package /tmp/dmlab_pkg2022年 10月 03日 星 ...