Dig deeper
[dueringa_WikiWalker.git] / sandbox / jsontest.cpp
blob092d05d7a4f7d156f6bdbcd959bf887270d9a6b0
1 #include <string>
2 #include <sstream>
3 #include <json/json.h>
5 class Myexcept : public std::exception
7 public:
8 Myexcept(std::string message)
9 : message(message){};
10 ~Myexcept() throw(){};
11 const char* what() const throw()
13 return message.c_str();
15 private:
16 std::string message;
19 int main()
21 std::string json = "{\"batchcomplete\":\"\",\"query\":{\"normalized\":[{\"from\":\"Amundsen_Basin\",\"to\":\"Amundsen Basin\"}],\"pages\":{\"37082520\":{\"pageid\":37082520,\"ns\":0,\"title\":\"Amundsen Basin\",\"links\":[{\"ns\":0,\"title\":\"Abyssal\"},{\"ns\":0,\"title\":\"Arctic\"},{\"ns\":0,\"title\":\"Arctic Ocean\"},{\"ns\":0,\"title\":\"Cenozoic\"},{\"ns\":0,\"title\":\"Eurasian Basin\"},{\"ns\":0,\"title\":\"Gakkel Ridge\"},{\"ns\":0,\"title\":\"Lomonosov Ridge\"},{\"ns\":0,\"title\":\"Nansen Basin\"},{\"ns\":0,\"title\":\"Roald Amundsen\"},{\"ns\":0,\"title\":\"Seafloor spreading\"},{\"ns\":0,\"title\":\"Wilkes Basin\"}]}}}}";
22 Json::Reader reader;
23 Json::Value document;
25 // this works
26 ////std::istringstream jsondatastream(json);
27 ////jsondatastream >> document;
29 // this, too
30 // this gives better error reporting?
31 bool success = reader.parse(json, document, false);
33 if(!success) {
34 throw Myexcept("Error parsing JSON");
37 bool needmoredata = !document.isMember("batchcomplete");
38 //! TODO: fetch more
40 auto next = document.get("query", Json::Value::nullSingleton());
42 if (next == Json::Value::nullSingleton()) {
43 throw Myexcept("Error parsing JSON - no query result");
46 next = next.get("pages", Json::Value::nullSingleton());
48 if (next == Json::Value::nullSingleton()) {
49 throw Myexcept("Error parsing JSON - no pages result");