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 ""; } }