| 110 | } |
| 111 | |
| 112 | void first_example_d() |
| 113 | { |
| 114 | std::string path = "./input/books.json"; |
| 115 | std::fstream is(path); |
| 116 | if (!is) |
| 117 | { |
| 118 | std::cout << "Cannot open " << path << '\n'; |
| 119 | return; |
| 120 | } |
| 121 | json books = json::parse(is); |
| 122 | |
| 123 | auto options = json_options{} |
| 124 | .precision(2); |
| 125 | |
| 126 | for (const auto& book : books.at("books").array_range()) |
| 127 | { |
| 128 | try |
| 129 | { |
| 130 | std::string author = book["author"].as<std::string>(); |
| 131 | std::string title = book["title"].as<std::string>(); |
| 132 | if (book.contains("price") && book["price"].is_number()) |
| 133 | { |
| 134 | double price = book["price"].as<double>(); |
| 135 | std::cout << author << ", " << title << ", " << price << '\n'; |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | std::cout << author << ", " << title << ", " << "n/a" << '\n'; |
| 140 | } |
| 141 | } |
| 142 | catch (const std::exception& e) |
| 143 | { |
| 144 | std::cerr << e.what() << '\n'; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | |
| 150 | void second_example_a() |
| 151 | { |