MCPcopy Create free account
hub / github.com/danielaparker/jsoncons / introspection_example

Function introspection_example

examples/src/json_accessor_examples.cpp:93–121  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

91}
92
93void introspection_example()
94{
95 std::string path = "./input/books.json";
96 std::fstream is(path);
97 if (!is)
98 {
99 std::cout << "Cannot open " << path << '\n';
100 return;
101 }
102 json val = json::parse(is);
103 std::cout << std::boolalpha;
104 std::cout << "Is this an object? " << val.is_object() << ", or an array? " << val.is_array() << '\n';
105
106 if (val.at("books").is_array())
107 {
108 std::size_t index = 0;
109 for (const auto& book : val.at("books").array_range())
110 {
111 std::cout << "Is element " << index++ << " an object? " << book.is_object() << '\n';
112 if (book.is_object())
113 {
114 for (auto it = book.object_range().begin(); it != book.object_range().end(); ++it){
115 std::cout << "Is member " << it->key() << " a string? " << it->value().is<std::string>() << ", or a double? " << it->value().is<double>() << ", or perhaps an int? " << it->value().is<int>() << '\n';
116
117 }
118 }
119 }
120 }
121}
122
123void operator_at_examples()
124{

Callers 1

mainFunction · 0.85

Calls 8

parseFunction · 0.85
atMethod · 0.80
is_objectMethod · 0.45
is_arrayMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
keyMethod · 0.45
valueMethod · 0.45

Tested by

no test coverage detected