nlohmann/json是一個C++的讀寫JSON的組件,號稱使用現代C++範式寫的。簡單看了一下,這個組件確實包含了不少cpp11以上的特性,在vs2015及一下的版本甚至沒辦法正常編譯。要正常使用須要vs2017及以上版本才行。ios
在使用過程當中,遇到了一個問題是沒辦法保持插入的順序,每一個插入的鍵值對會按照字符串的順序排列的,由於其內部用到了std:map。查看了github的主頁說明,是這麼說的:git
By default, the library does not preserve the insertion order of object elements. This is standards-compliant, as the JSON standard defines objects as "an unordered collection of zero or more name/value pairs". If you do want to preserve the insertion order, you can specialize the object type with containers like tsl::ordered_map (integration) or nlohmann::fifo_map (integration).github
這段話的意思是JSON標準的定義是零個或多個鍵值對對的無序集合,若是要保證插入順序,能夠使用tsl::ordered_map(integration)或nlohmann::fifo_map(integration)等容器專門化對象類型。nlohmann::fifo_map一樣在github上找到,「專門化對象類型」的意思是nlohmann/json組件內部用到了不少std容器,只須要將其替換成能夠保存插入順序的容器就能夠了,也就是nlohmann::fifo_map。json
從新找了一些英文資料,最終找到的解決方案以下:app
#include "json.hpp" #include "fifo_map.hpp" #include <iostream> using namespace nlohmann; // A workaround to give to use fifo_map as map, we are just ignoring the 'less' compare template<class K, class V, class dummy_compare, class A> using my_workaround_fifo_map = fifo_map<K, V, fifo_map_compare<K>, A>; using my_json = basic_json<my_workaround_fifo_map>; int main() { my_json j; j["f"] = 5; j["a"] = 2; my_json j2 = { {"pi", 3.141}, {"happy", true}, {"name", "Niels"}, {"nothing", nullptr}, {"answer", { {"everything", 42} }}, {"list", {1, 0, 2}}, {"object", { {"currency", "USD"}, {"value", 42.99} }} }; std::cout << j.dump(4) << std::endl; std::cout << j2.dump(4) << std::endl; return 0; }
運行結果以下所示,能夠看到輸出的JSON再也不是字符串順序而是插入順序:less
[1] nlohmann/json主頁介紹
[2] nlohmann/json關於保留插入順序的討論spa