Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Standard headers
- #include <iostream>
- #include <fstream>
- #include <string>
- // External headers
- #include <Poco/JSON/Object.h>
- #include <Poco/JSON/Parser.h>
- #include <Poco/JSON/JSONException.h>
- #include <Poco/Dynamic/Var.h>
- // Internal headers
- #include "parser.h"
- using namespace Poco;
- namespace wowbingo {
- Parser::Parser() {
- }
- Parser::Parser(const std::string &src) {
- setSource(src);
- }
- void Parser::setSource(const std::string &src) {
- source = src;
- }
- Storage Parser::getData() {
- return Parser::getData(source);
- }
- Storage Parser::getData(const std::string &src) {
- Storage ret;
- std::fstream f;
- f.open(src);
- if(!f.is_open()) {
- std::cout << "Can't open " << src << std::endl;
- } else {
- JSON::Parser parser;
- Dynamic::Var result;
- try {
- result = parser.parse(f);
- } catch (JSON::JSONException e) {
- std::cout << e.displayText() << " (" << e.code() << ")" << std::endl;
- }
- f.close();
- if (!result) {
- std::cout << src << " parsed with errors" << std::endl;
- } else {
- // Get root JSON object
- JSON::Object::Ptr json = result.extract<JSON::Object::Ptr>();
- // Get "update" JSON object
- JSON::Object::Ptr update = json.get()->getObject("update");
- // Get regions list from "update" JSON object then print it out
- std::vector<std::string> regions_update;
- update.get()->getNames(regions_update);
- std::cout << "The following regions have been found in \"update\"\t:";
- for (const std::string &s: regions_update) {
- std::cout << s << " ";
- }
- std::cout << std::endl;
- // Get "history" JSON object
- JSON::Object::Ptr history = json.get()->getObject("history");
- // Get regions list from "history" JSON object then print it out
- std::vector<std::string> regions_history;
- history.get()->getNames(regions_history);
- std::cout << "The following regions have been found in \"history\"\t:";
- for (const std::string &s: regions_history) {
- std::cout << s << " ";
- }
- std::cout << std::endl;
- for (const std::string ®ion: regions_update) {
- // Get timeToSell value
- Dynamic::Var timeToSell = update->getObject(region)
- ->getObject("raw")
- ->get("timeToSell");
- // Get history array for "region"
- JSON::Array::Ptr arr = history.get()->getArray(region);
- // Print the summary
- std::cout << "Region: " << region
- << " Samples: " << arr->size()
- << " TimeToSell: " << timeToSell.toString()
- << std::endl;
- // Fill the data
- for (auto idx = 0; idx < arr->size(); idx++) {
- JSON::Array::Ptr sample = arr->getArray(idx);
- Dynamic::Var timestamp = sample->get(0);
- Dynamic::Var price = sample->get(1);
- ret << Sample (region,
- stoi(timestamp.toString()),
- stoi(price.toString()),
- static_cast<AuctionLength>(
- stoi(timeToSell.toString())));
- }
- }
- }
- }
- return ret;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement