微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Jsoncpp指南

导读

本篇文章介绍了Jsoncpp开源库在linux系统下的安装及使用。部署编译环境向来另程序员头痛的问题,往往耗费大量时间。因此,互联网上充斥着各种教程类的文章。我在部署环境的时候,看了许多相关教程,有时候结果并不理想。我的经验告诉我,大多数程序部署具有相似的流程。

例子1

cmake【version 2.x】版本过低,(引申,linux上通过sudo apt-get install安装的软件版本过低)

问题:如何升级cmake至【3.x】版本?

解答:通过源码安装,为什么呢?因为linux系统上许多程序在发布时,均提供configure文件和makefile。这意味着,你能够通过以下步骤安装程序

sudo ./configure
sudo make
sudo make install

cmake升级

  1. 首先,在cmake官网上的download页面下载最新版的cmake
    这里,下载cmake-3.10.0-rc2.tar.gz

  2. 解压cmake-3.10.0-rc2.tar.gz后,执行如下命令

    sudo apt-get autoremove cmake
    sudo ./configure
    sudo make
    sudo make install
  3. install完成后,查看version 【cmake –version】

综上,configure & make & make install,是最最基本的步骤。建议查看产品的官方说明和发布文档,好的产品是和“说明书”一起发布的。

jsoncpp编译

  1. 下载

    git clone https://github.com/open-source-parsers/jsoncpp 【当前版本 1.8.0】 
  2. 使用cmake

    在jsoncpp目录下可看到CMakeLists.txt文件,所以我们可以使用cmake编译
    首先,mkdir build | cd build
    然后,cmake ..
    接着,make
    于是,在build/src/lib_json/目录下,可以看到生成libjsoncpp.a静态库,成功第一步:smiley_cat:

jsoncpp读写

新建工程目录,包含jsoncpp/include/json/*.h和jsoncpp源文件编译所得的liibjsoncpp.a静态库

我的json-notes工程目录如下:

.
├── include
│   └── json
│       ├── allocator.h
│       ├── assertions.h
│       ├── autolink.h
│       ├── config.h
│       ├── features.h
│       ├── forwards.h
│       ├── json.h
│       ├── reader.h
│       ├── value.h
│       ├── version.h
│       └── writer.h
├── json.cc
├── lib
│   └── libjsoncpp.a
└── test.json

其中json.cc示例示例程序,内容如下:

#include <iostream>
#include <fstream>
using namespace std;

#include "include/json/json.h"

void display(const Json::Value & value);

int main(void)
{
    Json::Value hdu;
    Json::Value array;

    array.append("Jack");
    array.append("Rose");

    hdu["name"] = "Sun";
    hdu["ID"] = 427;
    hdu["Friend"] = array;
    hdu["subjuct"]["cs"] = "nice";

    // display Json data
    display(hdu);


    // write
    // wbuild为构建器,可以定制数据流格式
    Json::StreamWriterBuilder wbuilder;
    wbuilder["indentation"] = " ";
    cout << "'" << Json::writeString(wbuilder,hdu) << "'" << endl;

    // read
    Json::Value root;   // will contain the root value after parsing.
    Json::CharReaderBuilder rbuilder;

    ifstream test("test.json",ifstream::binary);
    string errs;
    bool issuccess = Json::parseFromStream(rbuilder,test,&root,&errs);
    if ( issuccess)
    {
        wbuilder["indentation"] = "";
         cout << Json::writeString(wbuilder,root)  << endl;
    }
    else
    {
        // report to the user the failure and their locations in the document.
        cout << errs << endl;
    }
    return 0;
}

void display(const Json::Value & value)
{
    cout << value["name"] << endl;
    cout << value["ID"] << endl;
    cout << value["Friend"][0] << " " << value["Friend"][1] << endl;
    cout << value["subjuct"]["cs"] << endl;
}
  • 值得注意的是新版的jsoncpp摒弃了Json::FastWriter 和Json::Reader读写类,改用Json::StreamWriterBuilder和Json::CharReaderBuilder读写类,前者比后者亲近用户

  • 为什么要做改变呢?

    Christopher Dunn says

    That may still be a little slower than the older version,but it is far more flexible. Other libraries are faster. JsonCpp has specific features,and the Builders make new features easier to add without breaking backward compatibility.

    原文

test.json

{
    "my-encoding" : "UTF-8","my-plug-ins" : [ "python","c++","ruby" ],"my-indent" : { "length": 3,"use_space": true } }

执行编译指令

g++ -o json json.cc lib/libjsoncpp.a -std=c++11

执行./json后,可得结果

"Sun"
427
"Jack" "Rose"
"nice"
'{
     "Friend" : 
     [ "Jack","Rose" ],"ID" : 427,"name" : "Sun","subjuct" : 
     { "cs" : "nice" } }'
{"my-encoding":"UTF-8","my-indent":{"length":3,"use_space":true},"my-plug-ins":["python","ruby"]}

总结:

  1. 使用二进制源码安装程序,基本上具有相似的流程(CMakeLists.txt
  2. 敢于尝试,电脑是你自己的(用办公电脑一定要谨慎),这是linux的自由之精神
  3. 尽量查看英文资料
  4. 不要把时间和精力过多耗费在,安装软件上。要知道编写优秀的程序,才是目标。

(全文完)

原文地址:https://www.jb51.cc/json/288705.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐