9 Commits

Author SHA1 Message Date
Pavetr
1893df8ca0 Merged dev into main and resolved main.cpp conflict 2026-04-21 11:22:12 +03:00
Pavetr
0a46003741 Added pragma once directive to Service 2026-04-21 11:21:11 +03:00
Pavetr
bde893a905 Created main program for testing 2026-04-21 11:20:51 +03:00
Pavetr
a9b258ceb3 Implemented JSON Service 2026-04-21 11:20:30 +03:00
Pavetr
823e4c83ef Added pragma once directive to Service 2026-04-21 11:12:19 +03:00
Pavetr
fa1be9fb42 Merged feature-xml into dev and resolved main.cpp conflict 2026-04-21 11:10:20 +03:00
Pavetr
ba0e8b81fa Merge branch 'feature-json' into dev 2026-04-21 10:53:35 +03:00
Pavetr
d757710f5d Created main program for testing 2026-04-21 10:51:42 +03:00
Pavetr
54f612e785 Implemented JSON Service 2026-04-21 10:43:39 +03:00
6 changed files with 25573 additions and 6 deletions

25
JsonService.cpp Normal file
View 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
View File

@@ -0,0 +1,8 @@
#include "Service.h"
#include <string>
class JsonService : public Service {
public:
virtual Weather getWeather(std::string s) override;
virtual ~JsonService() {}
};

View File

@@ -1,9 +1,10 @@
#pragma once
#include <string>
#include "Weather.h"
class Service {
public:
virtual ~Service() = default;
virtual Weather getWeather(std::string s) = 0;
};

View File

@@ -5,4 +5,4 @@ class XmlService : public Service {
public:
virtual Weather getWeather(std::string s) override;
virtual ~XmlService() {}
};
};

View File

@@ -1,15 +1,22 @@
#include <iostream>
#include <exception>
#include "JsonService.h"
#include "XmlService.h"
#include "Weather.h"
int main() {
try {
XmlService xs;
Weather w = xs.getWeather("endpoints/weather.xml");
std::cout << "Testing JSON Service" << std::endl;
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;
w.printWeather();
std::cout << "\nTesting XML Service" << std::endl;
XmlService xs;
Weather w2 = xs.getWeather("endpoints/weather.xml");
std::cout << "Weather object created (XML)" << std::endl;
w2.printWeather();
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;

25526
thirdparty/json.hpp vendored Normal file

File diff suppressed because it is too large Load Diff