material Item_factory::material_from_json(Item_tag new_id, Item_tag index, picojson::value::object value_map, int to_return){ //If the value isn't found, just return a group of null materials material material_list[2] = {MNULL, MNULL}; picojson::value::object::const_iterator value_pair = value_map.find(index); if(value_pair != value_map.end()){ if(value_pair->second.is<std::string>()){ material_list[0] = material_from_tag(new_id, value_pair->second.get<std::string>()); } else if (value_pair->second.is<picojson::array>()) { int material_count = 0; const picojson::array& materials_json = value_pair->second.get<picojson::array>(); for (picojson::array::const_iterator iter = materials_json.begin(); iter != materials_json.end(); ++iter) { if((*iter).is<std::string>()){ material_list[material_count] = material_from_tag(new_id, (*iter).get<std::string>()); } else { std::cerr << "Item "<< new_id << " has a non-string material listed." << std::endl; } ++material_count; if(material_count > 2){ std::cerr << "Item "<< new_id << " has too many materials listed." << std::endl; } } } else { std::cerr << "Item "<< new_id << " material was skipped, not a string or array of strings." << std::endl; } } return material_list[to_return]; }
//Grab int, with appropriate error handling int Item_factory::int_from_json(Item_tag new_id, Item_tag index, picojson::value::object value_map){ picojson::value::object::const_iterator value_pair = value_map.find(index); if(value_pair != value_map.end()){ if(value_pair->second.is<double>()){ return int(value_pair->second.get<double>()); } else { std::cerr << "Item "<< new_id << " attribute name was skipped, not a number." << std::endl; return 0; } } else { //If the value isn't found, just return a 0 return 0; } }
//Grab string, with appropriate error handling Item_tag Item_factory::string_from_json(Item_tag new_id, Item_tag index, picojson::value::object value_map){ picojson::value::object::const_iterator value_pair = value_map.find(index); if(value_pair != value_map.end()){ if(value_pair->second.is<std::string>()){ return value_pair->second.get<std::string>(); } else { std::cerr << "Item "<< new_id << " attribute " << index << "was skipped, not a string." << std::endl; return "Error: Unknown Value"; } } else { //If string is not found, just return an empty string return ""; } }
void JSONParseLog::JSONParseAndFill(picojson::value::object &obj, std::string indent) { picojson::value::object::const_iterator i = obj.begin(); for ( ; i != obj.end(); ++i ) { picojson::value v = i->second; if (i->second.is<picojson::object>()) { std::cout << indent << i->first << " : "<< std::endl; JSONParseAndFill(v.get<picojson::object>(), indent + " "); } else if (i->second.is<picojson::array>()) { std::cout << indent << i->first << " : ["<< std::endl; JSONParseAndFill(v.get<picojson::array>(), indent + " "); std::cout << " ]"<< std::endl; } else { std::cout << indent << i->first << " : " << i->second.to_str() << std::endl; //waiting weather.icon if (i->first == "icon") { std::shared_ptr<URL> spURL = APIConst::getURL(APIConst::Icon(i->second.to_str())); std::string logMessage = spURL->originalRequestString(); log(logMessage); Cache& cache = Cache::instance(); //log("Cache instance was created"); if (cache.check(spURL->originalRequestString()) != true){ //URL was not cached std::shared_ptr<HTTPRequest> request(new HTTPRequest(HTTPRequestType(HTTPRequestType::GET), spURL, HTTPProto(HTTPProto::HTTP1_0)) ); //log(spURL->originalRequestString()); request->setAction(std::make_shared<SaveFileAndCache>( i->second.to_str()+APIConst::instance().imgExtention, spURL) ); qmaster_.setHTTPRequest(request); } else { //URL was cached std::string fullPath = cache.getInfo(spURL->originalRequestString())->fullPath_; std::string logMessage = "URL " + spURL->originalRequestString() + " was already cached, "; logMessage += "Please look at " + fullPath + " file"; log(logMessage); } } } } }
void WeatherWidget::JSONParseAndFill(picojson::value::object &obj) { picojson::value::object::const_iterator i = obj.begin(); for ( ; i != obj.end(); ++i ) { picojson::value v = i->second; if (i->second.is<picojson::object>()) { JSONParseAndFill(v.get<picojson::object>()); } else if (i->second.is<picojson::array>()) { JSONParseAndFill(v.get<picojson::array>()); } else { setText(QString().fromStdString(i->first), QString().fromStdString(i->second.to_str()) ); } } }