Compare commits
12 Commits
969251661c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 1893df8ca0 | |||
| 0a46003741 | |||
| bde893a905 | |||
| a9b258ceb3 | |||
| 9e666a892d | |||
| 823e4c83ef | |||
| fa1be9fb42 | |||
| ba0e8b81fa | |||
| d757710f5d | |||
| c10755ad48 | |||
| 54f612e785 | |||
| 3e68a2959f |
25
JsonService.cpp
Normal file
25
JsonService.cpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#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);
|
||||||
|
}
|
||||||
8
JsonService.h
Normal file
8
JsonService.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#include "Service.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class JsonService : public Service {
|
||||||
|
public:
|
||||||
|
virtual Weather getWeather(std::string s) override;
|
||||||
|
virtual ~JsonService() {}
|
||||||
|
};
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "Weather.h"
|
#include "Weather.h"
|
||||||
|
|
||||||
|
|||||||
15
main.cpp
15
main.cpp
@@ -1,15 +1,22 @@
|
|||||||
#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 {
|
||||||
XmlService xs;
|
std::cout << "Testing JSON Service" << std::endl;
|
||||||
Weather w = xs.getWeather("endpoints/weather.xml");
|
JsonService js;
|
||||||
|
Weather w1 = js.getWeather("endpoints/weather.json");
|
||||||
|
std::cout << "Weather object created (JSON)" << std::endl;
|
||||||
|
w1.printWeather();
|
||||||
|
|
||||||
std::cout << "Weather object created" << std::endl;
|
std::cout << "\nTesting XML Service" << std::endl;
|
||||||
w.printWeather();
|
XmlService xs;
|
||||||
|
Weather w2 = xs.getWeather("endpoints/weather.xml");
|
||||||
|
std::cout << "Weather object created (XML)" << std::endl;
|
||||||
|
w2.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
Normal file
25526
thirdparty/json.hpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user