Advertisement
Guest User

Untitled

a guest
Mar 14th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. // Standard headers
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5.  
  6. // External headers
  7. #include <Poco/JSON/Object.h>
  8. #include <Poco/JSON/Parser.h>
  9. #include <Poco/JSON/JSONException.h>
  10. #include <Poco/Dynamic/Var.h>
  11.  
  12. // Internal headers
  13. #include "parser.h"
  14.  
  15. using namespace Poco;
  16.  
  17. namespace wowbingo {
  18.  
  19. Parser::Parser() {
  20. }
  21.  
  22. Parser::Parser(const std::string &src) {
  23. setSource(src);
  24. }
  25.  
  26. void Parser::setSource(const std::string &src) {
  27. source = src;
  28. }
  29.  
  30. Storage Parser::getData() {
  31. return Parser::getData(source);
  32. }
  33.  
  34. Storage Parser::getData(const std::string &src) {
  35. Storage ret;
  36. std::fstream f;
  37. f.open(src);
  38.  
  39. if(!f.is_open()) {
  40. std::cout << "Can't open " << src << std::endl;
  41. } else {
  42. JSON::Parser parser;
  43. Dynamic::Var result;
  44.  
  45. try {
  46. result = parser.parse(f);
  47. } catch (JSON::JSONException e) {
  48. std::cout << e.displayText() << " (" << e.code() << ")" << std::endl;
  49. }
  50.  
  51. f.close();
  52.  
  53. if (!result) {
  54. std::cout << src << " parsed with errors" << std::endl;
  55. } else {
  56. // Get root JSON object
  57. JSON::Object::Ptr json = result.extract<JSON::Object::Ptr>();
  58.  
  59. // Get "update" JSON object
  60. JSON::Object::Ptr update = json.get()->getObject("update");
  61.  
  62. // Get regions list from "update" JSON object then print it out
  63. std::vector<std::string> regions_update;
  64. update.get()->getNames(regions_update);
  65.  
  66. std::cout << "The following regions have been found in \"update\"\t:";
  67.  
  68. for (const std::string &s: regions_update) {
  69. std::cout << s << " ";
  70. }
  71.  
  72. std::cout << std::endl;
  73.  
  74. // Get "history" JSON object
  75. JSON::Object::Ptr history = json.get()->getObject("history");
  76.  
  77. // Get regions list from "history" JSON object then print it out
  78. std::vector<std::string> regions_history;
  79. history.get()->getNames(regions_history);
  80.  
  81. std::cout << "The following regions have been found in \"history\"\t:";
  82.  
  83. for (const std::string &s: regions_history) {
  84. std::cout << s << " ";
  85. }
  86. std::cout << std::endl;
  87.  
  88. for (const std::string &region: regions_update) {
  89. // Get timeToSell value
  90. Dynamic::Var timeToSell = update->getObject(region)
  91. ->getObject("raw")
  92. ->get("timeToSell");
  93. // Get history array for "region"
  94. JSON::Array::Ptr arr = history.get()->getArray(region);
  95.  
  96. // Print the summary
  97. std::cout << "Region: " << region
  98. << " Samples: " << arr->size()
  99. << " TimeToSell: " << timeToSell.toString()
  100. << std::endl;
  101.  
  102. // Fill the data
  103. for (auto idx = 0; idx < arr->size(); idx++) {
  104. JSON::Array::Ptr sample = arr->getArray(idx);
  105. Dynamic::Var timestamp = sample->get(0);
  106. Dynamic::Var price = sample->get(1);
  107. ret << Sample (region,
  108. stoi(timestamp.toString()),
  109. stoi(price.toString()),
  110. static_cast<AuctionLength>(
  111. stoi(timeToSell.toString())));
  112. }
  113. }
  114. }
  115. }
  116.  
  117. return ret;
  118. }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement