| 91 | } |
| 92 | |
| 93 | void 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 | |
| 123 | void operator_at_examples() |
| 124 | { |