1. 正文
nlohmann/json是一個C++的讀寫JSON的組件,號稱使用現代C++范式寫的。簡單看了一下,這個組件確實包含了很多cpp11以上的特性,在vs2015及一下的版本甚至沒辦法正常編譯。要正常使用需要vs2017及以上版本才行。
在使用過程中,遇到了一個問題是沒辦法保持插入的順序,每個插入的鍵值對會按照字符串的順序排列的,因為其內部用到了std:map。查看了github的主頁說明,是這么說的:
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).
這段話的意思是JSON標准的定義是零個或多個鍵值對對的無序集合,如果要保證插入順序,可以使用tsl::ordered_map(integration)或nlohmann::fifo_map(integration)等容器專門化對象類型。nlohmann::fifo_map同樣在github上找到,“專門化對象類型”的意思是nlohmann/json組件內部用到了很多std容器,只需要將其替換成可以保存插入順序的容器就可以了,也就是nlohmann::fifo_map。
重新找了一些英文資料,最終找到的解決方案如下:
#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不再是字符串順序而是插入順序:
