zoukankan      html  css  js  c++  java
  • jsoncpp安装与使用 cmake安装 升级g++ gcc支持c++11

      来了新公司之后,现在的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>
    /** rief 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>
    /** rief 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>
    /**
     * rief 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

      

  • 相关阅读:
    打开XX.etl文件
    ubuntu 安装 openssh-server,xinetd,vmware tools
    ESXi时间同步
    常用正则表达式字符说明
    RPC 服务器不可用
    linux 常用命令
    解决RDP连接不上
    python数据持久存储-pickle模块
    lambda表达式/对象引用计数
    关闭网络打开远程文件时防火墙安全弹窗
  • 原文地址:https://www.cnblogs.com/liudw-0215/p/13578378.html
Copyright © 2011-2022 走看看