mirror of
https://github.com/nlohmann/json.git
synced 2026-10-13 01:47:11 +00:00
deploy: 40a62947d3
This commit is contained in:
1 parent
79119509a9
commit
22fba27145
555 files changed
+17342
-1394
No files matched your search
@@ -30,13 +30,67 @@ The parser callback distinguishes the following events:
|
||||
depth and keeps every value (by always returning `#!cpp true`).
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/parse_event_t.cpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
// translate a parse_event_t to a human-readable name
|
||||
std::string event_name(json::parse_event_t event)
|
||||
{
|
||||
switch (event)
|
||||
{
|
||||
case json::parse_event_t::object_start:
|
||||
return "object_start";
|
||||
case json::parse_event_t::object_end:
|
||||
return "object_end";
|
||||
case json::parse_event_t::array_start:
|
||||
return "array_start";
|
||||
case json::parse_event_t::array_end:
|
||||
return "array_end";
|
||||
case json::parse_event_t::key:
|
||||
return "key";
|
||||
case json::parse_event_t::value:
|
||||
return "value";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// a small JSON text
|
||||
auto text = R"({"pi": 3.141, "numbers": [1, 2]})";
|
||||
|
||||
// parse the text and report every event together with its depth;
|
||||
// returning true keeps every value unchanged
|
||||
json j = json::parse(text, [](int depth, json::parse_event_t event, json& /*parsed*/)
|
||||
{
|
||||
std::cout << depth << " " << event_name(event) << '\n';
|
||||
return true;
|
||||
});
|
||||
|
||||
// the callback did not change anything, so the parsed value is unaffected
|
||||
std::cout << j << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/parse_event_t.output"
|
||||
0 object_start
|
||||
1 key
|
||||
1 value
|
||||
1 key
|
||||
1 array_start
|
||||
2 value
|
||||
2 value
|
||||
1 array_end
|
||||
0 object_end
|
||||
{"numbers":[1,2],"pi":3.141}
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
Reference in new issue
Block a user