nlohman json: nlohmann json github
假设我们需要创建一下的JSON 对象
{
"pi": 3.141,
"happy": true,
"name": "Niels",
"nothing": null,
"answer": {
"everything": 42
},
"list": [1, 0, 2],
"object": {
"currency": "USD",
"value": 42.99
}
}
我们可以使用一下两种方式:
// create an empty structure (null)
// 创建一个空结构体 null
json j;
// add a number that is stored as double (note the implicit conversion of j to an object)
// 添加一个数字并作为double 存储,(注意: j 会被隐式转换为 object)
j["pi"] = 3.141;
// add a Boolean that is stored as bool
// 添加一个 Boolean
j["happy"] = true;
// add a string that is stored as std::string
j["name"] = "Niels";
// add another null object by passing nullptr
j["nothing"] = nullptr;
// add an object inside the object
j["answer"]["everything"] = 42;
// add an array that is stored as std::vector (using an initializer list)
j["list"] = { 1, 0, 2 };
// add another object (using an initializer list of pairs)
j["object"] = { {"currency", "USD"}, {"value", 42.99} };
// instead, you could also write (which looks very similar to the JSON above)
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
你可以创建带有_json方式结尾的字符串创建json
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;
// or even nicer with a raw string literal
auto j2 = R"(
{
"happy": true,
"pi": 3.141
}
)"_json;
auto j3 = json::parse(R"({"happy": true, "pi": 3.14})");
Note: 这个库只支持 UTF-8,当遇到其他编码格式并调用 .dump 时,可能会抛出 json::error_handler_t::replace 或者 json::error_handler_t::ignore 的异常
// 显示转换
std::string s = j.dump(); // {"happy": true, "pi": 3,14}
// read a JSON file
std::ifstream i("file.json");
json j;
i >> j;
// write prettified JSON to another file
std::ofstream o("pretty.json");
o << std::setw(4) << j << std::endl;
可以从一个迭代器范围中解析JSON 对象。任何的顺序容器(std::array, std::vector, std::deque, std::forward_list, std::list) 的值可用于创建 JSON array (e.g int, float, bool, string, STL 容器)
std::vector<std::uint8_t> v = {'t', 'r', 'u', 'e'};
json j = json::parse(v.begin(), v.end());
// 或者 解析[begin, end)
json j = json::parse(v);
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); // only one entry for "one" is used
// ["four", "one", "three", "two"]
std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"};
json j_uset(c_uset); // only one entry for "one" is used
// maybe ["two", "three", "four", "one"]
std::multiset<std::string> c_mset {"one", "two", "one", "four"};
json j_mset(c_mset); // both entries for "one" are used
// maybe ["one", "two", "one", "four"]
std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"};
json j_umset(c_umset); // both entries for "one" are used
// maybe ["one", "two", "one", "four"]
从字典(关系型容器)中解析:
同样关联容器(std::set, std::multiset, std::unordered_set, std::unordered_multiset), 元素的顺序取决于在对应STL容器中顺序。
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); // only one entry for key "three" is used
// 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); // only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true}
struct MyContainer {
void advance();
const char& get_current();
};
struct MyIterator {
using difference_type = std::ptrdiff_t;
using value_type = char;
using pointer = const char*;
using reference = const char&;
using iterator_category = std::input_iterator_tag;
MyIterator& operator++() {
MyContainer.advance();
return *this;
}
bool operator!=(const MyIterator& rhs) const {
return rhs.target != target;
}
reference operator*() const {
return target.get_current();
}
MyContainer* target = nullptr;
};
MyIterator begin(MyContainer& tgt) {
return MyIterator{&tgt};
}
MyIterator end(const MyContainer&) {
return {};
}
void foo() {
MyContainer c;
json j = json::parse(c);
}
json j;
j.push_back("foo");
j.push_back(1);
j.push_back(true);
j.emplace_back(1.78);
// 访问
for (json::iterator it = j.begin(); it != j.end(); ++it) {
std::cout << *it << '\n';
}
for (auto& element : j) {
std::cout << element << '\n';
}
// 其他特性
j.size(); // 4
j.empty(); // false
j.type(); // json::value_t::array
j.clear();
// getter / setter
const auto tmp = j[0].get<std::string>();
j[1] = 43;
bool foo = j.at(2);
j == R"(["foo", 1, true, 1.78])"_json; // true
j.is_null();
j.is_boolean();
j.is_number();
j.is_string();
j.is_object();
j.is_array();
json o;
o["foo"] = 23;
o["bar"] = false;
o["baz"] = 3.2;
// emplace
o.emplace("weathre", "sunny");
// 迭代访问
for ( json::iterator it = o.begin(); it != o.end(); ++it) {
std::cout << it.key() << ":" << it.value() << "\n";
}
for(auto& el : o.items) {
std::cout << el.key() << " : " << el.value() << '\n';
}
for(auto& [key, value] : o.items()) {
std::std << key << ":" << value << '\n';
}
if (o.contains("foo")) {
}
if(o.find("foo") != o.end()) {
}
int foo_present = o.count("foo"); // 1
int fob_present = o.count("fob"); // 0
o.erase("foo");
JSON Pointer 作为处理结构化值得替代方法,除此以外,JSON Patch 允许描述两个JSON values 的差异。
// a JSON value
json j_original = R"({
"baz": ["one", "two", "three"],
"foo": "bar"
})"_json;
// access members with a JSON pointer (RFC 6901)
j_original["/baz/1"_json_pointer];
// "two"
// a JSON patch (RFC 6902)
json j_patch = R"([
{ "op": "replace", "path": "/baz", "value": "boo" },
{ "op": "add", "path": "/hello", "value": ["world"] },
{ "op": "remove", "path": "/foo"}
])"_json;
// apply the patch
json j_result = j_original.patch(j_patch);
// {
// "baz": "boo",
// "hello": ["world"]
// }
// calculate a JSON patch from two JSON values
json::diff(j_result, j_original);
// [
// { "op":" replace", "path": "/baz", "value": ["one", "two", "three"] },
// { "op": "remove","path": "/hello" },
// { "op": "add", "path": "/foo", "value": "bar" }
// ]