JSON
(JavaScript Object Notation
)格式作為一種數(shù)據(jù)格式酌摇,從最初作為JS
語言的子集,以其易于閱讀和處理的優(yōu)勢(shì)嗡载,逐漸被多種語言所支持窑多,如Python/Java
等,均提供了處理JSON
格式文本的標(biāo)準(zhǔn)庫/第三方庫洼滚,因而脫離了某種編程語言的標(biāo)簽埂息,成為一種通用的數(shù)據(jù)傳輸格式。
C++
也出現(xiàn)了很多的開源庫以支持JSON
格式文本的處理,其特點(diǎn)各異耿芹,本文主要介紹其中一個(gè)—— Nlohmann JSON崭篡。
Nlohmann JSON
庫以C++11
實(shí)現(xiàn),主要設(shè)計(jì)目標(biāo)為易用性吧秕,體現(xiàn)在該庫可以通過一個(gè)頭文件集成到任意項(xiàng)目琉闪,并且像STL
容器一樣操作JSON
對(duì)象。但是并沒有把內(nèi)存消耗和運(yùn)行速度作為重要的指標(biāo)砸彬,因此在性能上可能不及一些以性能為主要設(shè)計(jì)目標(biāo)的JSON
庫颠毙,如果對(duì)C++
實(shí)現(xiàn)的各類JSON
庫運(yùn)行性能對(duì)比感興趣可以看這里。
先上開源庫地址: https://github.com/nlohmann/json砂碉,本文的主要參考資料就是該庫的README蛀蜜,下面的例子中使用的庫版本為3.10.2。
一增蹭、集成
為支持單文件引入滴某,每次Release
版本中都會(huì)有一個(gè)json.hpp
,只要下載并把它添加到項(xiàng)目中滋迈,然后在要處理JSON
的文件中添加如下霎奢,并在編譯時(shí)支持C++11
(-std=c++11
)就可以使用了。
#include "json.hpp" // 替換成放該json.hpp的路徑
// 方便引用
using json = nlohmann::json;
二饼灿、使用
本節(jié)主要介紹此JSON
庫四種常用的對(duì)象構(gòu)造方法和一種操作方法
1. 從零構(gòu)造一個(gè)JSON
對(duì)象
作為JSON
庫最最基礎(chǔ)的用法幕侠,假設(shè)要?jiǎng)?chuàng)建一個(gè)這樣的JSON
對(duì)象:
{
"pi": 3.141,
"happy": true,
"name": "Niels",
"nothing": null,
"answer": {
"everything": 42
},
"list": [1, 0, 2],
"object": {
"currency": "USD",
"value": 42.99
}
}
使用這個(gè)庫,可以這樣寫:
// 先創(chuàng)建一個(gè)空J(rèn)SON結(jié)構(gòu)(null)
json j;
// 然后添加一個(gè)Number值碍彭,在C++中被存儲(chǔ)為double晤硕,j被隱式轉(zhuǎn)換為object結(jié)構(gòu)
j["pi"] = 3.141;
// 添加一個(gè)Boolean值,在C++中被存儲(chǔ)為bool
j["happy"] = true;
// 添加一個(gè)String值庇忌,在C++中被存儲(chǔ)為std::string
j["name"] = "Niels";
// 添加一個(gè)null對(duì)象舞箍,在C++中被存儲(chǔ)為nullptr
j["nothing"] = nullptr;
// 在Object中添加Object
j["answer"]["everything"] = 42;
// 添加一個(gè)Array對(duì)象,在C++中被存儲(chǔ)為std:vector(使用初始化列表)
j["list"] = { 1, 0, 2 };
// 添加一個(gè)Object漆枚,使用一對(duì)初始化列表
j["object"] = { {"currency", "USD"}, {"value", 42.99} };
// 也可以使用下面這種方法定義和上述相同結(jié)構(gòu)的JSON對(duì)象
// 但個(gè)人不太推薦创译,花括號(hào)多到數(shù)組和對(duì)象很容易混
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
2. 由字符串序列化
創(chuàng)建和解析JSON
字符串是一種常見的操作抵知。
可以通過在字符串后面添加_json
創(chuàng)建JSON
對(duì)象墙基。(由C++11
用戶定義字面量 特性支持)
// 通過字符串構(gòu)造JSON對(duì)象
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;
// 或者使用原生字符串構(gòu)造
auto j2 = R"(
{
"happy": true,
"pi": 3.141
}
)"_json;
// 也可以顯式通過json::parse解析字符串
auto j3 = json::parse(R"({"happy": true, "pi": 3.141})");
相反的,可以通過.dump()
方法將JSON
對(duì)象轉(zhuǎn)換成字符串
// 顯式轉(zhuǎn)換為字符串
std::string s = j.dump(); // {"happy":true,"pi":3.141}
// 向dump傳入間隔的空格數(shù)刷喜,以打印格式易于閱讀的字符串
std::cout << j.dump(4) << std::endl;
// {
// "happy": true,
// "pi": 3.141
// }
3. 像STL一樣操作
為了使C++
開發(fā)者易于上手残制,該庫對(duì)JSON
對(duì)象的操作模擬了常見的STL
容器操作。
// 使用push_back創(chuàng)建Array
json j;
j.push_back("foo");
j.push_back(1);
j.push_back(true);
// 也可以使用C++11標(biāo)準(zhǔn)中的emplace_back
j.emplace_back(1.78);
// 通過iterator遍歷這個(gè)Array
for (json::iterator it = j.begin(); it != j.end(); ++it) {
std::cout << *it << '\n';
}
// 或者C++11中的范圍for
for (auto& element : j) {
std::cout << element << '\n';
}
// getter/setter
const auto tmp = j[0].get<std::string>(); // .get<T>() 顯式轉(zhuǎn)換獲取值
bool foo = j.at(2); // .at(X) 隱式轉(zhuǎn)換獲取值掖疮,不過盡量避免使用隱式轉(zhuǎn)換
j[1] = 42;
// 比較
j == R"(["foo", 1, true, 1.78])"_json; // true
// 其他方法
j.size(); // 4
j.empty(); // false
j.type(); // json::value_t::array
j.clear();
// 類型檢查的一些方法
j.is_null();
j.is_boolean();
j.is_number();
j.is_object();
j.is_array();
j.is_string();
// 查找
if (j.contains("foo")) {
// JSON對(duì)象中有鍵名foo
}
// 或者通過iterator來查找
if (j.find("foo") != j.end()) {
// JSON對(duì)象中有鍵名foo
}
// 或者用更簡單的count()
int foo_present = j.count("foo"); // 1
int fob_present = j.count("fob"); // 0
// 刪除
j.erase("foo");
4. 由STL容器構(gòu)造
JSON
對(duì)象可以由STL
容器直接構(gòu)造初茶,但是由關(guān)聯(lián)容器構(gòu)造的JSON
對(duì)象中鍵的順序,由容器中元素的排序順序決定浊闪。
#include "json.hpp" // 替換成放該json.hpp的路徑
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <unordered_set>
// 方便引用
using json = nlohmann::json;
int main()
{
std::vector<int> c_vector {1, 2, 3, 4};
json j_vec(c_vector); // [1, 2, 3, 4]
std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6};
json j_deque(c_deque); // [1.2, 2.3, 3.4, 5.6]
std::list<bool> c_list {true, true, false, true};
json j_list(c_list); // [true, true, false, true]
std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
json j_flist(c_flist); // [12345678909876, 23456789098765, 34567890987654, 45678909876543]
std::array<unsigned long, 4> c_array {{1, 2, 3, 4}};
json j_array(c_array); // [1, 2, 3, 4]
std::set<std::string> c_set {"one", "two", "three", "four", "one"};
json j_set(c_set); // ["four", "one", "three", "two"]
std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"};
json j_uset(c_uset); // maybe ["two", "three", "four", "one"]
std::multiset<std::string> c_mset {"one", "two", "one", "four"};
json j_mset(c_mset); // maybe ["one", "two", "one", "four"]
std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"};
json j_umset(c_umset); // maybe ["one", "two", "one", "four"]
return 0;
}
同樣的恼布,對(duì)于有鍵值對(duì)的關(guān)聯(lián)容器螺戳,其鍵要能構(gòu)造成std::string
,它才能直接構(gòu)造為JSON
對(duì)象折汞。
std::map<std::string, int> c_map { {"one", 1}, {"two", 2}, {"three", 3} };
json j_map(c_map); // {"one": 1, "three": 3, "two": 2 }
std::unordered_map<const char*, double> c_umap { {"one", 1.2}, {"two", 2.3}, {"three", 3.4} };
json j_umap(c_umap); // {"one": 1.2, "two": 2.3, "three": 3.4}
std::multimap<std::string, bool> c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_mmap(c_mmap); // maybe {"one": true, "two": true, "three": true}
std::unordered_multimap<std::string, bool> c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_ummap(c_ummap); // maybe {"one": true, "two": true, "three": true}
5. 由自定義類型構(gòu)造
除了STL
容器倔幼,由自定義類型的對(duì)象構(gòu)造JSON
對(duì)象也是比較常用的操作。
當(dāng)然爽待,可以通過手動(dòng)一個(gè)個(gè)拷貝鍵值對(duì)來完成损同,如下
// 一個(gè)簡單的自定義結(jié)構(gòu)
struct person {
std::string name;
std::string address;
int age;
};
person p = {"Ned Flanders", "744 Evergreen Terrace", 60};
// person轉(zhuǎn)換成JSON: 把自定義對(duì)象中的每個(gè)值都拷貝給JSON對(duì)象
json j;
j["name"] = p.name;
j["address"] = p.address;
j["age"] = p.age;
// ...
// 由JSON轉(zhuǎn)成person: 把JSON對(duì)象中的每個(gè)值拷貝給自定義類型
person p {
j["name"].get<std::string>(),
j["address"].get<std::string>(),
j["age"].get<int>()
};
這樣確實(shí)可行,但實(shí)在談不上易用鸟款,所幸該庫提供了一個(gè)更好的方法:
// 創(chuàng)建person類型對(duì)象
person p {"Ned Flanders", "744 Evergreen Terrace", 60};
// person -> json
json j = p;
std::cout << j << std::endl;
// {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}
// json -> person
auto p2 = j.get<person>();
想要可以這樣轉(zhuǎn)換自定義類型膏燃,需要提供兩個(gè)方法:
using json = nlohmann::json;
void to_json(json& j, const person& p) {
j = json{{"name", p.name}, {"address", p.address}, {"age", p.age}};
}
void from_json(const json& j, person& p) {
j.at("name").get_to(p.name);
j.at("address").get_to(p.address);
j.at("age").get_to(p.age);
}
就是這樣,當(dāng)由自定義類型person
構(gòu)造JSON
時(shí)何什,會(huì)調(diào)用上面定義的void to_json(json& j, const person& p)
组哩。
相同的,由JSON
轉(zhuǎn)成自定義類型person
時(shí)处渣,會(huì)調(diào)用定義的void from_json(const json& j, person& p)
禁炒。
完整范例代碼如下:
#include <iostream>
#include "json.hpp" // 替換成放json.hpp的路徑
// 方便引用
using json = nlohmann::json;
// 一個(gè)簡單的自定義結(jié)構(gòu)
struct person {
std::string name;
std::string address;
int age;
};
// person -> json
void to_json(json& j, const person& p) {
j = json{{"name", p.name}, {"address", p.address}, {"age", p.age}};
}
// json -> person
void from_json(const json& j, person& p) {
j.at("name").get_to(p.name);
j.at("address").get_to(p.address);
j.at("age").get_to(p.age);
}
int main()
{
person p = {"Ned Flanders", "744 Evergreen Terrace", 60};
json j = p;
std::cout << j << std::endl;
auto p2 = j.get<person>();
return 0;
}
這種轉(zhuǎn)換方法的優(yōu)點(diǎn)是非侵入式,即無需修改原結(jié)構(gòu)體的代碼霍比,在新建的文件中聲明轉(zhuǎn)換方法即可幕袱。
以上就是Nlohmann JSON
庫的常用用法了,更詳盡的介紹可以詳見README或者json.nlohmann.me悠瞬。