Compare commits
3 Commits
dev
...
9e666a892d
| Author | SHA1 | Date | |
|---|---|---|---|
| 9e666a892d | |||
| c10755ad48 | |||
| 3e68a2959f |
@@ -1,25 +0,0 @@
|
|||||||
#include "JsonService.h"
|
|
||||||
#include "thirdparty/json.hpp"
|
|
||||||
#include <fstream>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
using nlohmann::json;
|
|
||||||
|
|
||||||
Weather JsonService::getWeather(std::string s) {
|
|
||||||
std::ifstream fin(s);
|
|
||||||
if (!fin)
|
|
||||||
throw std::runtime_error("error"); // Linux support
|
|
||||||
|
|
||||||
json j;
|
|
||||||
j = json::parse(fin);
|
|
||||||
|
|
||||||
std::string city = j["name"]; // Киров
|
|
||||||
double lon = j["coord"]["lon"]; // 49.6601
|
|
||||||
double lat = j["coord"]["lat"]; // 58.5966
|
|
||||||
double temperature = j["main"]["temp"]; // 5.69
|
|
||||||
std::string weather = j["weather"][0]["description"]; // дождь
|
|
||||||
double windSpeed = j["wind"]["speed"]; // 4.27
|
|
||||||
int clouds = j["clouds"]["all"]; // 100
|
|
||||||
|
|
||||||
return Weather(city, lon, lat, temperature, weather, windSpeed, clouds);
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
#include "Service.h"
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class JsonService : public Service {
|
|
||||||
public:
|
|
||||||
virtual Weather getWeather(std::string s) override;
|
|
||||||
virtual ~JsonService() {}
|
|
||||||
};
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
#pragma once
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "Weather.h"
|
#include "Weather.h"
|
||||||
|
|
||||||
|
|||||||
15
main.cpp
15
main.cpp
@@ -1,22 +1,15 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include "JsonService.h"
|
|
||||||
#include "XmlService.h"
|
#include "XmlService.h"
|
||||||
#include "Weather.h"
|
#include "Weather.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
try {
|
try {
|
||||||
std::cout << "Testing JsonService" << std::endl;
|
|
||||||
JsonService js;
|
|
||||||
Weather w1 = js.getWeather("endpoints/weather.json");
|
|
||||||
std::cout << "Weather object created (JSON)" << std::endl;
|
|
||||||
w1.printWeather();
|
|
||||||
|
|
||||||
std::cout << "\nTesting XmlService" << std::endl;
|
|
||||||
XmlService xs;
|
XmlService xs;
|
||||||
Weather w2 = xs.getWeather("endpoints/weather.xml");
|
Weather w = xs.getWeather("endpoints/weather.xml");
|
||||||
std::cout << "Weather object created (XML)" << std::endl;
|
|
||||||
w2.printWeather();
|
std::cout << "Weather object created" << std::endl;
|
||||||
|
w.printWeather();
|
||||||
}
|
}
|
||||||
catch (const std::exception& e) {
|
catch (const std::exception& e) {
|
||||||
std::cerr << "Error: " << e.what() << std::endl;
|
std::cerr << "Error: " << e.what() << std::endl;
|
||||||
|
|||||||
25526
thirdparty/json.hpp
vendored
25526
thirdparty/json.hpp
vendored
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user