diff --git a/CrestApi/CrestContainer.h b/CrestApi/CrestContainer.h index f1e3283dceadbaceccc9a3f7f49dd85be0e894d2..ceaa37b6f09342933e27c9fc12066a13467e6791 100644 --- a/CrestApi/CrestContainer.h +++ b/CrestApi/CrestContainer.h @@ -14,6 +14,25 @@ namespace Crest { Float, Double, String255, String4k, String64k, String16M, Blob64k, Blob16M, TypeIdCount }; + static std::map<TypeId, std::string> s_typeToString = { + { Bool, "Bool" }, + { UChar, "UChar" }, + { Int16, "Int16" }, + { UInt16, "UInt16" }, + { Int32, "Int32" }, + { UInt32, "UInt32" }, + { UInt63, "UInt63" }, + { Int64, "Int64" }, + { Float, "Float" }, + { Double, "Double" }, + { String255, "String255" }, + { String4k, "String4k" }, + { String64k, "String64k" }, + { String16M, "String16M" }, + { Blob64k, "Blob64k" }, + { Blob16M, "Blob16M" } + }; + enum ModeId{ Standard, DCS, DCS_FULL }; diff --git a/CrestApi/CrestModel.h b/CrestApi/CrestModel.h index 225ad0bb2bb7abf72c0c6f0f1a46c29b215cace9..a0ef692b92fc720685a96b4c7482a66e1a410914 100644 --- a/CrestApi/CrestModel.h +++ b/CrestApi/CrestModel.h @@ -39,17 +39,19 @@ public: class CrestBaseResponse { private: - int64_t m_size; + //int64_t m_size; public: - std::string datatype; - std::string format; + virtual ~CrestBaseResponse() = default; + std::optional<std::string> datatype; + //std::string format; std::optional<RespPage> page; std::optional<GenericMap> filter; - CrestBaseResponse():datatype("data"),format("StoreSetDto"){} - CrestBaseResponse(std::string _datatype,std::string _format):datatype(_datatype),format(_format){} json to_json() const; - void setSize(int64_t l_size){m_size=l_size;} - static CrestBaseResponse from_json(const json &j); + void load_from_json(const json &j); + virtual const char* getFormat() const =0; + virtual int64_t getSize() const =0; +// virtual void setFormat(std::string str); +// virtual void setSize(int64_t size); }; class GlobalTagDto @@ -59,7 +61,7 @@ public: int64_t validity; std::string description; std::string release; - std::string insertionTime; + std::optional<std::string> insertionTime; std::string snapshotTime; std::string scenario; std::string workflow; @@ -82,6 +84,8 @@ class GlobalTagSetDto : public CrestBaseResponse { public: std::vector<GlobalTagDto> resources; + const char* getFormat() const {return "GlobalTagSetDto";} + int64_t getSize() const{return resources.size();} json to_json() const; @@ -103,18 +107,18 @@ public: std::string description; long_t lastValidatedTime; long_t endOfValidity; - std::string insertionTime; - std::string modificationTime; + std::optional<std::string> insertionTime; + std::optional<std::string> modificationTime; // Ctor TagDto(std::string _name, std::string _timeType, std::string _description): name(_name), timeType(_timeType), objectType(""), - synchronization(""), + synchronization("none"), description(_description), - lastValidatedTime(0), endOfValidity(0), insertionTime(""), modificationTime(""){ + lastValidatedTime(0), endOfValidity(0){ } // Default Ctor - TagDto(): name(""), timeType(""), objectType(""), synchronization(""), description(""), lastValidatedTime(0), endOfValidity(0), insertionTime(""), modificationTime("") { + TagDto(): name(""), timeType(""), objectType(""), synchronization("none"), description(""), lastValidatedTime(0), endOfValidity(0) { } json to_json() const; @@ -125,6 +129,8 @@ class TagSetDto : public CrestBaseResponse { public: std::vector<TagDto> resources; + const char* getFormat() const {return "TagSetDto";} + int64_t getSize() const{return resources.size();} json to_json() const; @@ -157,6 +163,8 @@ class GlobalTagMapSetDto : public CrestBaseResponse { public: std::vector<GlobalTagMapDto> resources; + const char* getFormat() const {return "GlobalTagMapSetDto";} + int64_t getSize() const{return resources.size();} json to_json() const; static GlobalTagMapSetDto from_json(const json &j); @@ -164,22 +172,83 @@ public: static GlobalTagMapSetDto from_fs_json(const json &j); }; +class ChannelSetDto{ +private: + std::vector< std::pair<std::string,std::string> > m_channels; +public: + ChannelSetDto& operator=(const ChannelSetDto&) = default; + ChannelSetDto& operator=(ChannelSetDto&&) = default; + + ChannelSetDto(){} + ChannelSetDto(ChannelSetDto const& copy):m_channels(copy.getChannels()){} + void add(std::string id,std::string name); + std::vector< std::pair<std::string,std::string> > getChannels() const {return m_channels;} + size_t getSize() const {return m_channels.size();} + json to_json() const; + static ChannelSetDto from_json(const json &j); +}; + +class PayloadSpecDto{ +private: + std::vector< std::pair<std::string,std::string> > m_row; +public: + PayloadSpecDto& operator=(const PayloadSpecDto&) = default; + PayloadSpecDto& operator=(PayloadSpecDto&&) = default; + PayloadSpecDto(){} + PayloadSpecDto(PayloadSpecDto const& copy):m_row(copy.getColumns()){} + std::vector< std::pair<std::string,std::string> > getColumns() const {return m_row;} + size_t getSize() const {return m_row.size();} + void add(std::string id,std::string name); + json to_json() const; + static PayloadSpecDto from_json(const json &j); +}; + +class TagInfoDto{ +private: + std::string m_node_description; + PayloadSpecDto m_payload_spec; + ChannelSetDto m_channel_list; +public: + TagInfoDto(std::string description):m_node_description(description){ + } + TagInfoDto(std::string description,PayloadSpecDto& payload_spec,ChannelSetDto& channel_list): + m_node_description(description),m_payload_spec(payload_spec),m_channel_list(channel_list){ + } + void setPayloadSpec(PayloadSpecDto& spec){ + m_payload_spec=spec; + } + void setChannel(ChannelSetDto& ch){ + m_channel_list=ch; + } + PayloadSpecDto getPayloadSpec() + { + return m_payload_spec; + } + ChannelSetDto getChannels() + { + return m_channel_list; + } + size_t getChannelSize() const {return m_channel_list.getSize();} + size_t getColumnSize() const {return m_payload_spec.getSize();} + json to_json() const; + static TagInfoDto from_json(const json &j); + +}; + class TagMetaDto { public: std::string tagName; std::string description; - int chansize; - int colsize; - std::string tagInfo; - std::string insertionTime; + std::optional<std::string> insertionTime; + TagInfoDto tagInfo; // Ctor - TagMetaDto(std::string tagName, std::string description, int chansize, int colsize, std::string tagInfo): - tagName(tagName), description(description), chansize(chansize), colsize(colsize), tagInfo(tagInfo), insertionTime(""){ + TagMetaDto(std::string tagName, std::string description, const TagInfoDto& info): + tagName(tagName), description(description), tagInfo(info){ } // Default Ctor - TagMetaDto(): tagName(""), description(""), chansize(0), colsize(0), tagInfo(""), insertionTime(""){ + TagMetaDto(): tagName(""), description(""), tagInfo(""){ } json to_json() const; @@ -190,6 +259,8 @@ class TagMetaSetDto : public CrestBaseResponse { public: std::vector<TagMetaDto> resources; + const char* getFormat() const {return "TagMetaSetDto";} + int64_t getSize() const{return resources.size();} json to_json() const; static TagMetaSetDto from_json(const json &j); @@ -200,14 +271,14 @@ class IovDto public: std::string tagName; long_t since; - std::string insertionTime; + std::optional<std::string> insertionTime; std::string payloadHash; // Ctor IovDto(std::string tagName, long_t since, std::string payloadHash): - tagName(tagName), since(since), insertionTime(""), payloadHash(payloadHash) { + tagName(tagName), since(since), payloadHash(payloadHash) { } // Default Ctor - IovDto(): tagName(""), since(0), insertionTime(""), payloadHash("") { + IovDto(): tagName(""), since(0), payloadHash("") { } json to_json() const; @@ -218,6 +289,8 @@ class IovSetDto : public CrestBaseResponse { public: std::vector<IovDto> resources; + const char* getFormat() const {return "IovSetDto";} + int64_t getSize() const{return resources.size();} json to_json() const; @@ -228,25 +301,29 @@ public: class StoreDto { + std::optional<std::string> m_app_name; + std::optional<std::string> m_app_version; public: long_t since; std::string hash; std::string data; - std::string streamerInfo; - + std::optional<std::string> insertionTime; StoreDto(long_t l_since, std::string l_data): - since(l_since), hash(""), data(l_data), streamerInfo("{}"){ + since(l_since), hash(""), data(l_data){ } // Ctor StoreDto(long_t since, std::string hash, std::string data): - since(since), hash(hash), data(data), streamerInfo("{}"){ + since(since), hash(hash), data(data){ } // Default Ctor - StoreDto(): since(0), hash(""), data(""), streamerInfo("{}"){ + StoreDto(): since(0), hash(""), data(""){ } + void setAppVersion (const char* str){m_app_version=str;} + void setAppName (const char* str){m_app_name=str;} + json getStreamerInfo() const; json to_json() const; static StoreDto from_json(const json &j); @@ -256,9 +333,10 @@ class StoreSetDto : public CrestBaseResponse { public: std::vector<StoreDto> resources; - StoreSetDto():CrestBaseResponse(){;} + const char* getFormat() const {return "StoreSetDto";} + int64_t getSize() const{return resources.size();} + void push_back(StoreDto dto); - size_t size(); void clear(); json to_json() const; @@ -275,7 +353,7 @@ public: std::string compressionType; std::string checkSum; int size; - std::string insertionTime; + std::optional<std::string> insertionTime; // Function to serialize the object to JSON @@ -290,6 +368,8 @@ class PayloadSetDto : public CrestBaseResponse { public: std::vector<PayloadDto> resources; + const char* getFormat() const {return "PayloadSetDto";} + int64_t getSize() const{return resources.size();} // Function to serialize the object to JSON diff --git a/src/CrestApiFs.cxx b/src/CrestApiFs.cxx index 486a660dfc5d19ac02d9ff06199e843d4048caf8..f28d01905aca7842ff829acdcc6dae0d75a51d06 100644 --- a/src/CrestApiFs.cxx +++ b/src/CrestApiFs.cxx @@ -166,7 +166,7 @@ namespace Crest std::string folder = m_root_folder + s_FS_GLOBALTAG_PATH; GlobalTagSetDto tagSet; - int tagNumber = 0; + //int tagNumber = 0; bool ascending = true; if (sort == "name:ASC") @@ -208,13 +208,13 @@ namespace Crest for (std::string tag : taglist) { std::string file_name = folder + "/" + tag + "/" + s_FS_GLOBALTAG_FILE; - tagNumber++; + //tagNumber++; GlobalTagDto dto = findGlobalTag(tag); tagSet.resources.push_back(dto); } - tagSet.setSize(tagNumber); + //tagSet.setSize(tagNumber); tagSet.datatype = ""; - tagSet.format = "TagSetDto"; + //tagSet.format = "TagSetDto"; } catch (const std::exception &e) { @@ -295,7 +295,7 @@ namespace Crest std::string folder = m_root_folder + s_FS_TAG_PATH; TagSetDto tagSet; - int tagNumber = 0; + //int tagNumber = 0; bool ascending = true; if (sort == "name:ASC") @@ -337,13 +337,13 @@ namespace Crest for (std::string tag : taglist) { std::string file_name = folder + "/" + tag + "/" + s_FS_TAG_FILE; - tagNumber++; + //tagNumber++; TagDto dto = findTag(tag); tagSet.resources.push_back(dto); } - tagSet.setSize(tagNumber); + //tagSet.setSize(tagNumber); tagSet.datatype = ""; - tagSet.format = "TagSetDto"; + //tagSet.format = "TagSetDto"; } catch (const std::exception &e) { diff --git a/src/CrestContainer.cxx b/src/CrestContainer.cxx index fcdb6b85440ee84707a3d9cb058d0b5bb1ebab62..3b596523bbdcdf058e07abcc42ae8e83caaca64c 100644 --- a/src/CrestContainer.cxx +++ b/src/CrestContainer.cxx @@ -8,6 +8,8 @@ #include <fstream> #include <iomanip> +using json = nlohmann::json; + Crest::CrestContainer::CrestContainer() : m_modeId(Standard) { ; } Crest::CrestContainer::~CrestContainer() { @@ -165,8 +167,8 @@ void Crest::CrestContainer::addData(const char *channel_id) void Crest::CrestContainer::addExternalData(const char *channel_id, const nlohmann::json &data) { - std::cout << "CondContainer::addData: add data for channel " << channel_id << std::endl; - std::cout << "CondContainer::addData: row " << data.dump() << std::endl; + //std::cout << "CondContainer::addData: add data for channel " << channel_id << std::endl; + //std::cout << "CondContainer::addData: row " << data.dump() << std::endl; validatePayloadSize(); @@ -216,8 +218,8 @@ nlohmann::json Crest::CrestContainer::createRowArray(const nlohmann::json &data_ void Crest::CrestContainer::addIov(const uint64_t since) { - std::cout << "CondContainer::addIov: add data iov for since " << since << std::endl; - std::cout << "CondContainer::addIov: data:" << m_payload.dump() << std::endl; + //std::cout << "CondContainer::addIov: add data iov for since " << since << std::endl; + //std::cout << "CondContainer::addIov: data:" << m_payload.dump() << std::endl; m_iov_data["since"] = since; m_iov_data["data"] = m_payload; m_payload.clear(); @@ -231,19 +233,19 @@ const std::vector<std::pair<std::string, Crest::TypeId>> &Crest::CrestContainer: const nlohmann::json &Crest::CrestContainer::getPayloadChannel(const char *channel_id) { - std::cout << "CondContainer::getPayloadChannel: channel_id = " << channel_id << std::endl; + //std::cout << "CondContainer::getPayloadChannel: channel_id = " << channel_id << std::endl; if (m_payload.empty()) { m_payload = m_iov_data["data"]; } - std::cout << "CondContainer::getPayloadChannel: " << m_payload.dump() << std::endl; + //std::cout << "CondContainer::getPayloadChannel: " << m_payload.dump() << std::endl; auto it = m_payload.find(channel_id); if (it == m_payload.end()) { std::string msg = "Channel id " + std::string(channel_id) + " is not found."; throw CommonCrestException(msg.c_str()); } - std::cout << "CrestContainer::getPayloadChannel: channel_id = " << channel_id << ", data = " << it.value() << std::endl; + //std::cout << "CrestContainer::getPayloadChannel: channel_id = " << channel_id << ", data = " << it.value() << std::endl; if (m_row.empty()) { for (const auto &column : m_payload_spec) @@ -291,7 +293,7 @@ void Crest::CrestContainer::putRow2Vector() const nlohmann::json &Crest::CrestContainer::getRow() { - std::cout << "CondContainer::getRow: row " << m_row.dump() << " - data: " << m_vector_data << std::endl; + //std::cout << "CondContainer::getRow: row " << m_row.dump() << " - data: " << m_vector_data << std::endl; if (m_isVectorPayload) return m_vector_data; // This is a temporary object, changing for each channel. @@ -343,14 +345,20 @@ std::string Crest::CrestContainer::getJsonIovData() return m_iov_data.dump(); } -nlohmann::json Crest::CrestContainer::getPayloadSpec() +json Crest::CrestContainer::getPayloadSpec() { - nlohmann::json pspec_data(nlohmann::json::value_t::array); + json pspec_data=json::array(); for (auto &column : m_payload_spec) { - nlohmann::json j; + json j={}; std::cout << "CondContainer::getPayloadSpec: name = " << column.first << ", type = " << column.second << std::endl; - j[column.first] = std::to_string(column.second); + std::map<TypeId, std::string>::const_iterator pos = Crest::s_typeToString.find(column.second); + if (pos == Crest::s_typeToString.end()) { + std::cout << "CondContainer::getPayloadSpec: Type do not exist in the map. Type: name = " << column.first << ", type = " << column.second << std::endl; + throw CommonCrestException("Type do not exist in the map."); + } else { + j[column.first] = pos->second; + } pspec_data.push_back(j); } return pspec_data; @@ -364,7 +372,7 @@ void Crest::CrestContainer::setPayloadSpec(const nlohmann::json &j) { for (const auto &[name, type] : column.items()) { - std::cout << "CondContainer::setPayloadSpec: name = " << name << ", type = " << type << std::endl; + //std::cout << "CondContainer::setPayloadSpec: name = " << name << ", type = " << type << std::endl; addColumn(name, static_cast<TypeId>(std::stoi(type.get<std::string>()))); } } @@ -373,7 +381,7 @@ void Crest::CrestContainer::setPayloadSpec(const nlohmann::json &j) { for (const auto &[name, type] : j.items()) { - std::cout << "CondContainer::setPayloadSpec: name = " << name << ", type = " << type << std::endl; + //std::cout << "CondContainer::setPayloadSpec: name = " << name << ", type = " << type << std::endl; addColumn(name, static_cast<TypeId>(type.get<int>())); } } @@ -395,7 +403,7 @@ void Crest::CrestContainer::dump_json_to_file(const nlohmann::json &j, const std { file << std::setprecision(6) << j.dump(4); file.close(); - std::cout << "CondContainer::dump_json_to_file: JSON object dumped to file: " << filename << std::endl; + //std::cout << "CondContainer::dump_json_to_file: JSON object dumped to file: " << filename << std::endl; } else { @@ -413,7 +421,7 @@ nlohmann::json Crest::CrestContainer::read_json_from_file(const std::string &fil { specfile >> jspec; specfile.close(); - std::cout << "CondContainer::read_json_from_file: JSON specification object read from file: " << spec_filename << std::endl; + //std::cout << "CondContainer::read_json_from_file: JSON specification object read from file: " << spec_filename << std::endl; } else { @@ -428,7 +436,7 @@ nlohmann::json Crest::CrestContainer::read_json_from_file(const std::string &fil { file >> j; file.close(); - std::cout << "CondContainer::read_json_from_file: JSON object read from file: " << filename << std::endl; + //std::cout << "CondContainer::read_json_from_file: JSON object read from file: " << filename << std::endl; } else { diff --git a/src/CrestModel.cxx b/src/CrestModel.cxx index 54fc8eb739cb85109f2a872f1958cdd1a7762cfd..bc30a3bb82d5439d997427f123a6b08e15ff0f2a 100644 --- a/src/CrestModel.cxx +++ b/src/CrestModel.cxx @@ -1,5 +1,7 @@ #include <CrestApi/CrestModel.h> #include <iostream> +#include <string> +#include <iomanip> json RespPage::to_json() const { @@ -46,11 +48,16 @@ json CrestBaseResponse::to_json() const { - json result = { - {"size", m_size}, - {"datatype", datatype}, - {"format", format} - }; + json result={}; + if(datatype.has_value()) + result["datatype"] = datatype.value(); + result["format"]=getFormat(); + result["size"]=getSize(); +// json result = { +// {"size", m_size}, +// {"datatype", datatype}, +// {"format", format} +// }; // Check if the 'page' optional contains a value if (page.has_value()) { @@ -71,27 +78,26 @@ return result; } - CrestBaseResponse CrestBaseResponse::from_json(const json &j) + void CrestBaseResponse::load_from_json(const json &j) { - CrestBaseResponse crestResponse; - crestResponse.m_size = j.value("size", 0); - crestResponse.datatype = j.value("datatype", ""); - crestResponse.format = j.value("format", ""); + if (j.contains("datatype")) + datatype = j.value("datatype", ""); + //crestResponse.setSize(j.value("size", 0)); + //crestResponse.setFormat(j.value("format", "")); // Check for the presence of "page" key and create an optional if (j.contains("page")){ if (!j["page"].is_null()){ - crestResponse.page = std::make_optional((RespPage::from_json(j["page"]))); + page = std::make_optional((RespPage::from_json(j["page"]))); } } - else { - crestResponse.page = std::nullopt; - } + //else { + // crestResponse.page = std::nullopt; + //} // Check for the presence of "filter" key and create an optional - crestResponse.filter = j.contains("filter") ? std::make_optional(GenericMap::from_json(j["filter"])) : std::nullopt; + filter = j.contains("filter") ? std::make_optional(GenericMap::from_json(j["filter"])) : std::nullopt; - return crestResponse; } json GlobalTagSetDto::to_json() const { @@ -108,33 +114,34 @@ GlobalTagSetDto GlobalTagSetDto::from_json(const json &j) { GlobalTagSetDto globalTagSet; + globalTagSet.load_from_json(j); json jsonResources = j.value("resources", json::array()); for (auto it = jsonResources.begin(); it != jsonResources.end(); ++it) { globalTagSet.resources.push_back(GlobalTagDto::from_json(*it)); } - globalTagSet.setSize(j.value("size", 0)); - globalTagSet.datatype = j.value("datatype", ""); - globalTagSet.format = j.value("format", "GlobalTagSetDto"); + //globalTagSet.setSize(j.value("size", 0)); + //globalTagSet.datatype = j.value("datatype", ""); + //globalTagSet.format = j.value("format", "GlobalTagSetDto"); - CrestBaseResponse cbr = CrestBaseResponse::from_json(j); +/* CrestBaseResponse cbr = CrestBaseResponse::from_json(j); if (cbr.page.has_value()) { globalTagSet.page = cbr.page.value(); } if (cbr.filter.has_value()) { globalTagSet.filter = cbr.filter.value(); - } + }*/ return globalTagSet; } GlobalTagDto::GlobalTagDto(): name(""), validity(0), description(""), release(""), - insertionTime(""), snapshotTime(""), scenario(""), workflow(""), type("T"), + snapshotTime(""), scenario(""), workflow(""), type("T"), snapshotTimeMilli(0), insertionTimeMilli(0){ } GlobalTagDto::GlobalTagDto(const char* l_name, const char* l_description, const char* l_release, const char* l_workflow): name(l_name), validity(0), description(l_description), release(l_release), - insertionTime(""), snapshotTime(""), scenario(""), workflow(l_workflow), type("T"), + snapshotTime(""), scenario(""), workflow(l_workflow), type("T"), snapshotTimeMilli(0), insertionTimeMilli(0){ } @@ -144,7 +151,8 @@ js["validity"]=validity; js["description"]=description; js["release"]=release; - js["insertionTime"]=insertionTime; + if(insertionTime.has_value()) + js["insertionTime"]=insertionTime.value(); js["snapshotTime"]=snapshotTime; js["scenario"]=scenario; js["workflow"]=workflow; @@ -160,7 +168,8 @@ globalTag.validity = j.value<long_t>("validity", 0); globalTag.description = j.value("description", ""); globalTag.release = j.value("release", ""); - globalTag.insertionTime = j.value("insertionTime", ""); + if(j.contains(std::string{"insertionTime" })) + globalTag.insertionTime = j.value("insertionTime", ""); globalTag.snapshotTime = j.value("snapshotTime", ""); globalTag.scenario = j.value("scenario", ""); globalTag.workflow = j.value("workflow", ""); @@ -176,22 +185,14 @@ js["timeType"]=timeType; js["payloadSpec"]=objectType; js["synchronization"]=synchronization; - js["insertionTime"]=insertionTime; + if(insertionTime.has_value()) + js["insertionTime"]=insertionTime.value(); js["description"]=description; js["lastValidatedTime"]=lastValidatedTime; js["endOfValidity"]=endOfValidity; - js["modificationTime"]=modificationTime; + if(modificationTime.has_value()) + js["modificationTime"]=modificationTime.value(); return js; - /*{ - {"name", name}, - {"timeType", timeType}, - {"payloadSpec", objectType}, - {"synchronization", synchronization}, - {"description", description}, - {"lastValidatedTime", lastValidatedTime}, - {"endOfValidity", endOfValidity}, - {"insertionTime", insertionTime}, - {"modificationTime", modificationTime}};*/ } TagDto TagDto::from_json(const json &j) { @@ -208,11 +209,12 @@ tag.timeType = j.value("timeType", ""); tag.objectType = j.value("payloadSpec", ""); - tag.synchronization = j.value("synchronization", ""); + tag.synchronization = j.value("synchronization", "none"); tag.description = j.value("description", ""); tag.lastValidatedTime = j.value<long_t>("lastValidatedTime", 0); tag.endOfValidity = j.value<long_t>("endOfValidity", 0); - tag.insertionTime = j.value("insertionTime", ""); + if(j.contains(std::string{"insertionTime" })) + tag.insertionTime = j.value("insertionTime", ""); tag.modificationTime = j.value("modificationTime", ""); return tag; } @@ -230,23 +232,24 @@ TagSetDto TagSetDto::from_json(const json &j) { TagSetDto tagSet; + tagSet.load_from_json(j); json jsonResources = j.value("resources", json::array()); for (auto it = jsonResources.begin(); it != jsonResources.end(); ++it) { tagSet.resources.push_back(TagDto::from_json(*it)); } - tagSet.setSize(j.value("size", 0)); - tagSet.datatype = j.value("datatype", ""); - tagSet.format = j.value("format", "TagSetDto"); - + //tagSet.setSize(j.value("size", 0)); + //tagSet.datatype = j.value("datatype", ""); + //tagSet.format = j.value("format", "TagSetDto"); +/* CrestBaseResponse cbr = CrestBaseResponse::from_json(j); if (cbr.page.has_value()) { tagSet.page = cbr.page.value(); } if (cbr.filter.has_value()) { tagSet.filter = cbr.filter.value(); - } + }*/ return tagSet; } json GlobalTagMapDto::to_json() const @@ -281,12 +284,13 @@ GlobalTagMapSetDto GlobalTagMapSetDto::from_json(const json &j) { GlobalTagMapSetDto tagMapSet; + tagMapSet.load_from_json(j); json jsonResources = j.value("resources", json::array()); for (auto it = jsonResources.begin(); it != jsonResources.end(); ++it) { tagMapSet.resources.push_back(GlobalTagMapDto::from_json(*it)); } - +/* tagMapSet.setSize(j.value("size", 0)); tagMapSet.datatype = j.value("datatype", ""); tagMapSet.format = j.value("format", "GlobalTagMapSetDto"); @@ -297,46 +301,130 @@ } if (cbr.filter.has_value()) { tagMapSet.filter = cbr.filter.value(); - } + }*/ return tagMapSet; } GlobalTagMapSetDto GlobalTagMapSetDto::from_fs_json(const json &j) { GlobalTagMapSetDto tagMapSet; - int n = j.size(); + tagMapSet.load_from_json(j); + //int n = j.size(); for (auto it = j.begin(); it != j.end(); ++it) { tagMapSet.resources.push_back(GlobalTagMapDto::from_json(*it)); } - tagMapSet.setSize(n); + //tagMapSet.setSize(n); tagMapSet.datatype = "maps"; - tagMapSet.format = "GlobalTagMapSetDto"; + //tagMapSet.format = "GlobalTagMapSetDto"; return tagMapSet; } + void ChannelSetDto::add(std::string id,std::string name){ + m_channels.push_back(std::pair<std::string,std::string>(id,name)); + } + ChannelSetDto ChannelSetDto::from_json(const json &j) + { + ChannelSetDto chDto; + for(unsigned int i=0;i<j.size();i++) + { + for (auto& el : j[i].items()) + { + chDto.add(el.key(),el.value()); + break; + } + } + return chDto; + } + json ChannelSetDto::to_json() const{ + json chJson=json::array(); + for (auto &ch : m_channels) + { + json obj={}; + obj[ch.first]=ch.second; + chJson.push_back(obj); + } + return chJson; + } + void PayloadSpecDto::add(std::string name,std::string type){ + m_row.push_back(std::pair<std::string,std::string>(name,type)); + } + PayloadSpecDto PayloadSpecDto::from_json(const json &j) + { + PayloadSpecDto chDto; + for(unsigned int i=0;i<j.size();i++) + { + for (auto& el : j[i].items()) + { + chDto.add(el.key(),el.value()); + break; + } + } + return chDto; + } + json PayloadSpecDto::to_json() const{ + json chJson=json::array(); + for (auto &ch : m_row) + { + json obj={}; + obj[ch.first]=ch.second; + chJson.push_back(obj); + } + return chJson; + } + json TagInfoDto::to_json() const{ + json tagInfo={}; + tagInfo["node_description"]=m_node_description; + tagInfo["payload_spec"]=m_payload_spec.to_json(); + tagInfo["channel_list"]=m_channel_list.to_json(); + return tagInfo; + } + TagInfoDto TagInfoDto::from_json(const json &j){ + std::string node_description=j.value("node_description", ""); + json jsonSpec = j.value("payload_spec", json::array()); + if(jsonSpec.is_string()) + jsonSpec=json::parse(to_string(jsonSpec)); + PayloadSpecDto payload_spec=PayloadSpecDto::from_json(jsonSpec); + json jsonCh = j.value("channel_list", json::array()); + if(jsonCh.is_string()) + jsonCh=json::parse(to_string(jsonCh)); + ChannelSetDto channel_list=ChannelSetDto::from_json(jsonCh); + TagInfoDto dto(node_description,payload_spec,channel_list); + return dto; + } + + json TagMetaDto::to_json() const { - return { - {"tagName", tagName}, - {"description", description}, - {"chansize", chansize}, - {"colsize", colsize}, - {"tagInfo", tagInfo}, - {"insertionTime", insertionTime}}; + json tagMeta={}; + tagMeta["tagName"]=tagName; + tagMeta["description"]=description; + tagMeta["chansize"]=tagInfo.getChannelSize(); + tagMeta["colsize"]=tagInfo.getColumnSize(); + tagMeta["tagInfo"]=tagInfo.to_json(); + if(insertionTime.has_value()) + tagMeta["insertionTime"]=insertionTime.value(); + return tagMeta; } TagMetaDto TagMetaDto::from_json(const json &j) { TagMetaDto tag; tag.tagName = j.value("tagName", ""); - tag.chansize = j.value("chansize", 0); - tag.colsize = j.value("colsize", 0); - tag.tagInfo = j.value("tagInfo", ""); + json infoJs=j["tagInfo"]; + if(infoJs.is_string()){ + std::istringstream ss( to_string(infoJs) ); + std::string st; + ss >> std::quoted(st); + infoJs=json::parse(st); + } + tag.tagInfo = TagInfoDto::from_json(infoJs); + tag.description = j.value("description", ""); - tag.insertionTime = j.value("insertionTime", ""); + if(j.contains(std::string{"insertionTime" })) + tag.insertionTime = j.value("insertionTime", ""); return tag; } @@ -354,12 +442,13 @@ TagMetaSetDto TagMetaSetDto::from_json(const json &j) { TagMetaSetDto tagSet; + tagSet.load_from_json(j); json jsonResources = j.value("resources", json::array()); for (auto it = jsonResources.begin(); it != jsonResources.end(); ++it) { tagSet.resources.push_back(TagMetaDto::from_json(*it)); } - +/* tagSet.setSize(j.value("size", 0)); tagSet.datatype = j.value("datatype", ""); tagSet.format = j.value("format", ""); @@ -370,17 +459,19 @@ } if (cbr.filter.has_value()) { tagSet.filter = cbr.filter.value(); - } + }*/ return tagSet; } json IovDto::to_json() const { - return { - {"tagName", tagName}, - {"since", since}, - {"insertionTime", insertionTime}, - {"payloadHash", payloadHash}}; + json iov={}; + iov["tagName"]=tagName; + iov["since"]=since; + if(insertionTime.has_value()) + iov["insertionTime"]=insertionTime.value(); + iov["payloadHash"]=payloadHash; + return iov; } IovDto IovDto::from_json(const json &j) @@ -431,12 +522,13 @@ IovSetDto IovSetDto::from_json(const json &j) { IovSetDto iovSet; + iovSet.load_from_json(j); json jsonResources = j.value("resources", json::array()); for (auto it = jsonResources.begin(); it != jsonResources.end(); ++it) { iovSet.resources.push_back(IovDto::from_json(*it)); } - +/* iovSet.setSize(j.value("size", iovSet.resources.size())); iovSet.datatype = j.value("datatype", ""); iovSet.format = j.value("format", ""); @@ -446,34 +538,44 @@ } if (cbr.filter.has_value()) { iovSet.filter = cbr.filter.value(); - } + }*/ return iovSet; } IovSetDto IovSetDto::from_fs_json(const json &j) { IovSetDto iovSet; - int n = j.size(); + iovSet.load_from_json(j); + //int n = j.size(); for (auto it = j.begin(); it != j.end(); ++it) { iovSet.resources.push_back(IovDto::from_json(*it)); } - iovSet.setSize(n); +// iovSet.setSize(n); iovSet.datatype = "iovs"; - iovSet.format = "IovSetDto"; +// iovSet.format = "IovSetDto"; return iovSet; } json StoreDto::to_json() const { - return { - {"hash", hash}, - {"since", since}, - {"data", data}, - {"streamerInfo", streamerInfo}}; + json dto={}; + dto["hash"]=hash; + dto["since"]=since; + dto["data"]=data; + dto["streamerInfo"]=getStreamerInfo(); + return dto; + } + json StoreDto::getStreamerInfo() const{ + json js={}; + if(m_app_version.has_value()) + js["app_version"]=m_app_version.value(); + if(m_app_name.has_value()) + js["app_name"]=m_app_name.value(); + return js; } StoreDto StoreDto::from_json(const json &j) @@ -482,7 +584,7 @@ storeDto.hash = j.value("hash", ""); storeDto.since = j.value("since", 0.0); storeDto.data = j.value("data", ""); - storeDto.streamerInfo = j.value("streamerInfo", ""); + //storeDto.streamerInfo = j.value("streamerInfo", ""); return storeDto; } @@ -499,14 +601,11 @@ } void StoreSetDto::push_back(StoreDto dto){ resources.push_back(dto); - setSize(size()); - } - size_t StoreSetDto::size(){ - return resources.size(); + //setSize(size()); } void StoreSetDto::clear(){ resources.clear(); - setSize(size()); + //setSize(size()); } StoreSetDto StoreSetDto::from_json(const json &j) @@ -519,8 +618,8 @@ } //storeSetDto.size = j.value("size", storeSetDto.resources.size()); - storeSetDto.datatype = j.value("datatype", "data"); - storeSetDto.format = j.value("format", "StoreSetDto"); + //storeSetDto.datatype = j.value("datatype", "data"); + //storeSetDto.format = j.value("format", "StoreSetDto"); // we are skipping the parse of RespPage for page, and GenericMap for filters return storeSetDto; @@ -530,15 +629,17 @@ json PayloadDto::to_json() const { - return { - {"hash", hash}, - {"version", version}, - {"objectType", objectType}, - {"objectName", objectName}, - {"compressionType", compressionType}, - {"checkSum", checkSum}, - {"size", size}, - {"insertionTime", insertionTime}}; + json payloadDto={}; + payloadDto["hash"]=hash; + payloadDto["version"]=version; + payloadDto["objectType"]=objectType; + payloadDto["objectName"]=objectName; + payloadDto["compressionType"]=compressionType; + payloadDto["checkSum"]=checkSum; + payloadDto["size"]=size; + if(insertionTime.has_value()) + payloadDto["insertionTime"]=insertionTime.value(); + return payloadDto; } // Function to deserialize the object from JSON @@ -553,7 +654,8 @@ payloadDto.compressionType = j.value("compressionType", ""); payloadDto.checkSum = j.value("checkSum", ""); payloadDto.size = j.value("size", 0); - payloadDto.insertionTime = j.value("insertionTime", ""); + if(j.contains(std::string{"insertionTime" })) + payloadDto.insertionTime = j.value("insertionTime", ""); return payloadDto; } @@ -576,6 +678,7 @@ PayloadSetDto PayloadSetDto::from_json(const json &j) { PayloadSetDto payloadSetDto; + payloadSetDto.load_from_json(j); json jsonResources = j.value("resources", json::array()); for (auto it = jsonResources.begin(); it != jsonResources.end(); ++it) { @@ -583,9 +686,9 @@ } // Deserialize CrestBaseResponse part - payloadSetDto.setSize(j.value("size", payloadSetDto.resources.size())); - payloadSetDto.datatype = j.value("datatype", "data"); - payloadSetDto.format = j.value("format", "PayloadSetDto"); + //payloadSetDto.setSize(j.value("size", payloadSetDto.resources.size())); + //payloadSetDto.datatype = j.value("datatype", "data"); + //payloadSetDto.format = j.value("format", "PayloadSetDto"); // we are skipping the parse of RespPage for page, and GenericMap for filters return payloadSetDto;