龙空技术网

jsoncpp CharReaderBuilder和StreamWriterBuilder, 别deprecated

踪源 174

前言:

当前同学们对“ubuntu上jsoncpp”大致比较讲究,同学们都想要剖析一些“ubuntu上jsoncpp”的相关资讯。那么小编同时在网上汇集了一些对于“ubuntu上jsoncpp””的相关资讯,希望大家能喜欢,各位老铁们快快来学习一下吧!

jsoncpp是c++读写json的一个工具库,十分小巧好用。但是其更新也比较快,有时变化较大。很多人可能还在使用Json::Reader::parse或者Json::FastWriter::write方法进行读写。如果使用较新版本的话会发现这些方法都已经是deprecated了,建议使用CharReaderBuilder和StreamWriterBuilder进行替换。下面就简单举个例子说明一下,更多详细内容请参看源码或者manual文档。

example.json文件内容

{

"name": "xiaoli",

"age": 25,

"hobbies": [

"pingpong",

"listen song",

{

"other": [

"eating noddles",

"reading books"

],

"lucky number": 26

}

]

}

测试代码如下:

#include <iostream>

#include <fstream>

#include <sstream>

#include <memory>

#include <json/json.h>

using namespace std;

/* example.json content

{

"name": "xiaoli",

"age": 25,

"hobbies": [

"pingpong",

"listen song",

{

"other": [

"eating noddles",

"reading books"

],

"lucky number": 26

}

]

}

*/

int main()

{

ifstream ifs("example.json");

// get file content by string

ostringstream os;

os << ifs.rdbuf();

string file_str = os.str();

os.clear(); // clear flags

os.str(""); // clear content

ifs.seekg(0); // reset position to 0

cout << "======= json file content is =======" << endl;

cout << file_str << endl;

cout << "======= test reading ========" << endl;

Json::CharReaderBuilder rbuilder;

rbuilder["collectComments"] = false;

Json::Value root;

JSONCPP_STRING errs;

// parse json document

bool ok = Json::parseFromStream(rbuilder, ifs, &root, &errs);

if (!ok) {

cout << "***** PARSE ERROR: " << errs << endl;

return -1;

}

else {

try {

cout << "***** READ SUCCEED." << endl;

cout << "name is " << root["name"].asString() << endl;

cout << "age is " << root["age"].asInt() << endl;

cout << "lucky number is "

<< root["hobbies"][2]["lucky number"].asInt()

<< endl;

}

catch (const Json::LogicError& ex) {

cout << "****** EXCEPTION CAUGHT in reading: " << ex.what() << endl;

}

}

cout << endl;

cout << "======= test writing ========" << endl;

// write json Object Value to file

Json::StreamWriterBuilder wbuilder;

wbuilder["commentStyle"] = "None";

wbuilder["indentation"] = " "; // or whatever you like

unique_ptr<Json::StreamWriter> writer(wbuilder.newStreamWriter());

try {

writer->write(root, &os);

}

catch (const exception& ex) {

cout << "****** EXCEPTION CAUGHT in writing: " << ex.what() << endl;

}

string json_str = os.str();

if (os.fail()) {

cout << "***** Write ERROR." << endl;

}

else {

cout << "***** Write SUCCEED." << endl;

}

cout << "writed string is:" << endl;

cout << json_str << endl;

}

编译运行命令如下(与jsoncpp安装位置有关,我安装在 /usr/local/{include, lib}):

g++ --verbose -std=c++14 -Wall -Werror -Wextra $(pkg-config --cflags --libs jsoncpp) jsoncpp-example.cpp -o example

or

g++ --verbose -std=c++14 -Wall -Werror -Wextra -I/usr/local/include -L/usr/local/lib -ljsoncpp jsoncpp-example.cpp -o example

LD_LIBRARY_PATH=/usr/local/lib ./example

运行结果输出如下:

======= json file content is =======

{

"name": "xiaoli",

"age": 25,

"hobbies": [

"pingpong",

"listen song",

{

"other": [

"eating noddles",

"reading books"

],

"lucky number": 26

}

]

}

======= test reading ========

***** READ SUCCEED.

name is xiaoli

age is 25

lucky number is 26

======= test writing ========

***** Write SUCCEED.

writed string is:

{

"age" : 25,

"hobbies" :

[

"pingpong",

"listen song",

{

"lucky number" : 26,

"other" : [ "eating noddles", "reading books" ]

}

],

"name" : "xiaoli"

}

最后,与时俱进,少用deprecated方法,特别是对开源代码。

标签: #ubuntu上jsoncpp