//https://github.com/dongyusheng/csdn-code/tree/master/jsoncpp
//æµè¯jsoncppçä½¿ç¨æ¹æ³åæ§è½
#include
#include
#include
#include
#include
#include
#include
#include "jsoncpp/json/json.h"
#include "jsoncpp/json/value.h"
#include "jsoncpp/json/reader.h"
#include "jsoncpp/json/writer.h"
/*
{
"name": "milo",
"age": 80,
"languages": ["C++", "C"],
"phone": {
"number": "186****3143",
"type": "home"
},
"books":[
{
"name": "Linux kernel development",
"price":7.7
},
{
"name": "Linux server development",
"price": 8.0
}
]ï¼
"vip":true,
"address": null
}
*/
static uint64_t getNowTime()
{
struct timeval tval;
uint64_t nowTime;
gettimeofday(&tval, NULL);
nowTime = tval.tv_sec * 1000L + tval.tv_usec / 1000L;
return nowTime;
}
std::string JsoncppEncodeNew()
{
std::string jsonStr;
// ä¸ä¸ªvalueæ¯å¯ä»¥å
å«å¤ä¸ªé®å¼å¯¹
Json::Value root, languages, phone, book, books;
// å§å
root["name"] = "milo";
// å¹´é¾
root["age"] = 80;
// è¯è¨
languages[0] = "C++";
languages[1] = "Java";
root["languages"] = languages;
// çµè¯
phone["number"] = "186****3143";
phone["type"] = "home";
root["phone"] = phone;
// 书ç±
book["name"] = "Linux kernel development";
book["price"] = 7.7;
books[0] = book; // æ·±æ·è´
book["name"] = "Linux server development";
book["price"] = 8.0;
books[1] = book; // æ·±æ·è´
root["books"] = books;
// æ¯å¦ä¸ºvip
root["vip"] = true;
// addressä¿¡æ¯ç©ºnull
root["address"] = "yageguoji";
Json::StreamWriterBuilder writerBuilder;
std::ostringstream os;
std::unique_ptr<:streamwriter> jsonWriter(writerBuilder.newStreamWriter());
jsonWriter->write(root, &os);
jsonStr = os.str();
// std::cout << "Json:\n" << jsonStr << std::endl;
return jsonStr;
}
std::string JsoncppEncodeOld()
{
std::string jsonStr;
Json::Value root, languages, phone, book, books;
// å§å
root["name"] = "milo";
//root["name"] = Json::nullValue;
// å¹´é¾
root["age"] = 80;
// è¯è¨
languages[0] = "C++";
languages[1] = "Java";
root["languages"] = languages;
// çµè¯
phone["number"] = "186****3143";
phone["type"] = "home";
root["phone"] = phone;
// 书ç±
book["name"] = "Linux kernel development";
book["price"] = (float)7.7;
books[0] = book;
book["name"] = "Linux server development";
book["price"] = (float)8.0;
books[1] = book;
root["books"] = books;
// æ¯å¦ä¸ºvip
root["vip"] = true;
// addressä¿¡æ¯ç©ºnull
root["address"] = "yageguoji"; //;// Json::nullValue; // 妿æ¯nullï¼åè¦ç¨èªå®ä¹çJson::nullValueï¼ä¸è½ç¨NULL
Json::FastWriter writer;
jsonStr = writer.write(root);
// std::cout << "Json:\n" << jsonStr << std::endl;
return jsonStr;
}
// ä¸è½è¿åå¼ç¨
Json::Value JsoncppEncodeOldGet()
{
Json::Value root;
Json::Value languages, phone, book, books;
// å§å
root["name"] = "milo";
//root["name"] = Json::nullValue;
// å¹´é¾
root["age"] = 80;
// è¯è¨
languages[0] = "C++";
languages[1] = "Java";
root["languages"] = languages;
// çµè¯
phone["number"] = "186****3143";
phone["type"] = "home";
root["phone"] = phone;
// 书ç±
book["name"] = "Linux kernel development";
book["price"] = (float)7.7;
books[0] = book;
book["name"] = "Linux server development";
book["price"] = (float)8.0;
books[1] = book;
root["books"] = books;
// æ¯å¦ä¸ºvip
root["vip"] = true;
// addressä¿¡æ¯ç©ºnull
root["address"] = Json::nullValue; //"yageguoji";// Json::nullValue; // 妿æ¯nullï¼åè¦ç¨èªå®ä¹çJson::nullValueï¼ä¸è½ç¨NULL
std::cout << "JsoncppEncodeOldGet:\n" << std::endl;
return root;
}
void printJsoncpp(Json::Value &root)
{
if(root["name"].isNull())
{
std::cout << "name null\n";
}
std::cout << "name: " << root["name"].asString() << std::endl;
std::cout << "age: " << root["age"].asInt() << std::endl;
Json::Value &languages = root["languages"];
std::cout << "languages: ";
for (uint32_t i = 0; i < languages.size(); ++i)
{
if (i != 0)
{
std::cout << ", ";
}
std::cout << languages[i].asString();
}
std::cout << std::endl;
Json::Value &phone = root["phone"];
std::cout << "phone number: " << phone["number"].asString() << ", type: " << phone["type"].asString() << std::endl;
Json::Value &books = root["books"];
for (uint32_t i = 0; i < books.size(); ++i)
{
Json::Value &book = books[i];
std::cout << "book name: " << book["name"].asString() << ", price: " << book["price"].asFloat() << std::endl;
}
std::cout << "vip: " << root["vip"].asBool() << std::endl;
if (!root["address"].isNull())
{
std::cout << "address: " << root["address"].asString() << std::endl;
}
else
{
std::cout << "address is null" << std::endl;
}
}
bool JsoncppDecodeNew(const std::string &info)
{
if (info.empty())
return false;
bool res;
JSONCPP_STRING errs;
Json::Value root;
Json::CharReaderBuilder readerBuilder;
std::unique_ptr<:charreader> const jsonReader(readerBuilder.newCharReader());
res = jsonReader->parse(info.c_str(), info.c_str() + info.length(), &root, &errs);
if (!res || !errs.empty())
{
std::cout << "parseJson err. " << errs << std::endl;
return false;
}
// printJsoncpp(root);
return true;
}
bool JsoncppDecodeOld(const std::string &strJson)
{
if (strJson.empty())
return false;
bool res;
Json::Value root;
Json::Reader jsonReader;
res = jsonReader.parse(strJson, root);
if (!res)
{
std::cout << "jsonReader.parse err. " << std::endl;
return false;
}
// printJsoncpp(root);
return true;
}
const char *strCjson = "{ \
\"name\": \"milo\", \
\"age\": 80, \
\"languages\": [\"C++\", \"C\"],\
\"phone\": { \
\"number\": \"186****3143\", \
\"type\": \"home\" \
}, \
\"books\": [{ \
\"name\": \"Linux kernel development\", \
\"price\": 7.700000 \
}, { \
\"name\": \"Linux server development\", \
\"price\": 8 \
}], \
\"vip\": true, \
\"address\": null \
} \
";
#define TEST_COUNT 100000
int main()
{
std::string jsonStrNew;
std::string jsonStrOld;
jsonStrNew = JsoncppEncodeNew();
// JsoncppEncodeNew size: 337
std::cout << "JsoncppEncodeNew size: " << jsonStrNew.size() << std::endl;
std::cout << "JsoncppEncodeNew string: " << jsonStrNew << std::endl;
JsoncppDecodeNew(jsonStrNew);
jsonStrOld = JsoncppEncodeOld();
// JsoncppEncodeOld size: 248
std::cout << "\n\nJsoncppEncodeOld size: " << jsonStrOld.size() << std::endl;
std::cout << "JsoncppEncodeOld string: " << jsonStrOld << std::endl;
JsoncppDecodeOld(jsonStrOld);
Json::Value root = JsoncppEncodeOldGet();
Json::FastWriter writer;
std::cout << "writer:\n" << std::endl;
std::string jsonStr = writer.write(root);
std::cout << "\n\njsonStr string: " << jsonStrOld << std::endl;
#if 1
uint64_t startTime;
uint64_t nowTime;
startTime = getNowTime();
std::cout << "jsoncpp encode time testing" << std::endl;
for(int i = 0; i < TEST_COUNT; i++)
{
JsoncppEncodeNew();
}
nowTime = getNowTime();
std::cout << "jsoncpp encode " << TEST_COUNT << " time, need time: "
<< nowTime-startTime << "ms" << std::endl;
startTime = getNowTime();
std::cout << "\njsoncpp encode time testing" << std::endl;
for(int i = 0; i < TEST_COUNT; i++)
{
JsoncppEncodeOld();
}
nowTime = getNowTime();
std::cout << "jsoncpp encode " << TEST_COUNT << " time, need time: "
<< nowTime-startTime << "ms" << std::endl;
startTime = getNowTime();
std::cout << "\njsoncpp decode time testing" << std::endl;
for(int i = 0; i < TEST_COUNT; i++)
{
JsoncppDecodeNew(jsonStrNew);
}
nowTime = getNowTime();
std::cout << "jsoncpp decode " << TEST_COUNT << " time, need time: "
<< nowTime-startTime << "ms" << std::endl;
startTime = getNowTime();
std::cout << "\njsoncpp decode time testing" << std::endl;
for(int i = 0; i < TEST_COUNT; i++)
{
JsoncppDecodeOld(jsonStrNew);
}
nowTime = getNowTime();
std::cout << "jsoncpp decode " << TEST_COUNT << " time, need time: "
<< nowTime-startTime << "ms" << std::endl;
#endif
return 0;
}