mirror of
https://github.com/Telecominfraproject/wlan-cloud-owprov.git
synced 2025-11-01 02:58:00 +00:00
Finishing framework refactor.
This commit is contained in:
@@ -1,294 +0,0 @@
|
||||
//
|
||||
// Created by stephane bourque on 2021-07-05.
|
||||
//
|
||||
|
||||
#ifndef UCENTRALGW_RESTAPI_UTILS_H
|
||||
#define UCENTRALGW_RESTAPI_UTILS_H
|
||||
#include <functional>
|
||||
|
||||
#include "Poco/JSON/Object.h"
|
||||
#include "Poco/JSON/Parser.h"
|
||||
#include "Poco/Net/HTTPServerRequest.h"
|
||||
#include "framework/OpenWifiTypes.h"
|
||||
#include "framework/Utils.h"
|
||||
|
||||
namespace OpenWifi::RESTAPI_utils {
|
||||
|
||||
void EmbedDocument(const std::string & ObjName, Poco::JSON::Object & Obj, const std::string &ObjStr);
|
||||
|
||||
inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, bool V) {
|
||||
Obj.set(Field,V);
|
||||
}
|
||||
|
||||
inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const std::string & S) {
|
||||
Obj.set(Field,S);
|
||||
}
|
||||
|
||||
inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const std::vector<Types::StringPair> & S) {
|
||||
Poco::JSON::Array Array;
|
||||
for(const auto &i:S) {
|
||||
Poco::JSON::Object O;
|
||||
O.set("tag",i.first);
|
||||
O.set("value", i.second);
|
||||
}
|
||||
Obj.set(Field,Array);
|
||||
}
|
||||
|
||||
inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const char * S) {
|
||||
Obj.set(Field,S);
|
||||
}
|
||||
|
||||
inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, uint64_t V) {
|
||||
Obj.set(Field,V);
|
||||
}
|
||||
|
||||
inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const Types::StringVec &V) {
|
||||
Poco::JSON::Array A;
|
||||
for(const auto &i:V)
|
||||
A.add(i);
|
||||
Obj.set(Field,A);
|
||||
}
|
||||
|
||||
inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const Types::TagList &V) {
|
||||
Poco::JSON::Array A;
|
||||
for(const auto &i:V)
|
||||
A.add(i);
|
||||
Obj.set(Field,A);
|
||||
}
|
||||
|
||||
inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const Types::CountedMap &M) {
|
||||
Poco::JSON::Array A;
|
||||
for(const auto &[Key,Value]:M) {
|
||||
Poco::JSON::Object O;
|
||||
O.set("tag",Key);
|
||||
O.set("value", Value);
|
||||
A.add(O);
|
||||
}
|
||||
Obj.set(Field,A);
|
||||
}
|
||||
|
||||
|
||||
template<typename T> void field_to_json(Poco::JSON::Object &Obj,
|
||||
const char *Field,
|
||||
const T &V,
|
||||
std::function<std::string(const T &)> F) {
|
||||
Obj.set(Field, F(V));
|
||||
}
|
||||
|
||||
template<typename T> bool field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, T & V,
|
||||
std::function<T(const std::string &)> F) {
|
||||
if(Obj->has(Field))
|
||||
V = F(Obj->get(Field).toString());
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, std::string &S) {
|
||||
if(Obj->has(Field))
|
||||
S = Obj->get(Field).toString();
|
||||
}
|
||||
|
||||
inline void field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, uint64_t &V) {
|
||||
if(Obj->has(Field))
|
||||
V = Obj->get(Field);
|
||||
}
|
||||
|
||||
inline void field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, bool &V) {
|
||||
if(Obj->has(Field))
|
||||
V = (Obj->get(Field).toString() == "true");
|
||||
}
|
||||
|
||||
inline void field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, Types::StringPairVec &Vec) {
|
||||
if(Obj->isArray(Field)) {
|
||||
auto O = Obj->getArray(Field);
|
||||
for(const auto &i:*O) {
|
||||
std::string S1,S2;
|
||||
auto Inner = i.extract<Poco::JSON::Object::Ptr>();
|
||||
if(Inner->has("tag"))
|
||||
S1 = Inner->get("tag").toString();
|
||||
if(Inner->has("value"))
|
||||
S2 = Inner->get("value").toString();
|
||||
auto P = std::make_pair(S1,S2);
|
||||
Vec.push_back(P);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, Types::StringVec &V) {
|
||||
if(Obj->isArray(Field)) {
|
||||
V.clear();
|
||||
Poco::JSON::Array::Ptr A = Obj->getArray(Field);
|
||||
for(const auto &i:*A) {
|
||||
V.push_back(i.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, Types::TagList &V) {
|
||||
if(Obj->isArray(Field)) {
|
||||
V.clear();
|
||||
Poco::JSON::Array::Ptr A = Obj->getArray(Field);
|
||||
for(const auto &i:*A) {
|
||||
V.push_back(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T> void field_to_json(Poco::JSON::Object &Obj, const char *Field, const std::vector<T> &Value) {
|
||||
Poco::JSON::Array Arr;
|
||||
for(const auto &i:Value) {
|
||||
Poco::JSON::Object AO;
|
||||
i.to_json(AO);
|
||||
Arr.add(AO);
|
||||
}
|
||||
Obj.set(Field, Arr);
|
||||
}
|
||||
|
||||
template<class T> void field_to_json(Poco::JSON::Object &Obj, const char *Field, const T &Value) {
|
||||
Poco::JSON::Object Answer;
|
||||
Value.to_json(Answer);
|
||||
Obj.set(Field, Answer);
|
||||
}
|
||||
|
||||
template<class T> void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, std::vector<T> &Value) {
|
||||
if(Obj->isArray(Field)) {
|
||||
Poco::JSON::Array::Ptr Arr = Obj->getArray(Field);
|
||||
for(auto &i:*Arr) {
|
||||
auto InnerObj = i.extract<Poco::JSON::Object::Ptr>();
|
||||
T NewItem;
|
||||
NewItem.from_json(InnerObj);
|
||||
Value.push_back(NewItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T> void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, T &Value) {
|
||||
if(Obj->isObject(Field)) {
|
||||
Poco::JSON::Object::Ptr A = Obj->getObject(Field);
|
||||
Value.from_json(A);
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string to_string(const Types::TagList & ObjectArray) {
|
||||
Poco::JSON::Array OutputArr;
|
||||
if(ObjectArray.empty())
|
||||
return "[]";
|
||||
for(auto const &i:ObjectArray) {
|
||||
OutputArr.add(i);
|
||||
}
|
||||
std::ostringstream OS;
|
||||
Poco::JSON::Stringifier::stringify(OutputArr,OS, 0,0, Poco::JSON_PRESERVE_KEY_ORDER );
|
||||
return OS.str();
|
||||
}
|
||||
|
||||
inline std::string to_string(const Types::StringVec & ObjectArray) {
|
||||
Poco::JSON::Array OutputArr;
|
||||
if(ObjectArray.empty())
|
||||
return "[]";
|
||||
for(auto const &i:ObjectArray) {
|
||||
OutputArr.add(i);
|
||||
}
|
||||
std::ostringstream OS;
|
||||
Poco::JSON::Stringifier::condense(OutputArr,OS);
|
||||
return OS.str();
|
||||
}
|
||||
|
||||
template<class T> std::string to_string(const std::vector<T> & ObjectArray) {
|
||||
Poco::JSON::Array OutputArr;
|
||||
if(ObjectArray.empty())
|
||||
return "[]";
|
||||
for(auto const &i:ObjectArray) {
|
||||
Poco::JSON::Object O;
|
||||
i.to_json(O);
|
||||
OutputArr.add(O);
|
||||
}
|
||||
std::ostringstream OS;
|
||||
Poco::JSON::Stringifier::condense(OutputArr,OS);
|
||||
return OS.str();
|
||||
}
|
||||
|
||||
template<class T> std::string to_string(const T & Object) {
|
||||
Poco::JSON::Object OutputObj;
|
||||
Object.to_json(OutputObj);
|
||||
std::ostringstream OS;
|
||||
Poco::JSON::Stringifier::condense(OutputObj,OS);
|
||||
return OS.str();
|
||||
}
|
||||
|
||||
inline Types::StringVec to_object_array(const std::string & ObjectString) {
|
||||
|
||||
Types::StringVec Result;
|
||||
if(ObjectString.empty())
|
||||
return Result;
|
||||
|
||||
try {
|
||||
Poco::JSON::Parser P;
|
||||
auto Object = P.parse(ObjectString).template extract<Poco::JSON::Array::Ptr>();
|
||||
for (auto const &i : *Object) {
|
||||
Result.push_back(i.toString());
|
||||
}
|
||||
} catch (...) {
|
||||
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
||||
inline OpenWifi::Types::TagList to_taglist(const std::string & ObjectString) {
|
||||
Types::TagList Result;
|
||||
if(ObjectString.empty())
|
||||
return Result;
|
||||
|
||||
try {
|
||||
Poco::JSON::Parser P;
|
||||
auto Object = P.parse(ObjectString).template extract<Poco::JSON::Array::Ptr>();
|
||||
for (auto const &i : *Object) {
|
||||
Result.push_back(i);
|
||||
}
|
||||
} catch (...) {
|
||||
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
||||
template<class T> std::vector<T> to_object_array(const std::string & ObjectString) {
|
||||
|
||||
std::vector<T> Result;
|
||||
if(ObjectString.empty())
|
||||
return Result;
|
||||
|
||||
try {
|
||||
Poco::JSON::Parser P;
|
||||
auto Object = P.parse(ObjectString).template extract<Poco::JSON::Array::Ptr>();
|
||||
for (auto const i : *Object) {
|
||||
auto InnerObject = i.template extract<Poco::JSON::Object::Ptr>();
|
||||
T Obj;
|
||||
Obj.from_json(InnerObject);
|
||||
Result.push_back(Obj);
|
||||
}
|
||||
} catch (...) {
|
||||
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
||||
template<class T> T to_object(const std::string & ObjectString) {
|
||||
T Result;
|
||||
|
||||
if(ObjectString.empty())
|
||||
return Result;
|
||||
|
||||
Poco::JSON::Parser P;
|
||||
auto Object = P.parse(ObjectString).template extract<Poco::JSON::Object::Ptr>();
|
||||
Result.from_json(Object);
|
||||
|
||||
return Result;
|
||||
}
|
||||
|
||||
template<class T> bool from_request(T & Obj, Poco::Net::HTTPServerRequest &Request) {
|
||||
Poco::JSON::Parser IncomingParser;
|
||||
auto RawObject = IncomingParser.parse(Request.stream()).extract<Poco::JSON::Object::Ptr>();
|
||||
Obj.from_json(RawObject);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // UCENTRALGW_RESTAPI_UTILS_H
|
||||
Reference in New Issue
Block a user