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

Function first_example_d

examples/src/examples.cpp:112–148  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

110}
111
112void 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
150void second_example_a()
151{

Callers 1

mainFunction · 0.85

Calls 5

parseFunction · 0.85
atMethod · 0.80
precisionMethod · 0.45
containsMethod · 0.45
whatMethod · 0.45

Tested by

no test coverage detected